Skip to content

stream: do not detach error listener when stream is already destroyed - #67420

Open
rudy-07 wants to merge 1 commit into
nodejs:mainfrom
rudy-07:stream-async-iterator-unhandled-error
Open

rudy-07 wants to merge 1 commit into
nodejs:mainfrom
rudy-07:stream-async-iterator-unhandled-error

Conversation

@rudy-07

@rudy-07 rudy-07 commented Nov 11, 2026

Copy link
Copy Markdown

Description

When a Readable stream with autoDestroy: false is destroyed with an error before async iteration starts (such as an aborted signal or an explicit .destroy(err) call on a stream with an asynchronous _destroy), createAsyncIterator settles the error and calls finalize().

Because error was defined and autoDestroy was false, finalize() took the else branch and called cleanup(), removing the eos error listener. When _destroy finished asynchronously, emitErrorNT emitted an 'error' event on a stream with zero listeners, crashing the process with an unhandled 'error' event despite the consumer wrapping iteration in try / catch.

This change checks stream.destroyed in finalize(). When the stream is already destroyed, calling destroyer(stream, null) is a no-op that avoids prematurely detaching the error listener, allowing the pending error to settle cleanly without crashing.

Fixes: #67418

Checklist

  • Relevant tests included.
  • Commit message follows the commit message guidelines.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/streams

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. stream Issues and PRs related to Node.js streams. labels Nov 11, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

Caution

AgentScan found account activity patterns that may be consistent with automation. This is a heuristic, not proof that this pull request was opened by an agent or violates policy. AI-assisted contributions are permitted, but automated tooling must not open pull requests without advance approval, and contributors must personally understand, test, verify, and take responsibility for every submitted change. See the AgentScan analysis, AI use policy, and automation policy for additional context.

@MikeMcC399

This comment was marked as resolved.

@rudy-07
rudy-07 force-pushed the stream-async-iterator-unhandled-error branch from 31f099b to 194e407 Compare November 13, 2026 18:28
@rudy-07

rudy-07 commented Nov 13, 2026

Copy link
Copy Markdown
Author

my bad @MikeMcC399, rewrote it to stay within limit and force-pushed the branch.

@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.66%. Comparing base (50ce73f) to head (03a2cbc).
⚠️ Report is 51 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67420      +/-   ##
==========================================
- Coverage   92.61%   90.66%   -1.96%     
==========================================
  Files         431      799     +368     
  Lines      197244   280246   +83002     
  Branches    30081    54129   +24048     
==========================================
+ Hits       182684   254083   +71399     
- Misses      14233    16284    +2051     
- Partials      327     9879    +9552     
Files with missing lines Coverage Δ
lib/internal/streams/readable.js 98.93% <100.00%> (+13.02%) ⬆️

... and 525 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@MikeMcC399

This comment was marked as resolved.

@rudy-07
rudy-07 force-pushed the stream-async-iterator-unhandled-error branch from 194e407 to fbc9eb7 Compare November 14, 2026 20:24
When an async iterator runs on a Readable stream with
autoDestroy: false that was destroyed before iteration started,
finalize() took the detach branch because autoDestroy was false.
This called cleanup(), removing the eos error listener before
the stream's asynchronous _destroy completed. When _destroy
subsequently finished, emitErrorNT emitted an unhandled 'error'
event on a stream with no listeners, crashing the process even
though the iteration error was caught by the consumer.

Allow already-destroyed streams to take the finalize path
without prematurely detaching the error listener.

Fixes: nodejs#67418
Signed-off-by: rudy-07 <kanhaiffco2007@gmail.com>
@rudy-07
rudy-07 force-pushed the stream-async-iterator-unhandled-error branch from fbc9eb7 to 03a2cbc Compare November 15, 2026 08:23
@rudy-07

rudy-07 commented Nov 15, 2026

Copy link
Copy Markdown
Author

@MikeMcC399 fixed the lint issues, all CIs are passing now.

@MikeMcC399

Copy link
Copy Markdown
Member

fixed the lint issues, all CIs are passing now.

The pull request will need a new review and then it will require a successful Jenkins CI run.

@jasnell
jasnell requested a review from ronag November 15, 2026 23:56
@MikeMcC399 MikeMcC399 added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 16, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 16, 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

agentscan:automated-account needs-ci PRs that need a full CI run. stream Issues and PRs related to Node.js streams.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stream: async iterator detaches its 'error' listener before a pending error is emitted (autoDestroy: false), causing an unhandled 'error'

5 participants