Skip to content

fix: settle a compression cancel racing an abort like workerd (#252) - #255

Merged
ryuzdev merged 1 commit into
mainfrom
fix/compression-abort-race
Nov 24, 2026
Merged

ryuzdev merged 1 commit into
mainfrom
fix/compression-abort-race

Conversation

@ryuzdev

@ryuzdev ryuzdev commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

What

Issue #252: a CompressionStream/DecompressionStream with a write in
flight, a pending read(), and a synchronous reader.cancel("cancel-reason")
then writer.abort("abort-reason") answered the cancel reason everywhere.
Workerd answers:

step workerd before after
write() resolves resolves resolves
cancel() resolves resolves resolves
abort() resolves rejects cancel-reason resolves
writer.closed rejects abort-reason rejects cancel-reason rejects abort-reason
pending read() rejects Error: cancel-reason rejects the raw string rejects Error: cancel-reason
writer.desiredSize 0 null 0
later write() TypeError: This WritableStream has been closed. rejected cancel-reason same as workerd

How

Workerd's compression streams are internal streams on the read side too:

  • A readable cancel() records its error on the writable's private state
    (cancelled) instead of erroring it. The write already dispatched still
    completes; the transformer compresses the chunk and drops the output (the
    readable is cancelled); a write dispatched later refuses with the cancel
    error and closes the stream like a failed push.
  • The transformer's cancel waits for the write in flight (inFlightSettled,
    allocated only for internal writables) before it drops the native zlib
    state, so the in-flight chunk is not transformed into a dead handle.
  • An abort after that cancel is an ordinary abort, and an internal writable
    that aborts ends in the closed shape (desiredSize 0, later
    write()/close() report the closed stream) rather than the errored one
    a JS sink gets.
  • __cancelStream rejects a pending read with Error(reason) (an Error
    reason keeps its class, message and own enumerable properties).
  • A read on an internal stream settles after the microtask drain
    (deferReads), so a cancel or abort in the same turn as the producing
    write wins over the chunk — measured on workerd, whose native read settles
    several microtasks after the write: sync, t1, write-resolved, t2..t5, read-resolved.

deferReads and the per-write promise are opt-in flags, so a JS stream pays
nothing and only compression reads change timing.

Verification

  • Fail-before with the same fixture on a clean build of origin/main
    (6675ae7):

    compression-cancel-then-abort ... DIVERGES
    0.body: workerd={"...","abort":"ok","closed":{"name":"abort-reason",...},
      "pull":{"name":"Error","message":"cancel-reason"},"desiredSize":0,...}
        cello={"...","abort":{"name":"cancel-reason",...},
      "closed":{"name":"cancel-reason",...},"pull":{"name":"cancel-reason",...},
      "desiredSize":null,...}
    
  • After: node scripts/workerd-diff/runner.mjs — all 223 cases agree
    (workerd 1.20261121.1), including the tightened
    compression-cancel-then-abort case, which now pins the reasons, the
    pending-read outcome, desiredSize, locked, the getWriter() refusal
    and the later-write message instead of only "rejected".

  • cargo test --profile ci --locked -p cello --test stream_edges_conformance
    (3 passed) — the Rust test asserts the exact class/message pairs and now
    asserts workerd's shape, not the old one.

  • cargo test --profile ci --locked -p cello --no-fail-fast,
    cargo clippy --profile ci --locked --all-targets -- -D warnings,
    cargo fmt --check, node scripts/check-docs.mjs: green.

Known limit

Workerd's internal read settles several microtasks after the write (measured:
5-6), so a consumer that fully awaits the write and then cancels still sees
the read rejected on workerd while Cello resolves it. Matching that would
mean a task hop on every compression read; the same-turn case the issue
reports is pinned instead.

Closes #252

- A readable cancel on a CompressionStream/DecompressionStream records its
  error on the internal writable instead of erroring it, so the write in
  flight completes, a later write refuses with the closed-stream error, and
  the writable ends in workerd's closed shape.
- An abort after that cancel is an ordinary abort: it resolves, writer.closed
  rejects with the abort reason, and the pending read rejects with an Error
  carrying the cancel reason.
- Reads on these internal streams settle after the microtask drain, so a
  same-turn cancel wins over the chunk the producing write just enqueued.
- The compression-cancel-then-abort gate case now pins the reasons, the
  pending-read outcome and the writer's desiredSize.

Closes #252
@coderabbitai

coderabbitai Bot commented Nov 24, 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 54 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: f13b9285-92d3-4ac4-a08e-ef34b88f4994
Commits

Reviewing files that changed from the base of the PR and between 6675ae7 and b0c1a6a.

Files selected for processing: 7

  • crates/cello/js/compression_streams.js
  • crates/cello/js/streams.js
  • crates/cello/js/writable_stream.js
  • crates/cello/tests/stream_edges_conformance.rs
  • docs/changelog.md
  • docs/reference/cloudflare-compat.md
  • 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

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: compression stream abort racing a reader cancel picks a different reason and pending-read outcome

1 participant