-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Node process hangs during import('process')
if a sibling process reads from stdin
and stdin
is piped to a parent process
#56537
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
Comments
This has been causing me trouble in development with a process tree of:
This appears slightly similar to #10836 (which was just a single grandchild, but otherwise the same piping / inheriting tree). |
By not inheriting stdin, it will avoid a potential process hang Reference: nodejs/node#56537
I’ve investigated this issue in detail, and here are my findings: Node.js uses the DuplicateHandle API to create a separate handle for an existing pipe so it can be shared with a child process. In this particular case, there is a pipe between the main and middle processes, and this pipe is inherited by two grandchild processes. As a result, Node.js duplicates the pipe in both child processes. Since the first child process is created first, it accesses From what I understand, this creates a race condition because the pipe is already being read by the first child process. Furthermore, the documentation clarifies that only one reader can access a pipe at a time. A potential solution to this issue could be pausing the first child process after its work is done by calling I hope this explanation helps! |
Thanks for the information and detailed investigation! From your investigation, do you happen to know why |
Yes, for some reason, they work differently. However, in your case, if you want to use
|
Luckily I don't want to use stdin - in my real situation, I'm just forking a child running TypeScript and spawning an Electron Forge child as part of a build script, and Hopefully the hanging is a fixable bug though. Perfectly fine for the |
Uh oh!
There was an error while loading. Please reload this page.
Version
All versions, tested on v12 through v23.6.0
Platform
Windows 10 and 11
What steps will reproduce the bug?
Launching a process tree like the following causes
grandchild2
to hang (cannot attach Node inspector, no JS code executes) after callingimport('process')
(which never resolves, unless its siblinggrandchild1
exits):Example code:
main.js
middle.js
grandchild1.js
grandchild2.js
Example repo with the above code (plus a bit more logging): https://github.com/Jimbly/node-stdin-hang-bug-demo
Tested on (all fail):
Works fine on Linux, presumably all non-Windows platforms.
How often does it reproduce? Is there a required condition?
Always.
Additional information
Hang does not occur if any of these happen:
grandchild1
is launched beforegrandchild2
middle
'sstdin
is notpipe
d tomain
grandchild1
's orgrandchild2
'sstdin
is notinherit
grandchild1
exits (causesgrandchild2
to get unstuck)Hang still occurs if:
grandchild1
is anything that reads from stdio (e.g.cmd /c pause
)The hung process's (native) call stack is:
The text was updated successfully, but these errors were encountered: