nodejs / nodejs/node

Unhandled rejection event in web streams when stream error is piped

オープン
#50,707 コメント 6 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

web streams
主要言語
JavaScript
スター
122k
フォーク
37.4k
平均マージ
4日 3時間
マージ済み PR(30日)
272

説明

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(() => {});

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、提供された再現手順を実行し、streams/web pipeTo パスを調査します。宛先の readable が消費されたときに、その rejection がどのように伝播するかに注目してください。捕捉された stream エラーが unhandledRejection イベントをトリガーしなくなり、既存の捕捉されたエラーの動作が維持されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript
領域
backend
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
活発
明瞭さ
明確に書かれている
初心者へのやさしさ
68/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。