-
Notifications
You must be signed in to change notification settings - Fork 5.7k
fix(node): mirror webstreams Readable adapter cancel semantics #30526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…ms adapter Prevent uncaught TypeErrors when ReadableStream reader is canceled while the underlying Node.js stream is closing. Convert cancel() to async with proper Promise-based cleanup and add defensive error handling. - Make cancel() async and return Promise that resolves after destruction - Add isCanceled flag to prevent duplicate controller termination - Check isDestroyed() before processing to avoid invalid operations - Wrap controller operations in try-catch with debug logging - Remove data listener before destroy to prevent further callbacks - Add NODE_DEBUG=stream logging for terminated controller errors Fixes test-stream-readable-to-web-termination.js
- Add error handling for controller operations when stream is already closed - Prevent uncaught promise rejections during stream cancellation - Make cancel() async and properly wait for stream destruction - Add debug logging for swallowed errors in stream operations - Enable test-stream-readable-to-web-termination.js test Fixes parallel/test-stream-readable-to-web-termination.js by preventing race conditions between stream cancellation and controller operations.
@@ -543,8 +593,43 @@ export function newReadableStreamFromStreamReadable( | |||
streamReadable.resume(); | |||
}, | |||
|
|||
cancel(reason) { | |||
destroy(streamReadable, reason); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appears that Node.js, keeps track of wasCanceled
variable here and then later uses it to decide if the controller should be closed. Have you tried this approach? https://github.com/nodejs/node/blob/886e4b3b534a9f3ad2facbc99097419e06615900/lib/internal/webstreams/adapters.js#L499
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
addressed!
node:stream
tracking issue - parallel/test-stream-readable-to-web-termination.jsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
This PR was created by GitStart to address the requirements from this ticket: DENB-29802.
Fix WebStreams adapter race condition when canceling during stream close
This PR fixes a race condition in the Node.js WebStreams adapter that causes uncaught TypeErrors when a ReadableStream's reader is canceled while the underlying
Node.js stream is closing.
Problem:
When reader.cancel() is called during the 'close' event of a Node.js stream (as in test-stream-readable-to-web-termination.js), the adapter would throw "Cannot
read properties of undefined" errors because the stream was being destroyed multiple times or the controller was in an invalid state.
Solution:
- Convert cancel() method to async with proper Promise-based cleanup
- Add isCanceled flag to prevent the finished callback from trying to close an already-canceled controller
- Check isDestroyed() before attempting stream operations
- Add defensive try-catch blocks around all controller operations
- Include proper cleanup with event listener removal before destruction
- Add NODE_DEBUG=stream logging for debugging
Test Fixed:
- test-stream-readable-to-web-termination.js - Now passes without errors
The changes ensure graceful handling of the race condition between stream cancellation and closure events.