nodejs / nodejs/node

Unhandled rejection event in web streams when stream error is piped

Aperta
#50,707 6 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

web streams
Lingua principale
JavaScript
Stelle
122k
Fork
37.3k
Merge medio
4g 2h
PR unite (30g)
283

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo la riproduzione fornita e ispeziona il percorso streams/web pipeTo, concentrandoti su come viene propagato il suo rejection quando il readable di destinazione viene consumato. Il lavoro è completato quando l’errore dello stream intercettato non attiva più un evento unhandledRejection, mentre il comportamento esistente per gli errori intercettati rimane invariato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript
Ambito
backend
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Attiva
Chiarezza
Specificata chiaramente
Idoneità per principianti
68/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.