-
Notifications
You must be signed in to change notification settings - Fork 215
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
Add ProcessExitGroup to ensure children are killed on exit #5198
Conversation
b2c9d07
to
1b0ed7b
Compare
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.
Would it make sense to add a unit test that would:
- spawn process A that spawns process B
- kill process A
- observe that process B died
activation/post_supervisor_nix.go
Outdated
type ProcessExitGroup struct{} | ||
|
||
// NewProcessExitGroup returns a new ProcessExitGroup. | ||
func NewProcessExitGroup() (ProcessExitGroup, error) { | ||
return ProcessExitGroup{}, nil | ||
} | ||
|
||
// Dispose closes the ProcessExitGroup. | ||
func (g ProcessExitGroup) Dispose() error { | ||
return nil | ||
} | ||
|
||
// StartCommand starts the given command and adds it to the ProcessExitGroup. | ||
func (g ProcessExitGroup) StartCommand(cmd *exec.Cmd) error { | ||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true, Pgid: os.Getpid()} | ||
if err := cmd.Start(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
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.
How about making it all private?
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.
wdym? These methods need to be called from outside the object?
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.
But it's used in the same package, isn't it?
wouldn't work on linux (because the code actually doesn't work on non-windows systems) |
I think it's fine to test windows-specific code only on windows :) Just skip/don't compile the test on a different OS. I realize that the test would be much longer than the implementation, but on the other hand, the code for it is cryptic and prone to regressions :( |
This shouldn't be required with spacemeshos/post-rs#141. |
Superseded by #5201 |
Motivation
If the node exits unexpectedly or is killed with
SIGKILL
instead ofSIGTERM
it will not kill post services that it spawned.This PR fixes this by adding a job object that kills processes assigned to it on close. For information on how this works see the official Microsoft documentation on job objects: https://learn.microsoft.com/en-us/windows/win32/api/jobapi2/nf-jobapi2-assignprocesstojobobject and https://learn.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-jobobject_basic_limit_information
Changes
ProcessExitGroup
adds the new process to a job object (itself) and ensures that they are closed when itself is closedFor windows this ensures that if the main process crashes / is killed all commands started through the
ProcessExitGroup
will be killed as well. On other systems this is only true for crashes. Killing the node withkill -9
will not cause the child processes to exit automatically. For this they have to listen to signals about their parent and close themselves.Test Plan
TODO