fix(ws): wait for the send buffer before pulling the next outbound value (CARD-26, #41)
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 17s
CI / build (pull_request) Successful in 27s
CI / runtimes (edge) (pull_request) Successful in 14s
CI / runtimes (deno) (pull_request) Successful in 29s

The outbound pump pulled a @Ws handler's iterator as fast as it yielded,
whatever the peer read. With a slow client, 10,000 frames of 64 KiB were
all pulled at once and sat in the send buffer: 640 MiB.

WsSocket gains one optional member, drained(): it resolves once the
socket's send buffer is at or below the adapter's high-water mark
(WS_HIGH_WATER_MARK, 1 MiB, internal), or the socket has closed. The
pump awaits it before each pull, so the buffer peaks at the mark plus
one frame. A close ends the wait and the abort path runs the iterator's
return(), as before. A socket without drained() is never waited on.

Per runtime:
- Node: ws.bufferedAmount, released by each send's write callback;
- Bun: getBufferedAmount(), released by the server's drain callback;
- Deno: polls bufferedAmount every 10 ms, only while over the mark;
- workerd reports no buffered amount, so the edge never waits.

The Deno poll also waits while the socket is CONNECTING. Deno hands the
socket over before it opens, and a handler that yielded at once had its
first value pulled into a socket that was not open yet. The pump then
gave up, so the client saw no frame and a 1005 close.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-23 00:29:41 -05:00
1 parent 3455b421a4
commit 1603bcca53
10 files changed
+616 -8

No files matched your search

+78
View File
@@ -0,0 +1,78 @@
// #41 (CARD-26, D23) on Bun: the outbound pump waits while the socket is backed up. Bun has no
// `bufferedAmount` property; the adapter reads `getBufferedAmount()` and waits on `drain`.
// Node twin: test/ws-backpressure.test.ts, which explains the cases.
//
// Run with: npm run test:bun
import 'reflect-metadata';
import { test, expect } from 'bun:test';
import { createApp, Route, Ws, Module } from '../../src/index.ts';
import { serveBun } from '../../src/bun.ts';
import { rawWs } from '../helpers/raw-ws.ts';
const FRAMES = 10_000;
const FRAME = 64 * 1024;
const frame = (i: number) => String(i).padStart(8, '0') + 'x'.repeat(FRAME - 8);
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const state = { pulled: 0, returned: false };
@Route('/bp')
class Flood {
@Ws('/flood')
flood() {
return (async function* () {
try {
for (let i = 0; i < FRAMES; i++) {
state.pulled++;
yield frame(i);
}
} finally {
state.returned = true;
}
})();
}
}
@Module({ mountpoint: '/', controllers: [Flood] })
class M {}
async function serve() {
state.pulled = 0;
state.returned = false;
const server = await serveBun(createApp({ modules: [M] }), { port: 0 });
return { server, port: server.port };
}
test('serveBun: ws outbound stops pulling while the client is not reading, then delivers in order (T1)', async () => {
const { server, port } = await serve();
try {
const client = await rawWs(port, '/bp/flood', FRAME);
client.pause();
await sleep(300);
const stalled = state.pulled;
await sleep(200);
expect(state.pulled).toBe(stalled);
expect(stalled).toBeLessThan(FRAMES / 10);
expect(await client.readAll(FRAMES)).toEqual({ received: FRAMES, inOrder: true });
expect(state.pulled).toBe(FRAMES);
client.destroy();
} finally {
server.stop(true);
}
}, 60_000);
test('serveBun: ws outbound ends the wait and calls return() when the client goes away (T2)', async () => {
const { server, port } = await serve();
try {
const client = await rawWs(port, '/bp/flood', FRAME);
client.pause();
await sleep(300);
expect(state.pulled).toBeLessThan(FRAMES / 10);
client.destroy();
await sleep(300);
expect(state.returned).toBe(true);
expect(state.pulled).toBeLessThan(FRAMES / 10);
} finally {
server.stop(true);
}
}, 60_000);