Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/flow-control-frozen-clock.md
Original file line number Diff line number Diff line change
@@ -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.
30 changes: 30 additions & 0 deletions __tests__/flow-control.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
4 changes: 2 additions & 2 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
62 changes: 35 additions & 27 deletions src/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heads up, this diff looks way scarier than it is. the only real change is this if + the comment above it, everything below just got indented a level (and one comment re-wrapped to fit). hide whitespace and it's like 10 lines

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) {
Expand Down
Loading