child_process: fix stdin close event when child closes fd 0#64626
Open
mhayk wants to merge 1 commit into
Open
Conversation
Fixes nodejs#25131 When a child process closes its stdin file descriptor (fd 0), the parent process should receive a 'close' event on the stdin socket. This was working in v8.11.4 but broke in PR nodejs#18701. The root cause: PR nodejs#18701 explicitly set `readable: true` when creating child stdio sockets, which prevented net.Socket from calling `read(0)`. Without `read(0)`, libuv doesn't listen for EOF/HUP events from the child closing fd 0, so the 'close' event is never emitted. Solution: For stdin specifically (index 0), create the socket without explicitly setting the `readable` option. This allows net.Socket to call `read(0)` in its constructor. After the socket is created, manually set `readable = false` and `writable = true` to maintain the correct semantic properties of stdin. For all other stdio sockets (stdout, stderr, and custom pipes), the original behavior is preserved. Reviewed-By: Mhayk Whandson <hi@mhayk.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #25131
Problem
When a child process closes its stdin file descriptor (fd 0), the parent process should receive a 'close' event on the stdin socket. This was working in v8.11.4 but broke in PR #18701.
Root Cause
PR #18701 explicitly set
readable: truewhen creating child stdio sockets, which prevented net.Socket from callingread(0). Withoutread(0), libuv doesn't listen for EOF/HUP events from the child closing fd 0, so the 'close' event is never emitted.Solution
For stdin specifically (index 0), create the socket without explicitly setting the
readableoption. This allows net.Socket to callread(0)in its constructor. After socket creation, manually setreadable = falseandwritable = trueto maintain correct semantic properties.For all other stdio sockets (stdout, stderr, custom pipes), the original behavior is preserved.
Testing
test-child-process-stdin-close.jspasses ✅test-child-process-stdin.jspasses ✅