From ac54c5a2cde0873a4b11cfa29f9914303c2d749f Mon Sep 17 00:00:00 2001 From: Nathan Disidore Date: Fri, 20 Nov 2026 16:14:14 -0600 Subject: [PATCH] fix: keep the stream flow-control window finite on a coarse clock When a write and its ack share a timestamp, the ack interval and RTT are both zero and the window was computed as Infinity * 0 = NaN. A NaN window never unblocks a waiting writer and never blocks a new one. Skip the window update when the interval is zero. Fixes the intermittent workerd timeout of 'applies backpressure when window fills up'. --- .changeset/flow-control-frozen-clock.md | 5 ++ __tests__/flow-control.test.ts | 30 ++++++++++++ __tests__/index.test.ts | 4 +- src/streams.ts | 62 ++++++++++++++----------- 4 files changed, 72 insertions(+), 29 deletions(-) create mode 100644 .changeset/flow-control-frozen-clock.md diff --git a/.changeset/flow-control-frozen-clock.md b/.changeset/flow-control-frozen-clock.md new file mode 100644 index 00000000..906e8ca3 --- /dev/null +++ b/.changeset/flow-control-frozen-clock.md @@ -0,0 +1,5 @@ +--- +"capnweb": patch +--- + +Fix the flow-control window of a `WritableStream` sent over RPC becoming `NaN` when the clock does not advance between a write and its acknowledgement (e.g. on Cloudflare Workers, where the clock is coarse). A writer waiting on backpressure was never released, or backpressure stopped being applied. diff --git a/__tests__/flow-control.test.ts b/__tests__/flow-control.test.ts index 84f7efa6..fe129fa8 100644 --- a/__tests__/flow-control.test.ts +++ b/__tests__/flow-control.test.ts @@ -251,4 +251,34 @@ describe("FlowController", () => { // Window should have been clamped at MIN_WINDOW. expect(sim.fc.window).toStrictEqual(64 * 1024); }); + + it("keeps a usable window when the clock does not advance between send and ack", () => { + // A coarse clock (e.g. workerd's) can give a send and its ack the same timestamp. + let fc = new FlowController(() => 1000); + + let tokens: SendToken[] = []; + for (;;) { + let { token, shouldBlock } = fc.onSend(40000); + tokens.push(token); + if (shouldBlock) break; + } + + let unblocked = false; + for (let token of tokens) { + unblocked = fc.onAck(token); + expect(fc.window).toBeGreaterThanOrEqual(64 * 1024); + expect(fc.window).toBeLessThanOrEqual(1024 * 1024 * 1024); + } + + // Everything was acked, so a blocked sender must be released. + expect(fc.bytesInFlight).toBe(0); + expect(unblocked).toBe(true); + + // And the window must still be able to block a sender again. + let blocked = false; + for (let i = 0; i < 100000 && !blocked; i++) { + blocked = fc.onSend(40000).shouldBlock; + } + expect(blocked).toBe(true); + }); }); diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 8e358c4f..6528c631 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -3678,8 +3678,8 @@ describe("WritableStream over RPC", () => { if (writesSent == oldWritesSent) break; } - // With a 64KB window and ~10KB per write, the sender should have been blocked after about - // 7 writes. Without flow control, all 20 writes would be sent. + // With a 256KB initial window and ~40KB per write, the sender should have been blocked after + // about 7 writes. Without flow control, all 20 writes would be sent. expect(writesSent).toBeGreaterThanOrEqual(5); expect(writesSent).toBeLessThanOrEqual(10); expect(writesReceived).toBe(0); // Client hasn't received any writes yet. diff --git a/src/streams.ts b/src/streams.ts index e3ff1f1c..66bc9472 100644 --- a/src/streams.ts +++ b/src/streams.ts @@ -256,37 +256,45 @@ export class FlowController { } let interval = ackTime - baseTime; - let bytes = this.delivered - baseDelivered; - let bandwidth = bytes / interval; - // Choose our target growth factor depending on whether we're at startup or steady - // state. - let growthFactor = this.inStartupPhase ? STARTUP_GROWTH_FACTOR : STEADY_GROWTH_FACTOR; - - // Calculate new window to be our calculated bandwidth-delay product, plus a growth - // factor to account for the possibility that bandwidth is constrained only due to - // the window having been too small. - let newWindow = bandwidth * this.minRtt * growthFactor; - - // Don't allow the window to grow too quickly -- it can only grow by at most - // `growthFactor` for each RTT. - newWindow = Math.min(newWindow, token.windowAtSend * growthFactor); + // On a coarse clock (e.g. workerd's, which has millisecond resolution) acks can share a + // timestamp. A zero interval gives no bandwidth sample, and computing one anyway yields + // Infinity -- or NaN once multiplied by a zero RTT. A NaN window never blocks a sender + // and never unblocks one already waiting. Leave the window alone until an ack actually + // measures something. + if (interval > 0) { + let bytes = this.delivered - baseDelivered; + let bandwidth = bytes / interval; + + // Choose our target growth factor depending on whether we're at startup or steady + // state. + let growthFactor = this.inStartupPhase ? STARTUP_GROWTH_FACTOR : STEADY_GROWTH_FACTOR; + + // Calculate new window to be our calculated bandwidth-delay product, plus a growth + // factor to account for the possibility that bandwidth is constrained only due to + // the window having been too small. + let newWindow = bandwidth * this.minRtt * growthFactor; + + // Don't allow the window to grow too quickly -- it can only grow by at most + // `growthFactor` for each RTT. + newWindow = Math.min(newWindow, token.windowAtSend * growthFactor); + + if (token.windowFullAtSend) { + // Don't allow the window to shrink too quickly. + newWindow = Math.max(newWindow, token.windowAtSend * DECAY_FACTOR); + } else { + // Don't allow the window to shrink at all if we weren't saturating it -- in this + // case the sending app is not fully utilizing the connection, so no backpressure is + // needed. We clamp to this.window here, not this.windowAtSend, since we don't want to + // undo previous shrinkage, when alternating between sends that saturated and ones + // that didn't. + newWindow = Math.max(newWindow, this.window); + } - if (token.windowFullAtSend) { - // Don't allow the window to shrink too quickly. - newWindow = Math.max(newWindow, token.windowAtSend * DECAY_FACTOR); - } else { - // Don't allow the window to shrink at all if we weren't saturating it -- in this - // case the sending app is not fully utilizing the connection, so no backpressure is - // needed. We clamp to this.window here, not this.windowAtSend, since we don't want to - // undo previous shrinkage, when alternating between sends that saturated and ones that - // didn't. - newWindow = Math.max(newWindow, this.window); + // Clamp to min/max values. + this.window = Math.max(Math.min(newWindow, MAX_WINDOW), MIN_WINDOW); } - // Clamp to min/max values. - this.window = Math.max(Math.min(newWindow, MAX_WINDOW), MIN_WINDOW); - // Check if the startup phase is done. if (this.inStartupPhase && token.sentTime >= this.roundStartTime) { if (this.window > this.lastRoundWindow * STEADY_GROWTH_FACTOR) {