stream: fix TransformStream race on cancel with pending write#62040
Open
marcopiraccini wants to merge 2 commits intonodejs:mainfrom
Open
stream: fix TransformStream race on cancel with pending write#62040marcopiraccini wants to merge 2 commits intonodejs:mainfrom
marcopiraccini wants to merge 2 commits intonodejs:mainfrom
Conversation
Signed-off-by: marcopiraccini <marco.piraccini@gmail.com>
Signed-off-by: marcopiraccini <marco.piraccini@gmail.com>
Collaborator
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:
|
Collaborator
ShogunPanda
approved these changes
Mar 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.