-
Notifications
You must be signed in to change notification settings - Fork 1
Fix child process termination in containerized environments #3
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5e10112
Initial plan
Copilot 8d99941
Add process group and graceful timeout support
Copilot 1ec3730
Add test for process group termination
Copilot ba84e06
Update config.go
mudler e38c93a
Add platform-specific process group handling
Copilot c1a9ec4
Actually call SetSubreaper and implement child process reaping
Copilot 2b22273
Improve error handling in Stop() with context and wrapping
Copilot 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
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
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,58 @@ | ||
| //go:build unix | ||
|
|
||
| package process | ||
|
|
||
| import ( | ||
| "syscall" | ||
| "time" | ||
| ) | ||
|
|
||
| // getSysProcAttr returns the platform-specific syscall attributes for process creation | ||
| func getSysProcAttr() *syscall.SysProcAttr { | ||
| return &syscall.SysProcAttr{ | ||
| Setpgid: true, // Create new process group on Unix | ||
| } | ||
| } | ||
|
|
||
| // killProcess sends a signal to a process or process group | ||
| func killProcess(pid int, signal syscall.Signal, killProcessGroup bool) error { | ||
| target := pid | ||
| if killProcessGroup { | ||
| // Use negative PID to target the entire process group on Unix | ||
| target = -pid | ||
| } | ||
| return syscall.Kill(target, signal) | ||
| } | ||
|
|
||
| // reapChildren waits for and reaps orphaned child processes | ||
| // This is called when the process manager is acting as a subreaper | ||
| func (p *Process) reapChildren() { | ||
| for { | ||
| // Wait for any child process with WNOHANG (non-blocking) | ||
| var status syscall.WaitStatus | ||
| pid, err := syscall.Wait4(-1, &status, syscall.WNOHANG, nil) | ||
|
|
||
| // If we successfully reaped a child, continue the loop immediately | ||
| // to check for more children | ||
| if err == nil && pid > 0 { | ||
| continue | ||
| } | ||
|
|
||
| // No children to reap right now (ECHILD means no children, pid == 0 means no children ready) | ||
| if err == syscall.ECHILD || pid == 0 { | ||
| // Check if the main process is still alive | ||
| // If not, we can exit the reaper | ||
| if p.proc == nil || !p.IsAlive() { | ||
| return | ||
| } | ||
| // Sleep briefly before checking again to prevent busy-waiting | ||
| time.Sleep(100 * time.Millisecond) | ||
| continue | ||
| } | ||
|
|
||
| // For other errors, exit the reaper | ||
| if err != nil { | ||
| return | ||
| } | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot since we are here at touching
Stop(), let's improve errors that are returned such as we can identify better where they are coming from, and don't shadow errors, but wrap them where necessary.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Improved in 2b22273. The
Stop()method now:fmt.Errorfand%wto preserve error chainskillProcess()calls instead of ignoring them