diff --git a/.changeset/flow-control-frozen-clock.md b/.changeset/flow-control-frozen-clock.md new file mode 100644 index 0000000..906e8ca --- /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 84f7efa..fe129fa 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 8e358c4..6528c63 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 e3ff1f1..66bc947 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) {