nodejs / nodejs/node

Unhandled rejection event in web streams when stream error is piped

Abierto
#50,707 6 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

web streams
Lenguaje dominante
JavaScript
Estrellas
122k
Forks
37.4k
Merge medio
4 d 3 h
PR fusionados (30 d)
272

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza ejecutando la reproducción proporcionada e inspecciona la ruta streams/web pipeTo, centrándote en cómo se propaga su rejection cuando se consume el readable de destino. El trabajo estará terminado cuando el error del stream capturado ya no desencadene un evento unhandledRejection, mientras el comportamiento existente para errores capturados permanezca intacto.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
javascript
Área
backend
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Activo
Claridad
Bien especificado
Aptitud para principiantes
68/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.