Repository navigation
child_process: deliver each stdio event after the nextTicks and promise jobs of the one before #43790
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
Closed
Closed
child_process: deliver each stdio event after the nextTicks and promise jobs of the one before #43790
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eed6476
node:child_process: deliver each stdio event after the ticks and prom…
robobun 66871ac
child_process: queue a stdio stream's 'close' before the destroy call…
robobun 7b9dfc1
Shorten the comments of the after-tick-drain change to one line each
robobun 82080bf
Use the shared FIFO for the after-tick-drain queue
robobun 5b45752
Merge branch 'main' into robobun/b94aed8d/child-stdio-end-after-data-…
robobun 451e832
child_process: stdio 'close' carries hadError, and tests for a consum…
robobun 421f791
test: say that the held-open rows are not checked on Windows
robobun 1bbaee0
test: keep the grandchild of the held-open rows alive under BUN_FEATU…
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // Node calls into JS once for each libuv event, and after each call it runs | ||
| // every process.nextTick callback and every promise job, until both queues | ||
| // are empty. So the next event of a handle (the next chunk, the EOF) never | ||
| // runs ahead of a tick or a promise job that a listener of the previous event | ||
| // queued, however long the chain of ticks and jobs is. | ||
| // https://github.com/nodejs/node/blob/v24.9.0/lib/internal/process/task_queues.js#L72-L109 | ||
| // | ||
| // A module that gets several of Node's events out of one native call gives | ||
| // each of them a drain of its own with `runAfterTickDrain`. The tick loop | ||
| // (processTicksAndRejections in builtins/ProcessObjectInternals.ts) runs one | ||
| // of these callbacks each time both queues are empty, where Node's loop calls | ||
| // processPromiseRejections(). That happens inside the drain that is running, | ||
| // so no other I/O callback runs in between. | ||
| // | ||
| // Only nextTicks and promise jobs run between two callbacks. Bun reports | ||
| // unhandled rejections after the whole drain, and Node reads a pipe once for | ||
| // each turn of the event loop, so there a setImmediate() of a listener also | ||
| // runs before the next event. A setImmediate() here would wait for that too, | ||
| // but fake timers replace it, and it costs a turn of the event loop for each | ||
| // chunk. | ||
|
robobun marked this conversation as resolved.
Resolved
|
||
| const asyncHooksTick = require("internal/async_hooks_tick"); | ||
|
|
||
| type AfterTickDrainCallback = { | ||
| callback: (arg: any) => void; | ||
| arg: unknown; | ||
| frame: import("../../node/async_hooks").Frame | undefined; | ||
| }; | ||
|
|
||
| // A queue: entries before `head` are done. Bounded by the number of callers, which each wait for their turn. | ||
| const entries: (AfterTickDrainCallback | undefined)[] = []; | ||
| let head = 0; | ||
|
|
||
| function runAfterTickDrain(callback: (arg: any) => void, arg?: unknown) { | ||
| let ensureTickLoop = asyncHooksTick.ensureTickLoop; | ||
| if (ensureTickLoop === undefined) { | ||
| // The first read of process.nextTick creates the tick queue. Fake timers read it before they replace it. | ||
| void process.nextTick; | ||
| ensureTickLoop = asyncHooksTick.ensureTickLoop; | ||
| // process.nextTick was overwritten before its first read: there is no tick loop to wait for. | ||
| if (ensureTickLoop === undefined) return process.nextTick(callback, arg); | ||
| } | ||
| ensureTickLoop(); | ||
| $arrayPush(entries, { callback, arg, frame: $getInternalField($asyncContext, 0) }); | ||
| } | ||
|
|
||
| // The tick loop calls this each time both queues are empty. False: nothing waited. | ||
| function runAfterTickDrainCallback() { | ||
| if (head === entries.length) return false; | ||
| const { callback, arg, frame } = entries[head]!; | ||
| entries[head++] = undefined; | ||
| if (head === entries.length) { | ||
| entries.length = 0; | ||
| head = 0; | ||
| } | ||
|
robobun marked this conversation as resolved.
Resolved
|
||
| const restore = $getInternalField($asyncContext, 0); | ||
| $putInternalField($asyncContext, 0, frame); | ||
| // No catch and no finally, as for a tick: JSNextTickQueue::drain reports a throw inside the | ||
| // callback's async context, puts the context back, and calls the tick loop again. | ||
|
robobun marked this conversation as resolved.
Resolved
|
||
| callback(arg); | ||
| $putInternalField($asyncContext, 0, restore); | ||
| return true; | ||
| } | ||
| asyncHooksTick.runAfterTickDrainCallback = runAfterTickDrainCallback; | ||
|
|
||
| export default { runAfterTickDrain }; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.