stream: fix TransformStream race on cancel with pending write#62040
stream: fix TransformStream race on cancel with pending write#62040marcopiraccini wants to merge 2 commits intonodejs:mainfrom
Conversation
Signed-off-by: marcopiraccini <marco.piraccini@gmail.com>
Signed-off-by: marcopiraccini <marco.piraccini@gmail.com>
Codecov Reportβ
All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #62040 +/- ##
==========================================
- Coverage 89.65% 89.62% -0.03%
==========================================
Files 676 676
Lines 206231 206244 +13
Branches 39505 39502 -3
==========================================
- Hits 184898 184851 -47
- Misses 13463 13518 +55
- Partials 7870 7875 +5
π New features to boost your workflow:
|
Commit Queue failed- Loading data for nodejs/node/pull/62040 β Done loading data for nodejs/node/pull/62040 ----------------------------------- PR info ------------------------------------ Title stream: fix TransformStream race on cancel with pending write (#62040) Author Marco <marco.piraccini@gmail.com> (@marcopiraccini) Branch marcopiraccini:fix-transform-stream-race -> nodejs:main Labels author ready, web streams Commits 2 - stream: fix TransformStream race on cancel with pending write - linting fixup Committers 1 - marcopiraccini <marco.piraccini@gmail.com> PR-URL: https://github.com/nodejs/node/pull/62040 Fixes: https://github.com/nodejs/node/issues/62036 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> ------------------------------ Generated metadata ------------------------------ PR-URL: https://github.com/nodejs/node/pull/62040 Fixes: https://github.com/nodejs/node/issues/62036 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it> -------------------------------------------------------------------------------- βΉ This PR was created on Sat, 28 Feb 2026 13:15:07 GMT β Approvals: 2 β - Matteo Collina (@mcollina) (TSC): https://github.com/nodejs/node/pull/62040#pullrequestreview-3870446976 β - Paolo Insogna (@ShogunPanda) (TSC): https://github.com/nodejs/node/pull/62040#pullrequestreview-3871584746 β Last GitHub CI successful βΉ Last Full PR CI on 2026-02-28T21:40:44Z: https://ci.nodejs.org/job/node-test-pull-request/71504/ - Querying data for job/node-test-pull-request/71504/ β Build data downloaded β Last Jenkins CI successful -------------------------------------------------------------------------------- β No git cherry-pick in progress β No git am in progress β No git rebase in progress -------------------------------------------------------------------------------- - Bringing origin/main up to date... From https://github.com/nodejs/node * branch main -> FETCH_HEAD β origin/main is now up-to-date - Downloading patch for 62040 From https://github.com/nodejs/node * branch refs/pull/62040/merge -> FETCH_HEAD β Fetched commits as a6e9e3217833..398e70014ef6 -------------------------------------------------------------------------------- [main b5306f06be] stream: fix TransformStream race on cancel with pending write Author: marcopiraccini <marco.piraccini@gmail.com> Date: Sat Feb 28 14:09:57 2026 +0100 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-whatwg-transformstream-cancel-write-race.js [main bb247edd45] linting fixup Author: marcopiraccini <marco.piraccini@gmail.com> Date: Sat Feb 28 14:23:30 2026 +0100 1 file changed, 2 insertions(+), 1 deletion(-) β Patches applied There are 2 commits in the PR. Attempting autorebase. (node:368) [DEP0190] DeprecationWarning: Passing args to a child process with shell option true can lead to security vulnerabilities, as the arguments are not escaped, only concatenated. (Use `node --trace-deprecation ...` to show where the warning was created) Rebasing (2/4) Executing: git node land --amend --yes --------------------------------- New Message ---------------------------------- stream: fix TransformStream race on cancel with pending writehttps://github.com/nodejs/node/actions/runs/22577662568 |
Fixes: #62036
TransformStreamwhere a latewriter.write()racing withreader.cancel()could throw an internalTypeError: controller[kState].transformAlgorithm is not a functioncancel/abort/closeclears the controller's algorithms viatransformStreamDefaultControllerClearAlgorithmswhile a pending write reachestransformStreamDefaultControllerPerformTransformtransformStreamDefaultControllerPerformTransformto check iftransformAlgorithmis still callable before invoking itRace sequence
reader.read()releases backpressurereader.cancel()triggerstransformStreamDefaultSourceCancelAlgorithm, which callstransformStreamDefaultControllerClearAlgorithmsβ setstransformAlgorithm = undefinedwriter.write()enterstransformStreamDefaultSinkWriteAlgorithmand callstransformStreamDefaultControllerPerformTransformperformTransformtries to invoketransformAlgorithm()which is nowundefinedβ TypeErrorFix
Guard
transformStreamDefaultControllerPerformTransformso that iftransformAlgorithmhas been cleared (stream is shutting down), the write returns silently rather than throwing an internal error. The writable side's existing error handling surfaces the appropriate stream-level error to the caller.Spec note
The WHATWG streams reference implementation has the same unguarded call in
TransformStreamDefaultControllerPerformTransform. The race is latent in the spec but rarely surfaces in browsers due to WebIDL callback wrapping adding an extra promise tick that changes the interleaving order (see whatwg/streams#1296). Node.js hits it consistently because it lacks that extra indirection layer.Test plan
test/parallel/test-whatwg-transformstream-cancel-write-race.jsreproduces the race.