TransformStream error during pipeTo crashes the process despite being caught (v26.4.0 regression)
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- JavaScript
- Sterne
- 122k
- Forks
- 37.3k
- Ø Merge
- 4 T. 2 Std.
- Gemergte PRs (30 T.)
- 283
Beschreibung
Version
v26.4.0, v26.5.0, v27.0.0-nightly202607157a11a9b2db
Platform
Darwin 25.5.0 arm64 (pure JS internals — unlikely to be platform-specific)
Subsystem
webstreams
What steps will reproduce the bug?
When a TransformStream in a pipeThrough() chain throws while an upstream chunk is still in flight, the process crashes with an uncaught exception — even though the pipeTo() promise rejects as expected and user code catches the error.
// repro.mjs
const source = new ReadableStream({
start(controller) {
controller.enqueue("a");
controller.enqueue("b");
controller.close();
}
});
const asyncPassThrough = new TransformStream({
async transform(chunk, controller) {
controller.enqueue(chunk);
}
});
const identity = new TransformStream();
const failingTransform = new TransformStream({
transform() {
throw new Error("boom");
}
});
try {
await source
.pipeThrough(asyncPassThrough)
.pipeThrough(identity)
.pipeThrough(failingTransform)
.pipeTo(new WritableStream());
} catch (error) {
console.log("caught: " + error.message);
}
Execute with: node repro.mjs
The shape is timing-sensitive: the second chunk must reach the failing stream after it has errored. At least two chunks are required, and removing either intermediate stage (or making asyncPassThrough's transform synchronous) changes the interleaving enough that the crash disappears.
How often does it reproduce? Is there a required condition?
Deterministic with the reproduction above.
Behavior across runtimes and versions:
| Runtime | Result |
|---|---|
| Node.js v24.13.0, v25.9.0, v26.0.0, v26.2.0, v26.3.1 | caught: boom, exit 0 |
| Node.js v26.4.0, v26.5.0, v27 nightly | caught: boom, then uncaught exception, exit 1 |
| Deno 2.9.3, Bun 1.3.14 | caught: boom, exit 0 |
What is the expected behavior? Why is that the expected behavior?
The error thrown by the transform should surface exactly once, as the rejection of the pipeTo() promise. Since that rejection is caught, the process should exit cleanly. This is the behavior of Node.js ≤ v26.3.1, Deno, Bun, and browsers.
What do you see instead?
The rejection is delivered and caught (caught: boom is printed), and then the process crashes anyway with a duplicate of the same error:
caught: boom
node:internal/process/promises:324
triggerUncaughtException(err, true /* fromPromise */);
^
Error: boom
at Object.transform (file:///…/repro.mjs:19:9)
at node:internal/webstreams/util:195:32
at transformStreamDefaultControllerPerformTransform (node:internal/webstreams/transformstream:531:18)
at transformStreamDefaultSinkWriteAlgorithm (node:internal/webstreams/transformstream:577:10)
at node:internal/webstreams/transformstream:367:16
at writableStreamDefaultControllerProcessWrite (node:internal/webstreams/writablestream:1138:5)
at writableStreamDefaultControllerAdvanceQueueIfNeeded (node:internal/webstreams/writablestream:1245:5)
at writableStreamDefaultControllerWrite (node:internal/webstreams/writablestream:1100:3)
at writableStreamDefaultWriterWrite (node:internal/webstreams/writablestream:991:3)
at node:internal/webstreams/readablestream:1733:33
Node.js v26.5.0
Additional information
Root cause. Bisecting releases points to v26.4.0, and specifically to 6af90267de (#63572, "stream: drop per-chunk Promise alloc in pipeTo"), which changed both pipeTo write sites from setPromiseHandled(state.currentWrite) — which attaches real reaction handlers — to the native markPromiseAsHandled(state.currentWrite) binding, which only sets the promise's [[PromiseIsHandled]] bit:
// lib/internal/webstreams/readablestream.js (PipeToReadableStreamReadRequest[kChunk])
queueMicrotask(() => {
this.state.currentWrite = writableStreamDefaultWriterWrite(this.writer, chunk);
markPromiseAsHandled(this.state.currentWrite);
this.promise.resolve(false);
});
When the chunk arrives after the destination has already errored, writableStreamDefaultWriterWrite takes an early-return path and returns PromiseReject(storedError) — a promise that is already rejected at creation. V8 enqueues the kPromiseRejectWithNoHandler notification at rejection time. Setting the handled bit afterwards does not emit a kPromiseHandlerAddedAfterReject event (no handler is actually attached), so nothing cancels the pending notification, and Node promotes it to an uncaught exception at the end of the tick. The previous setPromiseHandled attached a real .then reaction, which does emit the cancellation event, so marking "too late" was harmless.
The ordering dependency is easy to demonstrate with the binding directly:
$ node --expose-internals -e "
const { internalBinding } = require('internal/test/binding');
const { markPromiseAsHandled } = internalBinding('util');
const p = Promise.reject(new Error('rejected, then marked'));
markPromiseAsHandled(p);
" # crashes
$ node --expose-internals -e "
const { internalBinding } = require('internal/test/binding');
const { markPromiseAsHandled } = internalBinding('util');
const { promise, reject } = Promise.withResolvers();
markPromiseAsHandled(promise);
reject(new Error('marked, then rejected'));
" # exits cleanly
Both call sites changed by #63572 have the same exposure (the batched fast-path write in readableStreamPipeTo and the PipeToReadableStreamReadRequest[kChunk] slow path; the reproduction above hits the slow path). A fix could restore real handlers at these two sites, or keep the optimization for the common pending-promise case and fall back to attaching a handler when the returned write promise is already rejected.
Impact. Found via zip.js, where reading a ZipCrypto-encrypted entry with a wrong password now kills the whole process: the Invalid password error thrown inside a decryption TransformStream crashes Node even though the library's pipeTo rejection handling and the application's try/catch both work as intended. Any pipeThrough chain whose transform can throw mid-stream (validation, decryption, parsing) is affected, and there is no userland workaround short of a process-level unhandledRejection handler.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit lib/internal/webstreams/readablestream.js, konzentriere dich auf die beiden durch #63572 geänderten pipeTo-Schreibstellen und führe repro.mjs auf einer betroffenen Node-Version aus. Verfolge, wie bereits abgelehnte Schreib-Promises behandelt werden, und verifiziere anschließend, dass der Fehler nur über die abgefangene pipeTo-Ablehnung gemeldet wird und der Prozess sauber beendet wird; stelle die Abdeckung beider Schreibpfade sicher, falls bestehende Tests gefunden werden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- javascript, node.js
- Bereich
- api, backend
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Ruhig
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 52/100