Skip to content

fix: close a compression writer across a readable cancel like workerd (#262) - #264

Merged
ryuzdev merged 2 commits into
mainfrom
fix/close-cancel
Nov 24, 2026
Merged

ryuzdev merged 2 commits into
mainfrom
fix/close-cancel

Conversation

@ryuzdev

@ryuzdev ryuzdev commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Closes #262

writer.close() around a same-turn readable cancel on a CompressionStream / DecompressionStream rejected where workerd resolves, for two reasons the issue names (both reproduced on main, gzip and deflate):

shape before workerd
write + close() + cancel() in one turn close/closed reject TypeError: zlib stream already closed both resolve
close() + cancel(), no write close/closed reject TypeError: Cannot enqueue a closed/errored stream both resolve

Probes (workerd 1.20261121.1, via compression-close-cancel-*)

Both orderings plus a cancel-then-close control, over gzip and deflate, recording close, closed, the pending read, desiredSize, a later write() and a later close()/closed. Workerd's shape for every mode is write: ok, close: ok, closed: ok, cancel: ok, pull: Error("cancel-reason"), desiredSize: 0, locked: true, later write/close TypeError: This WritableStream has been closed., closedAfter: ok. Cello matched only the cancel-first control before this change.

Change

  • __maybeCancelDrain waits for a close that is queued or in flight (__closing), and __finishClose runs the drain once that close has ended the compressor with its flush. The cancel no longer releases the native id the flush is about to use.
  • The readable cancel records wst.cancelled before the flushing bail-out, so a flush already running drops its output instead of enqueueing into the cancelled readable.
  • The transform's sink close skips rsc.close() when the readable was cancelled: the readable side closed itself, and closing it again was the Cannot enqueue a closed/errored stream rejection.

Nothing is released twice and no round leaks: close_across_cancel_releases_every_round runs 300 rounds × 3 modes with every CompressionStream wrapper still held, so a leak would hit the 128-stream cap's RangeError long before the end; live() is 0 afterwards. Verified fail-before by reverting writable_stream.js (the route reports the issue's two messages).

Verification on this head

  • Gate: node scripts/workerd-diff/runner.mjs — all 239 cases agree, including four new ones (compression-close-cancel-queued|inflight|no-write|cancel-first); the three regression cases fail on main.
  • cargo test --profile ci --locked -p cello --no-fail-fast — 896 passed, 0 failed.
  • cargo clippy --profile ci --locked --all-targets -- -D warnings — clean.
  • cargo fmt --check — clean.
  • node scripts/check-docs.mjs — clean.

Docs: docs/changelog.md (## Unreleased) and the compression paragraph in docs/reference/cloudflare-compat.md.

Summary by CodeRabbit

  • Bug Fixes
    • Resolved a race between closing a gzip or deflate stream and cancelling its readable side. A queued or in-progress close now completes successfully, including when no data was written first.
    • Close output is discarded if the readable side has been cancelled, and the compressor is released after the close settles.
  • Documentation
    • Clarified compression-stream behavior when close and readable cancellation overlap.

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

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 15 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: 9f7975e0-92e1-41c0-b578-d973f011ae34
Commits

Reviewing files that changed from the base of the PR and between 1e2774a and c11012c.

Files selected for processing: 6

  • crates/cello/js/writable_stream.js
  • crates/cello/js/zlib.rs
  • docs/changelog.md
  • docs/reference/cloudflare-compat.md
  • scripts/workerd-diff/cases.mjs
  • scripts/workerd-diff/corpus/streams/index.js

No actionable comments were generated in the recent review.

Walkthrough

Compression-stream close and readable cancellation now coordinate during queued or in-flight flushes. Regression tests and differential cases cover close and cancel orderings for gzip and deflate. The changelog and compatibility reference describe the resulting behavior.

Changes: Compression stream close and cancel

Layer / File(s) Summary
Coordinate cancellation with close and flush
crates/cello/js/writable_stream.js
Readable cancellation is recorded before the active-flush check. The compressor drain waits for queued or in-flight close completion, and flush skips closing an already-cancelled readable.
Verify and document close and cancel orderings
scripts/workerd-diff/corpus/streams/index.js, scripts/workerd-diff/cases.mjs, crates/cello/js/zlib.rs, docs/changelog.md, docs/reference/cloudflare-compat.md
Differential cases cover queued, in-flight, no-write, and cancel-first orderings for gzip and deflate. A 300-round regression test checks promise rejections, retained wrappers, and live native streams. The documentation describes the close and cancellation behavior.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Low

Possibly related PRs

  • ryuzcorp/cello#258: Changes compressor release on readable cancellation, which is the cancellation path extended here to account for queued or in-flight close.
  • ryuzcorp/cello#135: Changes the WritableStream._close outcome after stream errors, which is also part of the close path changed here.
Merge Risk: Minimal · previous review 1e277

This change makes same-turn readable cancellation around compression-stream close behave like workerd, so close() and writer.closed resolve instead of rejecting. The inspected close and cancel orderings do not show a reachable path that releases the compressor too early or leaks native streams, and regression and differential tests cover the cases. No merge-blocking risk remains.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Inconclusive Issue #262 has two coding requirements. (1) write + close() + cancel() in one turn must resolve close and closed. The issue asks to wait for a queued or in-flight close before the cancel releases the … Make the changed blobs for crates/cello/js/writable_stream.js available at the reviewed head and the merge base, or provide a narrower readable window covering __maybeCancelDrain, __finishClose, the readable cancel handler, the flushing bai…
Passed Passed checks: 3 passed

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: a compression writer is now closed across a same-turn readable cancel, matching workerd behavior. It also cites #262, which the linked issue tracks. The changeset (wri…
Out of Scope Changes check Passed Every changed area in the summary serves #262. The writable_stream.js change addresses the two causes the issue names. The zlib.rs change adds a regression test that runs close and cancel rounds and c…
Full details: Title check

Explanation

The title names the main change: a compression writer is now closed across a same-turn readable cancel, matching workerd behavior. It also cites #262, which the linked issue tracks. The changeset (writable_stream.js, zlib tests, changelog, workerd-diff cases) covers this exact behavior, so the title is fully related to the main change.

Full details: Out of Scope Changes check

Explanation

Every changed area in the summary serves #262. The writable_stream.js change addresses the two causes the issue names. The zlib.rs change adds a regression test that runs close and cancel rounds and checks for leaked native streams, which supports the no-leak claim in the issue. The changelog and cloudflare-compat.md entries document the behavior. The scripts/workerd-diff cases and corpus route add differential coverage for the four orderings the issue describes. No change in the summary covers unrelated behavior. This assessment rests on the summary. The source diff could not be read, so it does not confirm that no unrelated lines exist inside writable_stream.js.

Full details: Linked Issues check

Explanation

Issue #262 has two coding requirements. (1) write + close() + cancel() in one turn must resolve close and closed. The issue asks to wait for a queued or in-flight close before the cancel releases the compressor, or to make flush tolerate a released id. (2) close() + cancel() with no write must resolve close and closed. The issue asks to record wst.cancelled even while flushing and to skip enqueue into the cancelled readable. The PR summary and description say both are implemented in crates/cello/js/writable_stream.js (__maybeCancelDrain waits on __closing, __finishClose runs the drain, the cancel records cancelled before the flushing bail-out, and the transform sink skips rsc.close() when cancelled). The regression test close_across_cancel_releases_every_round and four workerd-diff cases are also described. I could not verify these code changes. The whole-PR diff for writable_stream.js returned no content because changed blobs are missing from the checkout. The file read at head was truncated at 16000 bytes before the cancel, drain, and close functions. The author-reported gate results cannot substitute for source evidence.

Resolution

Make the changed blobs for crates/cello/js/writable_stream.js available at the reviewed head and the merge base, or provide a narrower readable window covering __maybeCancelDrain, __finishClose, the readable cancel handler, the flushing bail-out, and the transform sink close. Confirm that the code matches the two requirements above. Also confirm that the new close_across_cancel_releases_every_round test exists at head in crates/cello/js/zlib.rs.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • 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

Recent review info

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: 053617b2-c65b-4aa0-a37f-d98d5718d5bd
Commits

Reviewing files that changed from the base of the PR and between 9b8a82a and 1e2774a.

Files selected for processing: 6

  • crates/cello/js/writable_stream.js
  • crates/cello/js/zlib.rs
  • docs/changelog.md
  • docs/reference/cloudflare-compat.md
  • scripts/workerd-diff/cases.mjs
  • scripts/workerd-diff/corpus/streams/index.js

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

…#262)

- A readable cancel no longer releases the compressor ahead of a close
  that is queued or in flight: the drain waits for the flush, which ends
  the native stream itself, so close() and closed resolve instead of
  rejecting with `zlib stream already closed`.
- The cancel records itself before the `flushing` bail-out, so a flush
  that is already running drops its output instead of enqueueing into
  the cancelled readable, and it no longer closes that side a second
  time (`Cannot enqueue a closed/errored stream`).
- The differential gate gained four cases (queued, in-flight, no write,
  and cancel before close) over gzip and deflate, all failing before
  this change; a 300-round held-wrapper probe keeps every round inside
  the 128-stream cap.
@ryuzdev
ryuzdev merged commit 00ce38c into main Nov 24, 2026
9 checks passed
@ryuzdev
ryuzdev deleted the fix/close-cancel branch November 24, 2026 16:53
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: writer.close() around a same-turn readable cancel on a compression stream rejects

1 participant