Skip to content

fix: TransformStream close after cancel rejects with the cancel reason (#129) - #135

Merged
ryuzdev merged 1 commit into
mainfrom
fix/workerd-diff-transform-close
Nov 23, 2026
Merged

ryuzdev merged 1 commit into
mainfrom
fix/workerd-diff-transform-close

Conversation

@ryuzdev

@ryuzdev ryuzdev commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

What

After reader.cancel("bye") on a TransformStream's readable, writer.close() rejected with TypeError: Stream already closed/errored instead of the cancel reason that writer.closed already reported. Workerd rejects with the stored error.

Root cause: WritableStream._close treated errored like closed and returned a fresh TypeError. It now rejects with stream._storedError, matching workerd; closed keeps its TypeError. The same path serves WritableStream.prototype.close() and the IdentityTransformStream/FixedLengthStream writers, whose readable cancel already errors the writable with workerd's converted Error.

The WHATWG text says a fresh TypeError for an errored stream, so this is recorded as the third deliberate workerd deviation in writable_stream.js.

Fail-before

Gate case transform-close-after-cancel, pre-fix binary:

transform-close-after-cancel ...
  workerd: [{"status":200,...,"body":"{\"close\":\"value: bye\",\"closed\":\"value: bye\"}","bytes":44}]
  cello:   [{"status":200,...,"body":"{\"close\":\"TypeError: Stream already closed/errored\",\"closed\":\"value: bye\"}","bytes":74}]
DIVERGES

After the fix the case is ok and the full gate is 36/36 agreeing.

Tests

  • Gate: node scripts/workerd-diff/runner.mjs — all 36 cases agree.
  • cargo test --profile ci --locked — green (one unrelated flake in d1_concurrency_conformance on one run, filed as test flake: tee-skew conformance can EPIPE its 40 MiB upload when the forwarded branch loses the skew budget #134; the re-run and the focused runs pass).
  • Focused: transform_backpressure_conformance (new check 15: close and closed reasons after cancel), js_tee_budget_conformance, response_stream_budget_conformance, virtual_clock_conformance — green.
  • cargo clippy --profile ci --all-targets --locked -- -D warnings, cargo fmt --check — clean.
  • Docs: npm run validate, npm run build — clean.

Out of scope, filed

Closes #129

Summary by CodeRabbit

  • Bug Fixes
    • Closing a stream after its readable side has been cancelled now rejects with the original cancellation reason instead of a generic TypeError.
    • Closing an already closed stream continues to reject with a TypeError.
  • Tests
    • Added coverage for cancellation and subsequent writer close behavior, including the rejection reason.

A readable cancel errors a TransformStream's writable with the cancel reason; writer.closed already reported it, but writer.close() took the errored early path and rejected with a generic TypeError. Close now rejects with the stream's stored error, as workerd does (the WHATWG text says a fresh TypeError). The same path covers WritableStream.prototype.close(), IdentityTransformStream and FixedLengthStream writes.

The differential gate gains transform-close-after-cancel, and the TransformStream conformance fixture checks the close and closed reasons.

Closes #129
@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

When WritableStream._close() encounters an errored stream, it now rejects with the stored error. Conformance tests and a differential case check that writer.close() rejects with the readable cancellation reason. Closing an already closed stream still rejects with a TypeError.

Changes: TransformStream close after readable cancellation

Layer / File(s) Summary
Stored-error rejection and conformance coverage
crates/cello/js/writable_stream.js, crates/cello/tests/fixtures/transform-backpressure/index.js, crates/cello/tests/transform_backpressure_conformance.rs, docs/changelog.md
WritableStream._close() now rejects with the stored error when the stream is errored. The conformance test checks that writer.close() and writer.closed reject with the cancellation reason. The closed-stream TypeError remains.
Differential case
scripts/workerd-diff/cases.mjs, scripts/workerd-diff/corpus/streams/index.js
The streams corpus adds /transform-close-after-cancel. The route cancels a TransformStream readable and reports the outcomes of writer.close() and writer.closed.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • ryuzcorp/cello#132 describes the closed-stream branch of writer.close(), which this change leaves unchanged.
  • ryuzcorp/cello#133 describes writer.abort() on an errored writable, a related but separate path.

Possibly related PRs

  • ryuzcorp/cello#105: Changes the TransformStream cancellation path that produces the errored-writable state covered here.
  • ryuzcorp/cello#112: Adds the differential harness extended by this change.
Merge Risk: Minimal · up to b9ddd

After a TransformStream readable is cancelled, writer.close() now rejects with the cancel reason instead of a generic TypeError, matching workerd. Closing an already closed stream behaves as before. The change is small, has added tests, and shows no merge-blocking risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the primary behavior change: after a TransformStream readable is cancelled, writer.close() rejects with the cancel reason instead of a generic TypeError. The raw summary confirms this …
Linked Issues check Passed Issue #129 is the only active direct link. Its requirement is that writer.close() after reader.cancel("bye") on a TransformStream reject with the cancel reason, matching writer.closed. At the reviewed…
Out of Scope Changes check Passed All changed files relate to the #129 fix. writable_stream.js changes the errored-close path and updates the deviation comment. The transform_backpressure_conformance fixture and Rust test doc add cove…
Full details: Title check

Explanation

The title names the primary behavior change: after a TransformStream readable is cancelled, writer.close() rejects with the cancel reason instead of a generic TypeError. The raw summary confirms this is the main change, with WritableStream._close, the conformance tests, the changelog, and the workerd-diff case all supporting it. The (#129) reference matches the linked issue. The title is specific and accurate.

Full details: Linked Issues check

Explanation

Issue #129 is the only active direct link. Its requirement is that writer.close() after reader.cancel("bye") on a TransformStream reject with the cancel reason, matching writer.closed. At the reviewed head, WritableStream._close in crates/cello/js/writable_stream.js returns Promise.reject(this._storedError) when _state === 'errored'. The closed state keeps its TypeError. A close requested while the stream is 'erroring' is queued in _closeRequest. _finishErroring then rejects it with _storedError, so the same reason is reached in that path too. The change covers the requirement in code. The fixture transform-backpressure/index.js adds a conformance check for close and closed reasons after cancel. The differential case transform-close-after-cancel is added in scripts/workerd-diff. The reported gate result (36/36 agreeing) and cargo test results are taken from the PR description and were not re-run here. The code path is verified by source reading. No coding requirement from #129 is unmet.

Full details: Out of Scope Changes check

Explanation

All changed files relate to the #129 fix. writable_stream.js changes the errored-close path and updates the deviation comment. The transform_backpressure_conformance fixture and Rust test doc add coverage for the new behavior. scripts/workerd-diff cases and corpus add the matching gate case. docs/changelog.md records the behavior change. The PR notes that IdentityTransformStream and FixedLengthStream reach the same _close path through the same cancel-to-error flow, so their behavior changes too. This is a direct effect of the shared code path, not unrelated work. Sibling issues #133, #132, and #134 are listed as out of scope and are not implemented here. The diff for abort() is not changed in this PR beyond the existing code, so no unrelated behavior change was found.

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: 05980bae-29de-49de-9fdf-e0e3c71ec430
Commits

Reviewing files that changed from the base of the PR and between e2aa7e8 and b9ddd4c.

Files selected for processing: 6

  • crates/cello/js/writable_stream.js
  • crates/cello/tests/fixtures/transform-backpressure/index.js
  • crates/cello/tests/transform_backpressure_conformance.rs
  • docs/changelog.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.

@ryuzdev
ryuzdev merged commit 8275a35 into main Nov 23, 2026
9 checks passed
@ryuzdev
ryuzdev deleted the fix/workerd-diff-transform-close branch November 23, 2026 08:22
@ryuzdev ryuzdev mentioned this pull request Nov 23, 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: TransformStream writer.close() after reader.cancel() rejects with a TypeError instead of the cancel reason

1 participant