Skip to content

fix: keep the stream flow-control window finite on a coarse clock - #307

Open
ndisidore wants to merge 1 commit into
mainfrom
fix/flow-control-nan-window
Open

ndisidore wants to merge 1 commit into
mainfrom
fix/flow-control-nan-window

Conversation

@ndisidore

@ndisidore ndisidore commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

The workerd test applies backpressure when window fills up has been timing out in CI most of the time (example). The test is fine. The stream flow-control window can turn into NaN.

If a write and its ack get the same timestamp, the ack interval and the RTT are both zero and the window works out to Infinity * 0. Once it's NaN, a writer waiting on backpressure is never woken up (the hang), and later writes are never held back at all. workerd's clock only has millisecond resolution, so same-timestamp acks are common there. Node's clock is finer, which is why this never showed up on Node.

The fix: when the interval is zero, leave the window as it is.

Running the whole workerd project locally, the test timed out in 7 of 30 runs before the fix and 0 of 70 after. Those runs had a temporary log line added to confirm the NaN window was the cause each time. It isn't in this PR.

  • Skip the update rather than guess a value. A zero interval has no bandwidth information in it
  • Checking interval > 0 is enough. Everything else is already clamped to the min/max window
  • New unit test with a constant clock, which gave NaN before the fix. The existing workerd test covers the hang end to end
  • Fixed a stale comment in that test (256KB window and ~40KB writes, not 64KB and ~10KB)
  • Left alone: minRtt never resets, so on a coarse clock a busy stream's window drifts down to the 64KB minimum. Slower, not broken, and it was already like that

The bug has been there since streams were added in #132. Node and workerd pass locally. I didn't run the browser projects.

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-bot

changeset-bot Bot commented Nov 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ac54c5a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
capnweb Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Nov 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/capnweb@307
npm i https://pkg.pr.new/cloudflare/capnweb/capnweb-validate@307

commit: ac54c5a

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved — A small, targeted fix that skips the window update when the ack interval is zero, with a regression test; no issues found.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🚀 Release: ✅ LGTM

A patch-level changeset is included for this bug fix to production flow-control code, and a regression test covers it.


🔒 Security: ✅ LGTM

Flow-control arithmetic only; this adds no new trust boundary or attack surface.


📖 AGENTS.md: ✅ LGTM

The change follows the existing conventions of the repository.


📋 Code Quality: ✅ LGTM

The interval > 0 guard correctly keeps the window finite, and the window is still clamped to its min/max on every path that updates it.


⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

The changeset and the corrected test comment describe the behaviour accurately.


Reviewed commit: ac54c5a2

Comment thread src/streams.ts
// 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

@cloudflare-workers-and-pages

Copy link
Copy Markdown

🚀 Deploying Preview to Cloudflare 🚀

Preview URL: https://fix-flow-control-nan-window.pr.capnweb.com (commit ac54c5a)

This URL reflects your latest Preview deployment

Preview Deployments by commit

Status Deployment URL Commit Updated (UTC) See this deployment's details
  • Build: Success ✅
  • Deployment: Success ✅

View logs ↗
https://1e4f774e.pr.capnweb.com ac54c5a 2026-11-20T22:15:23.996Z Visit the dashboard ↗

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant