fix(ws): give every inbound consumer its own bounded queue
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 15s
CI / build (pull_request) Successful in 31s
CI / runtimes (deno) (pull_request) Successful in 20s
CI / runtimes (edge) (pull_request) Successful in 14s

boundInbound returned one shared FIFO to every `inbound` iterator, with a
single waiter slot. Two consumers waiting at once left one hanging forever,
sequential consumers split frames, and one consumer's return() ended the
stream for all. Node used to fan out through `channel`; Deno, Bun and edge
already shared one iterator before #107, so the bug was older there.

Each iterator now gets its own queue, and maxWsQueue bounds each one. Frames
that arrive before anyone reads still go to the first consumer, which keeps
the mesh `hello`.

Refs #107

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-10 11:23:38 -05:00
1 parent f78c9dc6fd
commit 7fa02d16be
2 files changed
+120 -37

No files matched your search

+38
View File
@@ -302,6 +302,44 @@ describe('boundInbound (#107)', () => {
expect((await it.next()).done).toBe(true);
});
it('fans out: two consumers waiting at once both receive the frame', async () => {
const { socket, inbound } = fakeSocket();
const bounded = boundInbound(socket);
const a = bounded.inbound[Symbol.asyncIterator]();
const b = bounded.inbound[Symbol.asyncIterator]();
const [nextA, nextB] = [a.next(), b.next()];
inbound.push('x');
expect(await nextA).toEqual({ value: 'x', done: false });
expect(await nextB).toEqual({ value: 'x', done: false });
});
it('fans out: a consumer that returns does not end the next one', async () => {
const { socket, inbound } = fakeSocket();
const bounded = boundInbound(socket);
const first = bounded.inbound[Symbol.asyncIterator]();
const firstNext = first.next();
inbound.push('a');
expect(await firstNext).toEqual({ value: 'a', done: false });
await first.return?.();
const second = bounded.inbound[Symbol.asyncIterator]();
const secondNext = second.next();
inbound.push('b');
expect(await secondNext).toEqual({ value: 'b', done: false });
});
it('fans out: the bound applies to each consumer, so one stalled reader closes 1013', async () => {
const { socket, inbound, closes } = fakeSocket();
const bounded = boundInbound(socket, { maxWsQueue: 2 });
const reader = bounded.inbound[Symbol.asyncIterator]();
bounded.inbound[Symbol.asyncIterator](); // subscribed, never reads
for (const m of ['a', 'b', 'c']) {
const got = reader.next();
inbound.push(m);
await got;
}
expect(closes).toEqual([[1013, 'inbound queue full']]);
});
it('T15: keeps the socket capabilities other than inbound', () => {
const { socket } = fakeSocket();
const bounded = boundInbound(socket);