-
-
Notifications
You must be signed in to change notification settings - Fork 926
Create new pgroup when spawning process in chdir #5383
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
Merged
Merged
Conversation
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
`Process.spawn` is leaving orphaned processes when run inside `Dir.chdir`: ```ruby >> JRUBY_VERSION => "9.2.0.0" >> Dir.chdir('/tmp') { Process.spawn('cat') } => 88255 ``` ```bash $ ps xao pid,ppid,pgid,comm | grep -E ' (cat|sh)' 88255 88092 88092 sh 88256 88255 88092 cat ``` ```ruby >> Process.kill('TERM', 88255) => 1 ``` ```bash $ ps xao pid,ppid,pgid,comm | grep -E ' (cat|sh)' 88256 1 88092 cat ``` I believe the issue is that adding the `cd` command (for chdir) causes a second process to start. That process is grouped with the main java one and doesn't die when its parent (`sh`) is killed. This change creates a new process group for the `sh` command, which causes the child process to die when the parent is killed. I ran into this issue using Foreman to manage some JRuby processes.
Another option for this is using |
headius
approved these changes
Dec 18, 2018
This looks good, and it's a great discovery. Thank you! |
eregon
reviewed
Dec 28, 2018
headius
added a commit
to headius/jruby
that referenced
this pull request
Feb 26, 2021
In jruby#5383 we added logic to create a new process group when doing chdir in spawn, to avoid orphaning the eventual process spawned by the intermediate sh. Unfortunately this has a side effect of hanging on waitpid if the process spawns a long-running daemon. This patch removes the process group and instead uses exec in the generated sh script so that the sh process is replaced. The forked daemon lives on, as it should, but only the main process is waited on. Fixes jruby#6579
This was referenced Feb 26, 2021
headius
added a commit
to headius/jruby
that referenced
this pull request
Feb 26, 2021
In jruby#5383 we added logic to create a new process group when doing chdir in spawn, to avoid orphaning the eventual process spawned by the intermediate sh. Unfortunately this has a side effect of hanging on waitpid if the process spawns a long-running daemon. This patch removes the process group and instead uses exec in the generated sh script so that the sh process is replaced. The forked daemon lives on, as it should, but only the main process is waited on. Fixes jruby#6579
headius
added a commit
to headius/jruby
that referenced
this pull request
Feb 26, 2021
In jruby#5383 we added logic to create a new process group when doing chdir in spawn, to avoid orphaning the eventual process spawned by the intermediate sh. Unfortunately this has a side effect of hanging on waitpid if the process spawns a long-running daemon. This patch removes the process group and instead uses exec in the generated sh script so that the sh process is replaced. The forked daemon lives on, as it should, but only the main process is waited on. Fixes jruby#6579
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.
Process.spawn
is leaving orphaned processes when run insideDir.chdir
:I believe the issue is that adding the
cd
command (for chdir) causes asecond process to start. That process is grouped with the main java one
and doesn't die when its parent (
sh
) is killed.This change creates a new process group for the
sh
command, whichcauses the child process to die when the parent is killed.
I ran into this issue using Foreman to manage some JRuby processes.