Skip to content

Conversation

gitstart-app[bot]
Copy link
Contributor

@gitstart-app gitstart-app bot commented Aug 26, 2025

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.

gitstart-app[bot] added 2 commits August 26, 2025 05:57
…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);
Copy link
Member

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed!

@gitstart-app gitstart-app bot changed the title DEN-29802 - ☂️ node:stream tracking issue - parallel/test-stream-readable-to-web-termination.js fix(node): mirror webstreams Readable adapter cancel semantics Aug 28, 2025
Copy link
Member

@bartlomieju bartlomieju left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@bartlomieju bartlomieju enabled auto-merge (squash) September 1, 2025 08:50
@bartlomieju bartlomieju merged commit c1f873d into main Sep 1, 2025
19 checks passed
@bartlomieju bartlomieju deleted the DENB-29802-9 branch September 1, 2025 09:29
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.

2 participants