fix(ws): bound inbound frames synchronously and all-or-nothing
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 16s
CI / build (pull_request) Successful in 30s
CI / runtimes (deno) (pull_request) Successful in 19s
CI / runtimes (edge) (pull_request) Successful in 14s

Two gaps in boundInbound, both from review of #117.

The bound ran in an async pump reading the adapter's channel. `ws` emits a
whole TCP read's worth of frames synchronously, so the unbounded channel
took the burst before the pump saw the first frame: with maxWsQueue 2, a
2,000-frame burst reached the channel in full before close(1013). boundInbound now taps
the channel through an internal, non-exported hook that runs inside push(),
so each frame is bounded before it is queued. Measured on the Node
listen() path, the same burst now closes on the 3rd frame.

accept() also offered a frame to consumers in order and closed 1013 when a
later queue was full, so the rejected frame had already reached earlier
consumers. Capacity is now checked for every consumer before any delivery.

Refs #107

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-10 11:42:38 -05:00
1 parent 7fa02d16be
commit 26bacdb304
4 files changed
+96 -11

No files matched your search

+29
View File
@@ -340,6 +340,35 @@ describe('boundInbound (#107)', () => {
expect(closes).toEqual([[1013, 'inbound queue full']]);
});
it('fans out: a frame that overflows any queue reaches no consumer', async () => {
const { socket, inbound, closes } = fakeSocket();
const bounded = boundInbound(socket, { maxWsQueue: 1 });
const fast = bounded.inbound[Symbol.asyncIterator]();
bounded.inbound[Symbol.asyncIterator](); // stalled
const first = fast.next();
inbound.push('a'); // fast takes it, stalled queues it: 1/1
expect(await first).toEqual({ value: 'a', done: false });
const second = fast.next();
inbound.push('b'); // stalled is full: rejected for everyone
expect(closes).toEqual([[1013, 'inbound queue full']]);
expect(await second).toEqual({ value: undefined, done: true });
});
it('enforces the queue bound inside a synchronous burst, before the event loop yields', () => {
const { socket, inbound, closes } = fakeSocket();
const bounded = boundInbound(socket, { maxWsQueue: 2 });
bounded.inbound[Symbol.asyncIterator](); // stalled
for (let i = 0; i < 100_000; i++) inbound.push(String(i)); // what `ws` does with one TCP read
expect(closes).toEqual([[1013, 'inbound queue full']]);
});
it('enforces the size bound synchronously too', () => {
const { socket, inbound, closes } = fakeSocket();
boundInbound(socket, { maxWsMessageBytes: 3 });
inbound.push('abcd');
expect(closes).toEqual([[1009, 'message too large']]);
});
it('T15: keeps the socket capabilities other than inbound', () => {
const { socket } = fakeSocket();
const bounded = boundInbound(socket);