-
Notifications
You must be signed in to change notification settings - Fork 84
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
Child process inherits all open file descriptors on linux #13
Comments
I just ran into this as well. It's causing issues because the parent process tries to restart an embedded HTTP server, but the restart fails because the child ends up taking control of the port the server was running on. |
@dano As @lfbayer noted, this is quite tricky. Because NuProcess does not use any native code (we use JNA), we don't have any control over the forked process. The process implementation in Java uses JNI to first fork the JVM process, then the child JVM process performs a close of all file descriptors except for the pipes to the parent, and then executes the user-specified process, which replaces the child process in-situ (never to return) but inherits the file descriptors (now only the pipes to the parent). There are two solutions:
The first is a huge pain, and a look at the comments in The second actually seems doable, though calling the private Pull requests on this later approach welcome. |
Our pet project ended up closing the file descriptors in the spawned process as recommended (Unix platform independent): if [ -d /proc/$$/fd/ ]; then
for descriptor_path in /proc/$$/fd/*; do
descriptor="$(basename "$descriptor_path")"
# Don't close stdin/stderr/stdout (-gt 2)
if [ $descriptor -gt 2 ]; then
exec {descriptor}<&-
fi
done
fi |
Fixed in v1.2.0 |
Because CLOEXEC isn't set on all the file descriptors, the child process inherits them all. Unfortunately it looks like there is no easy way to avoid this when using posix_spawnp. I haven't been able to find what the correct solution would be.
Here is an interesting thread about this issue. It sounds a bit messy, but one proposed solution seems to be to dup all file descriptors (even those that aren't open) to stdout and close them (except for stdin/stdout/stderr).
http://comp.unix.programmer.narkive.com/ZlyvRUIY/handling-the-posix-spawn-file-descriptor-hell#post57
The text was updated successfully, but these errors were encountered: