Skip to content

Fix same-turn write+cancel and write-during-abort edge races (#283) - #288

Merged
ryuzdev merged 1 commit into
mainfrom
fix/edge-races-283
Nov 25, 2026
Merged

ryuzdev merged 1 commit into
mainfrom
fix/edge-races-283

Conversation

@ryuzdev

@ryuzdev ryuzdev commented Nov 25, 2026

Copy link
Copy Markdown
Member

Two workerd-diff edge races in the streams implementation (crates/cello/js/writable_stream.js).

Root cause

Race 1: on a plain TransformStream, a write() issued in the same turn as construction sits behind the writable's start gate (undispatched), but the readable-cancel path treated it like a parked write once the gate opened: the erroring path rejected the write, ready and closed all with the cancel reason. Workerd resolves the write and ready instead (the transform never runs -- verified calls: 0 on workerd) and only closed rejects, in the order closed, ready, write.

Race 2: on IdentityTransformStream/FixedLengthStream, a write() landing between abort() and its settlement hit the already-errored state and rejected at once with the abort reason. Workerd follows its abortPromise lifecycle instead: the write settles after the abort with the closed-stream TypeError, and closed rejects last. The abort-waiting-for-in-flight-write deferral also never applied to byte streams (they park in _deliver with no reader coming), so abort() with a parked write and no reader deadlocked instead of unparking the write with the abort reason.

Fix

  • Readable cancel on a plain transform with a still-shut start gate and queued writes settles closed first, then ready, then resolves and drops the queue, before erroring the stream for the gate.
  • __write on internal streams with a recorded abort request chains on st.abortPromise (throwing the closed-stream TypeError, so no extra thenable-adoption tick) instead of rejecting with the stored error; __finishErroring defers the close/closed rejections one hop past those chained writes.
  • The abort deferral now only applies to sinks parked on the readable handshake (unblockWrite set); byte streams take the erroring path so the abort hook unparks a parked write with the abort reason.

Fail-before / after evidence (SHOW_TRANSCRIPTS=1)

On origin/main both new cases diverge; after the fix all 298 gate cases agree:

  • transform-same-turn-cancel: main gave write/ready/closed all rejected with same-turn; workerd (and now Cello) give write: done, ready: ready-ok, closed rejected, order [closed-rejected, ready-ok, write-ok], desiredSize: null.
  • identity-fixed-write-during-abort: main gave in-flight writes rejected with abort-boom synchronously; workerd (and now Cello) give abort: aborted, valid/invalid writes rejected with This WritableStream has been closed., ready: ok, closed rejected with abort-boom, order [abort-ok, valid/invalid-rejected, closed-rejected], for both idle and parked shapes on both stream kinds.

Checks: cargo fmt --check, cargo clippy --profile ci --locked --all-targets -- -D warnings, full cargo test --profile ci --locked -p cello --no-fail-fast (0 failures), full workerd-diff gate (298 agree), node scripts/check-docs.mjs -- all green.

Closes #283

@coderabbitai

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 18 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3b539453-c19e-45be-bf37-ffbafcdd38fd
Commits

Reviewing files that changed from the base of the PR and between 4390228 and fc6c55c.

Files selected for processing: 4

  • crates/cello/js/writable_stream.js
  • docs/changelog.md
  • scripts/workerd-diff/cases.mjs
  • scripts/workerd-diff/corpus/streams/index.js
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

- A write() followed by a readable cancel() in the same turn on a plain TransformStream now resolves the write and ready while closed rejects with the cancel reason; writes between abort() and its settlement on identity/fixed streams follow the abort and report the closed stream, with closed last.
- Gate cases transform-same-turn-cancel and identity-fixed-write-during-abort agree with workerd 1.20261121.1.
@ryuzdev
ryuzdev merged commit 4643ddf into main Nov 25, 2026
9 checks passed
@ryuzdev
ryuzdev deleted the fix/edge-races-283 branch November 25, 2026 07:52
@ryuzdev ryuzdev mentioned this pull request Nov 25, 2026
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.

workerd diff (edge races): same-turn write+cancel, and a write between abort() and its settlement on identity/fixed streams

1 participant