Repository navigation
node:stream: deliver child stdio 'data' listener throws as uncaughtException #37171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 8 commits
7bbe836
8844326
76da0ec
b8f5439
ac08cfd
00abd2a
1dcd323
29f89c6
1c0a867
ffbf23a
0a7670a
56155ec
9006a43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ const transferToNativeReadable = $newCppFunction( | |
| 1, | ||
| ); | ||
| const { errorOrDestroy } = require("internal/streams/destroy"); | ||
| const { reportUncaughtException } = require("internal/shared"); | ||
|
|
||
| const kRefCount = Symbol("refCount"); | ||
| const kCloseState = Symbol("closeState"); | ||
|
|
@@ -20,6 +21,8 @@ const kHighWaterMark = Symbol("highWaterMark"); | |
| const kPendingRead = Symbol("pendingRead"); | ||
| const kHasResized = Symbol("hasResized"); | ||
| const kRemainingChunk = Symbol("remainingChunk"); | ||
| const kUncaughtOnListenerThrow = Symbol("uncaughtOnListenerThrow"); | ||
| const kListenerError = Symbol("listenerError"); | ||
|
|
||
| const MIN_BUFFER_SIZE = 512; | ||
| let dynamicallyAdjustChunkSize = (_?) => ( | ||
|
|
@@ -49,6 +52,8 @@ interface NativeReadable extends NodeReadable { | |
| [kHighWaterMark]: number; | ||
| [kHasResized]: boolean; | ||
| [kRemainingChunk]: Buffer | undefined; | ||
| [kUncaughtOnListenerThrow]: boolean; | ||
| [kListenerError]: { error: unknown } | undefined; | ||
| debugId: number; | ||
| } | ||
|
|
||
|
|
@@ -65,7 +70,15 @@ interface NativePtr { | |
|
|
||
| let debugId = 0; | ||
|
|
||
| function constructNativeReadable(readableStream: ReadableStream, options): NativeReadable { | ||
| // `uncaughtOnListenerThrow`: a throw from a listener that `push()` runs (a | ||
| // 'data' listener) is reported as an uncaughtException, as Node does for the | ||
| // streams it feeds from a native read callback (child stdio). Otherwise the | ||
| // throw rejects the pending pull promise, as Node's Readable.fromWeb does. | ||
| function constructNativeReadable( | ||
| readableStream: ReadableStream, | ||
| options, | ||
| uncaughtOnListenerThrow: boolean = false, | ||
| ): NativeReadable { | ||
| $assert(typeof readableStream === "object" && readableStream instanceof ReadableStream, "Invalid readable stream"); | ||
| const bunNativePtr = (readableStream as any).$bunNativePtr; | ||
| $assert(typeof bunNativePtr === "object", "Invalid native ptr"); | ||
|
|
@@ -86,6 +99,8 @@ function constructNativeReadable(readableStream: ReadableStream, options): Nativ | |
| stream[kPendingRead] = false; | ||
| stream[kHasResized] = !dynamicallyAdjustChunkSize(); | ||
| stream[kCloseState] = [false]; | ||
| stream[kUncaughtOnListenerThrow] = uncaughtOnListenerThrow; | ||
| stream[kListenerError] = undefined; | ||
|
|
||
| const highWaterMark = options.highWaterMark; | ||
| stream[kHighWaterMark] = typeof highWaterMark === "number" ? highWaterMark : 256 * 1024; | ||
|
|
@@ -182,6 +197,11 @@ function read(this: NativeReadable, maxToRead: number) { | |
| ); | ||
| this[kPendingRead] = false; | ||
| this[kRemainingChunk] = handleResult(this, result, chunk, this[kCloseState][0]); | ||
| const thrown = this[kListenerError]; | ||
| if (thrown !== undefined) { | ||
| this[kListenerError] = undefined; | ||
| throw thrown.error; | ||
| } | ||
| }, | ||
| reason => { | ||
| errorOrDestroy(this, reason); | ||
|
|
@@ -222,12 +242,33 @@ function pushEof(stream: NativeReadable) { | |
| // the consumer paused); stop the native reader so kernel backpressure reaches | ||
| // the writer (readStop, like net.Socket). The next `_read()` re-enables it. | ||
| function pushAndCheck(stream: NativeReadable, chunk: any) { | ||
| if (!stream.push(chunk)) { | ||
| let wantMore: boolean; | ||
| try { | ||
| wantMore = stream.push(chunk); | ||
|
Comment on lines
+226
to
+227
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift Handle listener throws when buffered data flows. If a fast-writing child fills the readable buffer, 🤖 Prompt for AI Agents
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A 'data' listener throw from flow() (buffered data) is not this PR's bug. flow() runs from a nextTick, so that throw already reaches uncaughtException. After it, the Readable is left with flowing=true and a full buffer. Node's Readable leaves a net.Socket or child stdio stream in that same state after the same throw: the loop in flow() is abandoned and nothing reads again until the consumer calls read() or resume(). This PR fixes the path where bun differed from Node: the throw from the pending-pull reaction, which surfaced as unhandledRejection and stalled the stream.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Analysis chain
In a Node 24.22.1 check with a full buffer, the throw reached Learnings used
Review thread resolved. You are interacting with an AI system.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correction to my reply above: what it says about Node is wrong, and this finding stands. In Node the stream reads on after a listener throw on this path, because the handle pushes the next chunk by itself. Measured with Node v26.3.0: a child that writes 13107200 bytes without pause and a listener that throws on its first 3 calls ends with all 13107200 bytes and 3
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @robobun Thanks for the correction. I was wrong to withdraw the finding. My earlier Node check did not establish parity for the fast-writer case. Your reported reproduction shows that Node reads all 13,107,200 bytes after three listener throws, while this Bun head stops early. The finding remains open. A regression test should check repeated throws, Learnings used
You are interacting with an AI system. |
||
| } catch (e) { | ||
| wantMore = true; | ||
| // The throw unwound addChunk before maybeReadMore; keep the stream reading. | ||
| process.nextTick(readAfterListenerThrow, stream); | ||
|
claude[bot] marked this conversation as resolved.
Resolved
|
||
| if (stream[kUncaughtOnListenerThrow]) { | ||
| reportUncaughtException(e); | ||
| } else { | ||
| // Only the pending-pull reaction emits 'data' from inside push(): it | ||
| // rethrows this once the read's bookkeeping is done. | ||
|
robobun marked this conversation as resolved.
Resolved
|
||
| stream[kListenerError] = { error: e }; | ||
| } | ||
| } | ||
| if (!wantMore) { | ||
| const ptr = stream.$bunNativePtr; | ||
| if (ptr) ptr.setFlowing?.(false); | ||
| } | ||
| } | ||
|
|
||
| function readAfterListenerThrow(stream: NativeReadable) { | ||
| // A sync pull from the uncaughtException handler can have closed the stream; its EOF push is already queued. | ||
| if (stream.destroyed || stream[kCloseState][0]) return; | ||
|
claude[bot] marked this conversation as resolved.
Resolved
|
||
| stream.read(0); | ||
| } | ||
|
|
||
| function handleNumberResult(stream: NativeReadable, result: number, chunk: any, isClosed: boolean) { | ||
| if (result > 0) { | ||
| const slice = chunk.subarray(0, result); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.