fix(mesh): RPC frames wait for the send buffer too (CARD-26, #41)
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 20s
CI / build (pull_request) Successful in 29s
CI / runtimes (edge) (pull_request) Successful in 19s
CI / runtimes (deno) (pull_request) Successful in 33s

CARD-26 made the WebSocket route pump await `drained()` before each pull,
but mesh RPC traffic sends outside that pump, so it never waited:

- teapot: `handleRpc` runs concurrently per rpc-req, so a teacup that
  keeps sending requests and stops reading grows the teapot's buffer by
  one response each.
- link: `sendRpc` sent each rpc-req at once, whatever the buffer held.

Both now wait for `drained()` before sending. The teapot drops a response
whose socket closed while waiting. The link starts its timeout before
waiting, so a teapot that stops reading still answers 504, and a call
that settles while waiting (timeout or close) is never sent late.

Found in the pre-freeze integrated review.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-24 13:57:35 -05:00
1 parent cdef95ffcb
commit 77d4f32eff
4 files changed
+88 -4

No files matched your search

+38 -1
View File
@@ -11,6 +11,7 @@ const env = { method: 'GET', params: {}, query: {}, body: undefined, headers: {}
// teapot that validates secret 'good', returns a manifest, answers scope rpc (boom -> error)
function startTeapot(secret = 'good') {
const wss = new WebSocketServer({ port: 0 });
const names: string[] = []; // every rpc-req name received, in order
wss.on('connection', (ws) => {
let authed = false;
ws.on('message', (d) => {
@@ -21,13 +22,14 @@ function startTeapot(secret = 'good') {
return;
}
if (f.type === 'rpc-req') {
names.push(f.name);
if (f.name === 'boom') ws.send(encode({ type: 'rpc-res', id: f.id, ok: false, error: { message: 'denied', status: 403 } }));
else ws.send(encode({ type: 'rpc-res', id: f.id, ok: true, result: { got: f.name } }));
}
});
});
const port = (wss.address() as any).port;
return { url: `ws://127.0.0.1:${port}/__mesh__/control`, close: () => wss.close() };
return { url: `ws://127.0.0.1:${port}/__mesh__/control`, names, close: () => wss.close() };
}
// teapot that completes the handshake but NEVER answers rpc-req (for timeout)
@@ -135,4 +137,39 @@ describe('connectLink', () => {
await expect(link.rpc('scope', 'whatever', env)).rejects.toThrow(/timeout/i);
link.close(); t.close();
});
/** The `ws` client, reporting a send buffer over the 1 MiB mark while `stalled`. */
let stalled = false;
const realBuffered = Object.getOwnPropertyDescriptor(WS.prototype, 'bufferedAmount')!.get!;
class Backed extends WS {
constructor(url: string) {
super(url);
Object.defineProperty(this, 'bufferedAmount', { get: () => (stalled ? 2_000_000 : realBuffered.call(this)) });
}
}
const BackedCtor = Backed as unknown as SocketCtor;
it('holds an rpc-req while the link is backed up, and sends it once it drains (#41)', async () => {
const t = startTeapot();
const link = await connectLink({ url: t.url, secret: 'good', Ctor: BackedCtor });
stalled = true;
let settled = false;
const call = link.rpc('scope', 'auth', env).finally(() => (settled = true));
await new Promise((resolve) => setTimeout(resolve, 60));
expect(settled).toBe(false);
stalled = false;
expect(await call).toEqual({ got: 'auth' });
link.close(); t.close();
});
it('times out an rpc still waiting for room, and never sends it (#41)', async () => {
const t = startTeapot();
const link = await connectLink({ url: t.url, secret: 'good', timeoutMs: 100, Ctor: BackedCtor });
stalled = true;
await expect(link.rpc('scope', 'auth', env)).rejects.toMatchObject({ status: 504 });
stalled = false;
expect(await link.rpc('scope', 'next', env)).toEqual({ got: 'next' });
expect(t.names).toEqual(['next']); // the timed-out call was dropped, not sent late
link.close(); t.close();
});
});
+34
View File
@@ -165,4 +165,38 @@ describe('createMeshControl', () => {
const ids = t.sent.filter((f: any) => f.type === 'rpc-res').map((f: any) => f.id);
expect(ids).toEqual(['fast', 'slow']);
});
it('holds rpc responses while the peer is backed up, and sends them once it drains (#41)', async () => {
const t = fakeSocket();
let open: () => void = () => {};
let gate: Promise<void> | undefined;
t.socket.drained = () => gate ?? Promise.resolve();
make().handle(t.socket);
await t.deliver({ type: 'hello', v: V, secret: 'good' });
gate = new Promise<void>((resolve) => (open = resolve));
await t.deliver({ type: 'rpc-req', id: '1', kind: 'scope', name: 'config', ctx: {} });
await t.deliver({ type: 'rpc-req', id: '2', kind: 'scope', name: 'secretsvc', ctx: {} });
expect(t.sent.filter((f: any) => f.type === 'rpc-res')).toEqual([]);
open();
await settle();
expect(t.sent.filter((f: any) => f.type === 'rpc-res').map((f: any) => [f.id, f.ok])).toEqual([
['1', true],
['2', false],
]);
});
it('drops a held response when the socket closes while waiting (#41)', async () => {
const t = fakeSocket();
let gate: Promise<void> | undefined;
t.socket.drained = () => gate ?? Promise.resolve();
make().handle(t.socket);
await t.deliver({ type: 'hello', v: V, secret: 'good' });
let open: () => void = () => {};
gate = new Promise<void>((resolve) => (open = resolve));
await t.deliver({ type: 'rpc-req', id: '1', kind: 'scope', name: 'config', ctx: {} });
t.close();
open(); // a real socket releases its drained() waiters on close
await settle();
expect(t.sent.filter((f: any) => f.type === 'rpc-res')).toEqual([]);
});
});