-
Notifications
You must be signed in to change notification settings - Fork 11
account for RangeError: Array buffer allocation failed #303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| import { beforeEach, describe, test, expect, vi, assert } from 'vitest'; | ||
| import { TestSetupHelpers, transports } from '../testUtil/fixtures/transports'; | ||
| import { BinaryCodec, Codec } from '../codec'; | ||
| import { | ||
| advanceFakeTimersByHeartbeat, | ||
| createPostTestCleanups, | ||
| } from '../testUtil/fixtures/cleanup'; | ||
| import { createServer } from '../router/server'; | ||
| import { createClient } from '../router/client'; | ||
| import { TestServiceSchema } from '../testUtil/fixtures/services'; | ||
| import { waitFor } from '../testUtil/fixtures/cleanup'; | ||
| import { numberOfConnections, closeAllConnections } from '../testUtil'; | ||
| import { cleanupTransports } from '../testUtil/fixtures/cleanup'; | ||
| import { testFinishesCleanly } from '../testUtil/fixtures/cleanup'; | ||
| import { ProtocolError } from '../transport/events'; | ||
|
|
||
| let isOom = false; | ||
| // simulate RangeError: Array buffer allocation failed | ||
| const OomableCodec: Codec = { | ||
| toBuffer(obj) { | ||
| if (isOom) { | ||
| throw new RangeError('failed allocation'); | ||
| } | ||
|
|
||
| return BinaryCodec.toBuffer(obj); | ||
| }, | ||
| fromBuffer: (buff: Uint8Array) => { | ||
| return BinaryCodec.fromBuffer(buff); | ||
| }, | ||
| }; | ||
|
|
||
| describe.each(transports)( | ||
| 'failed allocation test ($name transport)', | ||
| async (transport) => { | ||
| const clientOpts = { codec: OomableCodec }; | ||
| const serverOpts = { codec: BinaryCodec }; | ||
|
|
||
| const { addPostTestCleanup, postTestCleanup } = createPostTestCleanups(); | ||
| let getClientTransport: TestSetupHelpers['getClientTransport']; | ||
| let getServerTransport: TestSetupHelpers['getServerTransport']; | ||
| beforeEach(async () => { | ||
| // only allow client to oom, server has sane oom handling already | ||
| const setup = await transport.setup({ | ||
| client: clientOpts, | ||
| server: serverOpts, | ||
| }); | ||
| getClientTransport = setup.getClientTransport; | ||
| getServerTransport = setup.getServerTransport; | ||
| isOom = false; | ||
|
|
||
| return async () => { | ||
| await postTestCleanup(); | ||
| await setup.cleanup(); | ||
| }; | ||
| }); | ||
|
|
||
| test('oom during heartbeat kills the session, client starts new session', async () => { | ||
| // setup | ||
| const clientTransport = getClientTransport('client'); | ||
| const serverTransport = getServerTransport(); | ||
| const services = { test: TestServiceSchema }; | ||
| const server = createServer(serverTransport, services); | ||
| const client = createClient<typeof services>( | ||
| clientTransport, | ||
| serverTransport.clientId, | ||
| ); | ||
|
|
||
| const errMock = vi.fn(); | ||
| clientTransport.addEventListener('protocolError', errMock); | ||
| addPostTestCleanup(async () => { | ||
| clientTransport.removeEventListener('protocolError', errMock); | ||
| await cleanupTransports([clientTransport, serverTransport]); | ||
| }); | ||
|
|
||
| // establish initial connection | ||
| const result = await client.test.add.rpc({ n: 1 }); | ||
| expect(result).toStrictEqual({ ok: true, payload: { result: 1 } }); | ||
|
|
||
| await waitFor(() => expect(numberOfConnections(serverTransport)).toBe(1)); | ||
| await waitFor(() => expect(numberOfConnections(clientTransport)).toBe(1)); | ||
| const oldClientSession = serverTransport.sessions.get('client'); | ||
| const oldServerSession = clientTransport.sessions.get('SERVER'); | ||
| assert(oldClientSession); | ||
| assert(oldServerSession); | ||
|
|
||
| // simulate some OOM during heartbeat | ||
| for (let i = 0; i < 5; i++) { | ||
| isOom = i % 2 === 0; | ||
| await advanceFakeTimersByHeartbeat(); | ||
| } | ||
|
|
||
| // verify session on client is dead | ||
| await waitFor(() => expect(clientTransport.sessions.size).toBe(0)); | ||
|
|
||
| // verify we got MessageSendFailure errors | ||
| await waitFor(() => { | ||
| expect(errMock).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| type: ProtocolError.MessageSendFailure, | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| // client should be able to reconnect and make new calls | ||
| isOom = false; | ||
| const result2 = await client.test.add.rpc({ n: 2 }); | ||
| expect(result2).toStrictEqual({ ok: true, payload: { result: 3 } }); | ||
|
|
||
| // verify new session IDs are different from old ones | ||
| const newClientSession = serverTransport.sessions.get('client'); | ||
| const newServerSession = clientTransport.sessions.get('SERVER'); | ||
| assert(newClientSession); | ||
| assert(newServerSession); | ||
| expect(newClientSession.id).not.toBe(oldClientSession.id); | ||
| expect(newServerSession.id).not.toBe(oldServerSession.id); | ||
|
|
||
| await testFinishesCleanly({ | ||
| clientTransports: [clientTransport], | ||
| serverTransport, | ||
| server, | ||
| }); | ||
| }); | ||
|
|
||
| test('oom during handshake kills the session, client starts new session', async () => { | ||
| // setup | ||
| const clientTransport = getClientTransport('client'); | ||
| const serverTransport = getServerTransport(); | ||
| const services = { test: TestServiceSchema }; | ||
| const server = createServer(serverTransport, services); | ||
| const client = createClient<typeof services>( | ||
| clientTransport, | ||
| serverTransport.clientId, | ||
| ); | ||
| const errMock = vi.fn(); | ||
| clientTransport.addEventListener('protocolError', errMock); | ||
| addPostTestCleanup(async () => { | ||
| clientTransport.removeEventListener('protocolError', errMock); | ||
| await cleanupTransports([clientTransport, serverTransport]); | ||
| }); | ||
|
|
||
| // establish initial connection | ||
| await client.test.add.rpc({ n: 1 }); | ||
| await waitFor(() => expect(numberOfConnections(serverTransport)).toBe(1)); | ||
| await waitFor(() => expect(numberOfConnections(clientTransport)).toBe(1)); | ||
|
|
||
| // close connection to force reconnection | ||
| closeAllConnections(clientTransport); | ||
| await waitFor(() => expect(numberOfConnections(serverTransport)).toBe(0)); | ||
| await waitFor(() => expect(numberOfConnections(clientTransport)).toBe(0)); | ||
|
|
||
| // simulate OOM during handshake | ||
| isOom = true; | ||
| clientTransport.connect('SERVER'); | ||
| await waitFor(() => expect(numberOfConnections(serverTransport)).toBe(0)); | ||
| await waitFor(() => expect(numberOfConnections(clientTransport)).toBe(0)); | ||
|
|
||
| await waitFor(() => { | ||
| expect(errMock).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| type: ProtocolError.MessageSendFailure, | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| // client should be able to reconnect and make new calls | ||
| isOom = false; | ||
| const result = await client.test.add.rpc({ n: 2 }); | ||
| expect(result).toStrictEqual({ ok: true, payload: { result: 3 } }); | ||
|
|
||
| await testFinishesCleanly({ | ||
| clientTransports: [clientTransport], | ||
| serverTransport, | ||
| server, | ||
| }); | ||
| }); | ||
| }, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { Value } from '@sinclair/typebox/value'; | ||
| import { | ||
| OpaqueTransportMessage, | ||
| OpaqueTransportMessageSchema, | ||
| } from '../transport'; | ||
| import { Codec } from './types'; | ||
| import { DeserializeResult, SerializeResult } from '../transport/results'; | ||
| import { coerceErrorString } from '../transport/stringifyError'; | ||
|
|
||
| /** | ||
| * Adapts a {@link Codec} to the {@link OpaqueTransportMessage} format, | ||
| * accounting for fallibility of toBuffer and fromBuffer and wrapping | ||
| * it with a Result type. | ||
| */ | ||
| export class CodecMessageAdapter { | ||
| constructor(private readonly codec: Codec) {} | ||
|
|
||
| toBuffer(msg: OpaqueTransportMessage): SerializeResult { | ||
| try { | ||
| return { | ||
| ok: true, | ||
| value: this.codec.toBuffer(msg), | ||
| }; | ||
| } catch (e) { | ||
| return { | ||
| ok: false, | ||
| reason: coerceErrorString(e), | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| fromBuffer(buf: Uint8Array): DeserializeResult { | ||
| try { | ||
| const parsedMsg = this.codec.fromBuffer(buf); | ||
| if (!Value.Check(OpaqueTransportMessageSchema, parsedMsg)) { | ||
| return { | ||
| ok: false, | ||
| reason: 'transport message schema mismatch', | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| ok: true, | ||
| value: parsedMsg, | ||
| }; | ||
| } catch (e) { | ||
| return { | ||
| ok: false, | ||
| reason: coerceErrorString(e), | ||
| }; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| export { BinaryCodec } from './binary'; | ||
| export { NaiveJsonCodec } from './json'; | ||
| export type { Codec } from './types'; | ||
| export { CodecMessageAdapter } from './adapter'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should leave doc strings on all of our classes!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
curious why an adapter instead of making the implementation conform to this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
codec is user substitutable and we cant expect every consumer to remember to try/catch toBuffer fromBuffer, so we do it at this layer