Skip to content

fix: construct stream errors without new - #561

Open
joshbarros wants to merge 2 commits into
nodejs:mainfrom
joshbarros:fix/532-final-callback-without-new
Open

joshbarros wants to merge 2 commits into
nodejs:mainfrom
joshbarros:fix/532-final-callback-without-new

Conversation

@joshbarros

Copy link
Copy Markdown

_final calls ERR_MULTIPLE_CALLBACK() with no new. E() exports a class, so that call throws TypeError: Class constructor NodeError cannot be invoked without 'new' before the constructor body runs.

A second synchronous callback emits that TypeError on error, with no code. A second callback on nextTick throws outside the try around _final, and the process exits. write() already uses new and emits ERR_MULTIPLE_CALLBACK.

Node fixed the call site in nodejs/node#52110. This package is the Node 18.19.0 cut, so lib/internal/streams/writable.js stays that copy. The factory in ours/errors.js returns the same instance with or without new. instanceof, name, code and the message stay the same.

On Node v22.19.0, from ffeeda66:

  • A second synchronous _final callback emitted TypeError with no code. It now emits ERR_MULTIPLE_CALLBACK, matching node:stream.
  • A second _final callback on nextTick exited 1. It no longer throws. finish has already destroyed the stream, so no second error is emitted. node:stream on Node 22 does the same.
  • A second synchronous write callback still emits ERR_MULTIPLE_CALLBACK.

npm test passed 191 files. npx eslint on the touched src files was clean. npx prettier -c on the six files was clean.

Fixes #532

Node 18's vendored _final calls ERR_MULTIPLE_CALLBACK() with no new.
The class throws TypeError before the constructor body. A second
synchronous callback emits that TypeError. A second callback on
nextTick escapes as an uncaught exception. write() already uses new
and emits ERR_MULTIPLE_CALLBACK.

The factory returns the same instance with or without new. The
vendored writable.js stays the Node 18.19.0 copy.

Signed-off-by: Josue Barros <joshbarrosweb@gmail.com>

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@joshbarros

Copy link
Copy Markdown
Author

Hi! The new commit needs workflow approval before CI can run. Could you approve the runs when you have a moment? Thanks!

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.

Is ERR_MULTIPLE_CALLBACK missing a new?

2 participants