Skip to content

fix: compression output per push and accepted writes across a cancel (#257) - #258

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

ryuzdev merged 1 commit into
mainfrom
fix/compression-residuals3

Conversation

@ryuzdev

@ryuzdev ryuzdev commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

What

Issue #257, both residuals of #252/#255.

1. A write accepted before a same-turn cancel completes.

reader.read(); writer.write(x); reader.cancel(y); writer.abort(y) with no
await in between: the write is still queued behind the writable's start
gate when the cancel runs. Before, the cancelled guard rejected it with
Error: cancel-reason and closed reported that instead of the abort
reason. Workerd completes a write it accepted, so the transform still runs:

workerd before after
valid member write resolves Error: cancel-reason resolves
valid member closed abort-reason Error: cancel-reason abort-reason
invalid member write TypeError: Decompression failed. Error: cancel-reason TypeError: Decompression failed.
invalid member closed TypeError: Decompression failed. Error: cancel-reason same

The cancel marks the writable state (cancelled) and keeps the compressor
alive only as long as an accepted write needs it: the native stream is
released at cancel time when no write is pending, and otherwise by a drain
that runs the moment the last accepted write settles. An accepted write's
push still runs, so its failure is what the stream keeps; only the output
is dropped, because the readable is gone. A write accepted after the
cancel still refuses with the cancel error (the queue entry records the
marker at accept time).

That keeps the 128-stream cap honest in both directions: 300 cancel-only
rounds still construct (the state frees immediately), and a
cancel-behind-a-pending-write frees only after the write runs. Unit tests
cap_refuses_the_stream_past_it and
cancel_releases_after_the_accepted_write pin both, and the cap message no
longer suggests that a cancel frees a slot.

2. Encoders emit per push.

__zlib_stream_push returned nothing until 32 KiB had accumulated. Probed
on workerd 1.20261121.1 with writes of 3, 3, 1000 and 40000 bytes then
close():

format workerd chunks cello before cello after
gzip [10, 65] [75] [10, 65]
deflate [2, 61] [63] [2, 61]
deflate-raw [57] [57] [57]
brotli [13] [13] [13]

So workerd does not sync-flush: it enqueues whatever the compressor emitted
per push — gzip's 10-byte header and deflate's 2-byte zlib header on the
first write, nothing from deflate-raw/brotli until close. Dropping the
32 KiB hold reproduces exactly that, and the concatenation is unchanged
(compression-encoder-bytes still agrees, byte for byte).

Throughput (16 MiB through CompressionStream("gzip"), external timing, 5 runs after a warm-up, medians)

shape before after
256 × 64 KiB writes 243 / 242 ms 240 / 241 ms
16384 × 1 KiB writes 43 / 42 ms 41 / 42 ms

No per-chunk cost regression: std::mem::take on the sink swaps an empty
Vec in, no allocation, and the same bytes are produced either way.

Verification

  • Fail-before on a clean build of origin/main (fabf238):

    compression-queued-write-cancel ... DIVERGES
    0.body: workerd={"valid":{"write":"ok",...,"closed":{"name":"abort-reason"},
        ...},"invalid":{"write":{"name":"TypeError","message":"Decompression
        failed."},...}}
        cello={"valid":{"write":{"name":"Error","message":"cancel-reason"},...
    compression-encoder-chunks ... DIVERGES
    0.body: workerd={"gzip":{"chunks":[{"len":10,...},{"len":65,...}],...}}
        cello={"gzip":{"chunks":[{"len":75,...}],...}}
    
  • After: node scripts/workerd-diff/runner.mjs — all 231 cases agree.

  • cargo test --profile ci --locked -p cello --no-fail-fast: every target
    ok (66 result lines), including the 5 stream_edges_conformance routes
    and the updated js::zlib cap/release unit tests.

  • cargo clippy --profile ci --locked --all-targets -- -D warnings,
    cargo fmt --check, node scripts/check-docs.mjs: green.

Not attempted

The await-the-write-then-cancel timing (workerd settles its internal read
5–6 microtasks after the write) stays out of scope, as the issue says: a
task hop per read would overshoot workerd and cap throughput. The
cloudflare-compat.md entry documents it.

Closes #257

Summary by CodeRabbit

  • New Features
    • Compression streams now emit output as writes are processed, with chunk timing varying by compression format.
  • Bug Fixes
    • Writes accepted before readable-side cancellation can finish processing; writes accepted afterward fail with the cancellation error.
    • Compressor resources are released after pending writes settle, or immediately when no write is pending. Failed writes retain their original error, and subsequent writes report that the stream is closed.

@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

Walkthrough

Compression streams now process writes accepted before readable cancellation and defer native resource release until those writes settle. Encoder pushes return emitted output without waiting for a 32 KiB threshold. New tests cover cancellation outcomes and output chunking across compression formats.

Changes: Queued-write cancellation

Layer / File(s) Summary
Accepted writes and deferred release
crates/cello/js/compression_streams.js, crates/cello/js/writable_stream.js, crates/cello/js/zlib.rs, crates/cello/tests/stream_edges_conformance.rs, scripts/workerd-diff/corpus/streams/index.js, docs/reference/cloudflare-compat.md
Queue entries retain the cancellation error present when each write was accepted. Writes accepted before cancellation proceed, and native state is released after those writes settle. Tests cover valid and invalid input, cancellation, abort, and stream state.

Per-push encoder output

Layer / File(s) Summary
Return output emitted by each push
crates/cello/js/zlib.rs, crates/cello/tests/stream_edges_conformance.rs, scripts/workerd-diff/cases.mjs, scripts/workerd-diff/corpus/streams/index.js, docs/changelog.md, docs/reference/cloudflare-compat.md
ZlibStream::push returns bytes emitted during each push instead of holding output until a 32 KiB threshold. Corpus and conformance checks cover chunk lengths across gzip, deflate, deflate-raw, and Brotli.

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

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant ReadableStream
  participant WritableStreamState
  participant ZlibStream
  ReadableStream->>WritableStreamState: cancel and record cancellation error
  WritableStreamState->>ZlibStream: run writes accepted before cancellation
  ZlibStream-->>WritableStreamState: settle accepted writes
  WritableStreamState->>ZlibStream: release after the queue drains
Loading

Possibly related issues

  • Issue 262 describes a same-turn write, close, and readable cancel on a compression stream. This PR changes accepted-write handling on the cancel path, but does not show a fix for the reported close/closed rejection.
  • Issue 252 describes a pending read, write, readable cancel, and writable abort. This PR changes accepted-write outcomes in a related sequence, but does not show a fix for the reported abort-reason mismatch.

Possibly related PRs

  • ryuzcorp/cello#106: Both PRs change the release lifecycle of native compression state.
  • ryuzcorp/cello#247: Both PRs change writable queue behavior and compression encoder output.
  • ryuzcorp/cello#105: Both PRs define outcomes for pending writes during readable cancellation.

Suggested reviewers: ry

Merge Risk: Moderate · up to cfc99

Compression streams now handle cancellation and per-push output more like Workerd. Two edge cases remain: releasing the writer lock after a same-turn cancel can leave the native compressor allocated, and a queued close after cancel can fail. Several doc statements about post-cancel errors and deflate-raw timing are also inaccurate. These are narrow, but they should be fixed before merging.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

Biome (2.6.5)

crates/cello/js/compression_streams.js

File contains syntax errors that prevent linting: Line 17: Illegal return statement outside of a function

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

- An encoder now returns whatever it emitted for each push instead of
  holding output until 32 KiB or close, matching workerd's chunk
  boundaries: gzip's 10-byte header on the first write, deflate's 2-byte
  zlib header, and nothing from deflate-raw or brotli before close. The
  concatenation is unchanged byte for byte.
- A readable cancel no longer refuses a write the internal writable had
  already accepted: the transform still runs (so a failed push is the
  error the stream keeps) and only its output is dropped. A write
  accepted after the cancel still refuses with the cancel error.
- The compressor is released at cancel time, or as soon as the writes the
  cancel arrived behind have settled, so a bare cancel never holds a slot
  in the 128-stream cap while an accepted write still sees the
  compressor. New unit tests pin both.
- New gate cases compression-queued-write-cancel and
  compression-encoder-chunks, plus their Rust conformance routes.

Closes #257
@ryuzdev
ryuzdev force-pushed the fix/compression-residuals3 branch from 2ad0ed1 to cfc9953 Compare November 24, 2026 15:27
@ryuzdev
ryuzdev merged commit 9b8a82a into main Nov 24, 2026
8 of 9 checks passed
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 residuals (write queued before a same-turn cancel; encoder emits output only at 32 KiB/close)

1 participant