nodejs / nodejs/node

Unhandled rejection event in web streams when stream error is piped

Open
#50,707 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

web streams
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

Version

21.1.0

Platform

Linux kunigunde 6.5.9-arch2-1 #1 SMP PREEMPT_DYNAMIC Thu, 26 Oct 2023 00:52:20 +0000 x86_64 GNU/Linux

Subsystem

streams/web

What steps will reproduce the bug?

Run the following code:

import { ReadableStream } from "stream/web";

process.on('unhandledRejection', (err) => {
	console.log(`Unhandled rejection: ${err.message}`);
});

const stream1 = new ReadableStream({
	start(controller) {
		controller.error(new Error('Test'));
	},
});

const stream2 = new TransformStream();

stream1.pipeTo(stream2.writable);

try {
	await stream2.readable.getReader().read();
} catch (err) {
	console.log(`Caught error: ${err.message}`);
}
How often does it reproduce? Is there a required condition?

With the given code example, it happens every time.

It does not happen in the following situations:

  • Reading from stream1 directly rather than piping it to stream2
  • Reading from stream2 but not catching the error but letting the app crash instead
What is the expected behavior? Why is that the expected behavior?

No unhandledRejection event should be triggered, since the exception is actually handled.

What do you see instead?

An unhandledRejection event is triggered. The output of the sample script is:

Caught error: Test
Unhandled rejection: Test
Workaround

I’m not sure if this has any unintended side effects, but catching the rejection by the promise returned by pipeTo() seems to solve the problem:

stream1.pipeTo(stream2.writable).catch(() => {});

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the supplied reproduction and inspect the streams/web pipeTo path, focusing on how its rejection is propagated when the destination readable is consumed. Done means the caught stream error no longer triggers an unhandledRejection event, while the existing caught-error behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.