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
20 changes: 20 additions & 0 deletions packages/zero-cache/src/types/websocket-handoff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {createServer, type Server} from 'node:http';
import {connect, type AddressInfo} from 'node:net';
import {afterEach, expect, test} from 'vitest';
import {createSilentLogContext} from '../../../shared/src/logging-test-utils.ts';
import type {Worker} from './processes.ts';
import {installWebSocketHandoff} from './websocket-handoff.ts';

let server: Server | undefined;
Expand Down Expand Up @@ -41,3 +42,22 @@ test('a client reset while a handoff is pending does not crash the process', asy
}
expect(uncaught).toEqual([]);
});

test('a handoff whose socket closed on the way does not crash the process', () => {
// Node delivers the message without the socket when the connection closed
// while it was being sent to this process.
let deliver!: (msg: unknown, socket?: unknown) => void;
const parent = {
onMessageType: (_type: string, handler: typeof deliver) => {
deliver = handler;
},
} as unknown as Worker;
installWebSocketHandoff(createSilentLogContext(), () => {}, parent);
expect(() =>
deliver({
message: {url: '/sync/v57/connect', headers: {}},
head: new ArrayBuffer(0),
payload: undefined,
}),
).not.toThrow();
});
6 changes: 6 additions & 0 deletions packages/zero-cache/src/types/websocket-handoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ export function installWebSocketHandoff<P>(
} else {
// handoff messages from this worker's parent.
source.onMessageType<Handoff<P>>('handoff', (msg, socket) => {
// As in installWebSocketReceiver: the connection may have closed while
// it was being sent to this process, and then there's no socket.
if (!socket) {
lc.warn?.('websocket closed during handoff');
return;
}
const {message, head} = msg;
handle(message, socket as Socket, head);
});
Expand Down
Loading