Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .changeset/sdk-error-handling-fixes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@audius/sdk': patch
---

Fix several error-handling bugs:

- Uploads reject when transcode/resize polling fails instead of never settling, and aborting an upload stops status polling right away.
- The storage node selector reorders using the refreshed node list after an earlier node list fetch failed.
- The request signature middleware no longer attaches a previously cached signature after the wallet is gone (e.g. after logout) or signing fails.
- `SolanaRelayWalletAdapter.connect()` resets `connecting` when fetching the fee payer fails.
- `SolanaRelay.relay()` rethrows the original response error when the error body isn't JSON.
- `SolanaClient.buildTransaction()` rounds the percentile-based priority fee, which threw on fractional values.
- Chats: a failed public key fetch is no longer cached, and errors handling websocket events are logged instead of becoming unhandled rejections.
16 changes: 14 additions & 2 deletions packages/sdk/src/sdk/api/chats/ChatsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,14 @@ export class ChatsApi

private async getPublicKey(userId: string): Promise<Uint8Array> {
if (!this.publicKeyCache[userId]) {
this.publicKeyCache[userId] = this.fetchPublicKey(userId)
const promise = this.fetchPublicKey(userId)
this.publicKeyCache[userId] = promise
// Don't cache failures, so a transient error can be retried
promise.catch(() => {
if (this.publicKeyCache[userId] === promise) {
delete this.publicKeyCache[userId]
}
})
}
const cachedPromise = this.publicKeyCache[userId]
if (!cachedPromise) {
Expand Down Expand Up @@ -1049,7 +1056,12 @@ export class ChatsApi
})
}
}
handleAsync()
handleAsync().catch((e) => {
this.logger.error(
'[audius-sdk]: Error: Failed to handle websocket event',
e
)
})
})
ws.addEventListener('open', () => {
this.eventEmitter.emit('open')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { generatePrivateKey } from 'viem/accounts'
import { describe, it, expect, vi } from 'vitest'

import { createAppWalletClient, Logger } from '../services'
Expand Down Expand Up @@ -105,6 +106,39 @@ describe('addRequestSignatureMiddleware', () => {
expect(ctx?.init.headers).not.toHaveProperty('Encoded-Data-Signature')
})

it('does not reuse a cached signature once the wallet is gone', async () => {
const appWalletClient = createAppWalletClient({
apiKey: '',
apiSecret: generatePrivateKey()
})
const getAddresses = vi.fn(() => appWalletClient.getAddresses())
const services = {
audiusWalletClient: {
...appWalletClient,
getAddresses,
signMessage: appWalletClient.signMessage.bind(appWalletClient)
} as any,
logger: { warn: vi.fn() } as any
}

const m = addRequestSignatureMiddleware({ services })
const request = () =>
m.pre?.({
fetch,
url: 'example.com',
init: { headers: { 'x-something': 'value' } }
})

const ctx = await request()
expect(ctx?.init.headers).toHaveProperty('Encoded-Data-Signature')

// Simulate logging out
getAddresses.mockRejectedValue(new HedgehogWalletNotFoundError())
const ctx2 = await request()
expect(ctx2?.init.headers).not.toHaveProperty('Encoded-Data-Message')
expect(ctx2?.init.headers).not.toHaveProperty('Encoded-Data-Signature')
})

it('logs warning for other errors', async () => {
const mockLogger = {
warn: vi.fn()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ export const addRequestSignatureMiddleware = ({
timestamp = currentTimestamp
}
} catch (e) {
// Don't keep sending a stale signature, e.g. after logout
message = null
signature = null
signatureAddress = null
timestamp = null
// Don't log a warning for HedgehogWalletNotFoundError as it's expected when user is logged out
if (!(e instanceof HedgehogWalletNotFoundError)) {
logger?.warn(`Unable to add request signature: ${e}`)
Expand Down
6 changes: 5 additions & 1 deletion packages/sdk/src/sdk/services/Solana/SolanaRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,12 @@ export class SolanaRelay extends BaseAPI {
// SendTransactionError to transparently raise to the caller.
if (e instanceof Error && e.name === 'ResponseError') {
const resp = (e as runtime.ResponseError).response.clone()
const body = await resp.json()
// The error body isn't always JSON; if it can't be parsed, surface the
// original ResponseError instead of a SyntaxError
const body = await resp.json().catch(() => null)
if (
body !== null &&
typeof body === 'object' &&
'error' in body &&
'transactionMessage' in body &&
'signature' in body
Expand Down
17 changes: 10 additions & 7 deletions packages/sdk/src/sdk/services/Solana/SolanaRelayWalletAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,17 @@ export class SolanaRelayWalletAdapter implements SolanaWalletAdapter {
*/
public async connect() {
this._connecting = true
this._publicKey = await this.solanaRelay.getFeePayer()
if (!this._publicKey) {
throw new Error(
'Failed to connect SolanaRelayWalletAdapter: Failed to get fee payer.'
)
try {
this._publicKey = await this.solanaRelay.getFeePayer()
if (!this._publicKey) {
throw new Error(
'Failed to connect SolanaRelayWalletAdapter: Failed to get fee payer.'
)
}
this._connected = true
} finally {
this._connecting = false
}
this._connecting = false
this._connected = true
}

public async disconnect() {
Expand Down
52 changes: 52 additions & 0 deletions packages/sdk/src/sdk/services/Solana/programs/SolanaClient.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {
ComputeBudgetProgram,
Keypair,
SystemProgram,
TransactionMessage
} from '@solana/web3.js'
import { describe, it, expect, vi } from 'vitest'

import type { SolanaWalletAdapter } from '../types'

import { SolanaClient } from './SolanaClient'

const feePayer = Keypair.generate().publicKey
const recentBlockhash = Keypair.generate().publicKey.toBase58()

describe('SolanaClient', () => {
describe('buildTransaction', () => {
// The default VERY_HIGH priority uses a 1.5x multiplier, so an odd recent
// fee produces a fractional microLamports value
it('rounds a fractional priority fee', async () => {
const client = new SolanaClient({
rpcEndpoints: ['http://localhost:8899'],
solanaWalletAdapter: {} as SolanaWalletAdapter
})
vi.spyOn(
client.connection,
'getRecentPrioritizationFees'
).mockResolvedValue([{ slot: 1, prioritizationFee: 200_001 }])

const transaction = await client.buildTransaction({
feePayer,
recentBlockhash,
computeLimit: { units: 1000 },
instructions: [
SystemProgram.transfer({
fromPubkey: feePayer,
toPubkey: Keypair.generate().publicKey,
lamports: 1
})
]
})

const { instructions } = TransactionMessage.decompile(transaction.message)
const expected = ComputeBudgetProgram.setComputeUnitPrice({
microLamports: 300_002
})
expect(
instructions.some((i) => Buffer.from(i.data).equals(expected.data))
).toBe(true)
})
})
})
9 changes: 6 additions & 3 deletions packages/sdk/src/sdk/services/Solana/programs/SolanaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,12 @@ export class SolanaClient {
0
)
const baseFee = (orderedFees[percentileIndex] ?? 0) * multiplier
const microLamports = Math.min(
Math.max(baseFee, priorityFee.minimumMicroLamports ?? 0),
priorityFee.maximumMicroLamports ?? Number.MAX_SAFE_INTEGER
// setComputeUnitPrice converts to BigInt, which throws on fractions
const microLamports = Math.round(
Math.min(
Math.max(baseFee, priorityFee.minimumMicroLamports ?? 0),
priorityFee.maximumMicroLamports ?? Number.MAX_SAFE_INTEGER
)
)
if (microLamports !== undefined) {
instructions.push(
Expand Down
24 changes: 24 additions & 0 deletions packages/sdk/src/sdk/services/Storage/Storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,30 @@ describe('pollProcessingStatus', () => {
expect(mockFetch).toHaveBeenCalledTimes(1)
})

it('stops polling once the upload is aborted', async () => {
const ac = new AbortController()
mockFetch.mockImplementation(async () => {
ac.abort()
return statusResponse({ id: 'upload-1', status: 'busy' })
})

const storage = new Storage({ storageNodeSelector: nodeSelector })
const pollWithSignal = (
storage as unknown as {
pollProcessingStatus: (
id: string,
template: string,
total: number,
onProgress: undefined,
abortSignal: AbortSignal
) => Promise<unknown>
}
).pollProcessingStatus('upload-1', 'audio', 1, undefined, ac.signal)

await expect(pollWithSignal).rejects.toThrow('Upload aborted')
expect(mockFetch).toHaveBeenCalledTimes(1)
}, 20000)

// Image resizes have no '320' result to wait for; the gate is audio-only.
it('does not require a 320 result for image templates', async () => {
mockFetch.mockResolvedValue(
Expand Down
15 changes: 9 additions & 6 deletions packages/sdk/src/sdk/services/Storage/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,22 @@ export class Storage implements StorageService {
transcode: 0
})
},
onSuccess: async () => {
onSuccess: () => {
const uploadId = upload?.url?.split('/').pop()
if (!uploadId) {
reject(new Error('No upload ID received'))
return
}
const res = await this.pollProcessingStatus(
// tus doesn't await onSuccess, so forward polling failures
this.pollProcessingStatus(
uploadId,
metadata.template,
cachedTotal,
onProgress,
ac.signal
)
resolve(res)
.then(resolve)
.catch(reject)
}
})

Expand Down Expand Up @@ -241,10 +243,11 @@ export class Storage implements StorageService {
: MAX_IMAGE_RESIZE_TIMEOUT_MS

while (Date.now() - start < maxPollingMs) {
// Outside the try so the abort isn't caught and retried
if (abortSignal?.aborted) {
throw new Error('Upload aborted')
}
try {
if (abortSignal?.aborted) {
throw new Error('Upload aborted')
}
const resp = await this.getProcessingStatus(id)
if (resp?.status === 'done') {
if (template !== 'audio' || resp.results?.['320']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,43 @@ describe('StorageNodeSelector', () => {
expect(second).toEqual(storageNodeC.endpoint)
expect(healthCheckCalls).toBe(1)
})

it('reorders with the refreshed node list after a failed fetch', async () => {
let healthCheckCalls = 0
const storageNodeC = {
endpoint: 'https://node-c.audius.co',
delegateOwnerWallet: '0xc0ffee254729296a45a3885639AC7E10F9d54973'
}
server.use(
rest.get(`${discoveryNode}/health_check`, (_req, res, ctx) => {
healthCheckCalls++
if (healthCheckCalls === 1) {
return res.networkError('Failed to connect')
}
const data: HealthCheckResponseData = {
network: { content_nodes: [storageNodeC] }
}
return res(ctx.status(200), ctx.json({ data }))
}),
rest.get(`${storageNodeC.endpoint}/health_check`, (_req, res, ctx) => {
return res(ctx.status(200), ctx.json({ data: { diskHasSpace: true } }))
})
)

const storageNodeSelector = new StorageNodeSelector({
bootstrapNodes: [storageNodeA, storageNodeB],
logger,
endpoint: discoveryNode
})

// The first fetch fails, so selection falls back to the bootstrap nodes
expect([storageNodeA.endpoint, storageNodeB.endpoint]).toContain(
await storageNodeSelector.getSelectedNode()
)

expect(await storageNodeSelector.getSelectedNode(true)).toEqual(
storageNodeC.endpoint
)
expect(healthCheckCalls).toBe(2)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export class StorageNodeSelector implements StorageNodeSelectorService {
}

this.nodes = contentNodes
// Drop any ordering computed from the bootstrap nodes
this.orderedNodes = undefined
this.selectionState = 'healthy_only'
return true
}
Expand Down Expand Up @@ -127,22 +129,24 @@ export class StorageNodeSelector implements StorageNodeSelectorService {
if (!this.orderedNodes?.length) {
this.orderedNodes = this.orderNodes(new Date().toString())
}
// A node list refresh can replace this.orderedNodes while we await below
const orderedNodes = this.orderedNodes

if (this.orderedNodes.length === 0) {
if (orderedNodes.length === 0) {
return undefined
}

const currentNodeIndex = this.selectedNode
? this.orderedNodes.indexOf(this.selectedNode)
? orderedNodes.indexOf(this.selectedNode)
: -1

let selectedNode: string | undefined
let nextNodeIndex = currentNodeIndex

while (nextNodeIndex !== this.orderedNodes.length - 1) {
while (nextNodeIndex !== orderedNodes.length - 1) {
nextNodeIndex++
const nextNode = this.orderedNodes[nextNodeIndex]
if (!nextNode) continue // should never happen unless this.orderedNodes has falsy values
const nextNode = orderedNodes[nextNodeIndex]
if (!nextNode) continue // should never happen unless orderedNodes has falsy values
if (await isNodeHealthy(nextNode)) {
selectedNode = nextNode
break
Expand Down
Loading