Skip to content

stream: prevent enqueue after cancel in Readable.toWeb()#64766

Open
kairosci wants to merge 1 commit into
nodejs:mainfrom
kairosci:fix/webstreams-readable-cancel-backpressure
Open

stream: prevent enqueue after cancel in Readable.toWeb()#64766
kairosci wants to merge 1 commit into
nodejs:mainfrom
kairosci:fix/webstreams-readable-cancel-backpressure

Conversation

@kairosci

Copy link
Copy Markdown

This pull request resolves an issue where converting a Node readable stream to a web stream using the toWeb method could result in an uncatchable exception when the stream is cancelled during a backpressure resume cycle. When the cancel method is invoked on the underlying source, the controller transitions to a closed state but the data listener remains attached to the source stream. Consequently, a scheduled flow tick may deliver an additional data event which triggers an invalid state error upon attempting to enqueue the chunk into the already closed controller. To fix this behavior structurally, a cancellation guard is added directly inside the data event listener to intercept and discard any incoming chunks once the stream has been marked as cancelled. A regression test is also included to ensure that disconnecting a pipeline under backpressure properly tears down the stream without throwing uncaught exceptions. Closes #64529

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. web streams labels Jul 26, 2026
@avivkeller avivkeller added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Jul 26, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jul 26, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. needs-ci PRs that need a full CI run. web streams

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Readable.toWeb(): uncaughtException (ERR_INVALID_STATE "Controller is already closed") when the stream is canceled during backpressure resume

3 participants