Skip to content

Commit

Permalink
The RPC request coalescer now throws exceptions whether you configure…
Browse files Browse the repository at this point in the history
… it with an `AbortSignal` or not (#2910)

* The RPC request coalescer now throws exceptions whether you configure it with an `AbortSignal` or not

* Add changeset

* Prettier

---------

Co-authored-by: steveluscher <[email protected]>
  • Loading branch information
Jac0xb and steveluscher authored Jul 22, 2024
1 parent 1b0dde7 commit 42a70f4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-walls-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@solana/rpc': patch
---

Fixed a bug where the RPC would fail to throw errors in the event that you configured it with an `AbortSignal`
21 changes: 21 additions & 0 deletions packages/rpc/src/__tests__/rpc-request-coalescer-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,25 @@ describe('RPC request coalescer', () => {
});
});
});
// https://github.com/solana-labs/solana-web3.js/pull/2910
describe('regression test #2910', () => {
beforeEach(() => {
// Necessary to prevent the coalescer from bailing out.
hashFn.mockReturnValue('samehash');
});
it('throws an error in the case of failure, if it was not configured with an `AbortSignal`', async () => {
expect.assertions(1);
const mockError = { err: 'bad' };
mockTransport.mockRejectedValueOnce(mockError);
await expect(coalescedTransport({ payload: null })).rejects.toBe(mockError);
});
it('throws an error in the case of failure, if it was configured with an `AbortSignal`', async () => {
expect.assertions(1);
const mockError = { err: 'bad' };
mockTransport.mockRejectedValueOnce(mockError);
await expect(coalescedTransport({ payload: null, signal: new AbortController().signal })).rejects.toBe(
mockError,
);
});
});
});
9 changes: 6 additions & 3 deletions packages/rpc/src/rpc-request-coalescer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ export function getRpcTransportWithRequestCoalescing<TTransport extends RpcTrans
reject((e.target as AbortSignal).reason);
};
signal.addEventListener('abort', handleAbort);
responsePromise.then(resolve).finally(() => {
signal.removeEventListener('abort', handleAbort);
});
responsePromise
.then(resolve)
.catch(reject)
.finally(() => {
signal.removeEventListener('abort', handleAbort);
});
});
} else {
return (await coalescedRequest.responsePromise) as TResponse;
Expand Down

0 comments on commit 42a70f4

Please sign in to comment.