Repository navigation
Add procattr to start and stop process trees on each platform - #108
Conversation
| // KillGroup terminates proc and every process descended from it. | ||
| func KillGroup(proc *os.Process) error { | ||
| // Windows has no group-wide signal; taskkill /T walks the parent-pid tree. | ||
| err := exec.Command("taskkill", taskkillArgs(proc.Pid)...).Run() |
There was a problem hiding this comment.
Consider adding SysProcAttr{HideWindow: true} so every kill doesn't flash a console window.
| } | ||
|
|
||
| // KillGroup terminates proc and every process descended from it. | ||
| func KillGroup(proc *os.Process) error { |
There was a problem hiding this comment.
I would also thread a context here and pass it as exec.CommandContext. On unix it's just a syscall, but on windows we're spawning a subprocess that can block for an unbound amount of time.
| // Signal sends sig to the process with the given pid. Windows supports only | ||
| // SIGKILL. | ||
| func Signal(pid int, sig syscall.Signal) error { | ||
| proc, err := os.FindProcess(pid) |
There was a problem hiding this comment.
I think we need a defer proc.Release() here.
| cmd.SysProcAttr = NewGroup() | ||
| require.NoError(t, cmd.Run()) | ||
|
|
||
| assert.ErrorIs(t, KillGroup(cmd.Process), os.ErrProcessDone) |
There was a problem hiding this comment.
After cmd.Run() the process has returned, the pgid is free for reuse and SIGKILL(-pid) can land on an unrelated group. Low probability, but getting random process killed on a busy CI machine would not be fun to debug.
| // `go run` exec the real program as a grandchild that SIGKILL | ||
| // cannot be forwarded to. Joining an existing Pgid is someone | ||
| // else's group and not ours to signal. | ||
| attr := cmd.SysProcAttr |
There was a problem hiding this comment.
the comment above now belongs to the LeadsGroup implementations
| err := syscall.Kill(int(pid), signal) | ||
| err := procattr.Signal(int(pid), signal) | ||
| if err != nil { | ||
| return fmt.Errorf("syscall kill: %w", err) |
There was a problem hiding this comment.
we should update this error now to "Send signal: %w"
Four call sites built syscall.SysProcAttr literals with Setpgid, Setsid and Setctty, and the file scheme read Setpgid and Pgid and killed process groups with syscall.Kill. None of these exist on Windows, so workspace, workspacerpc, extensionv2, idedebug and vte fail to compile there (unstablebuild#68). Collect them in internal/procattr: NewGroup, NewSession, LeadsGroup, KillGroup and Signal. The unix implementations are the old code moved unchanged, including killProcessGroup from the file scheme. On Windows a new group is CREATE_NEW_PROCESS_GROUP, a group is killed with taskkill /T /F, which walks the process tree, sessions and controlling terminals do not exist, and Signal supports SIGKILL only. idelsp has the same Setpgid literal; it moves in a follow-up so this change stays independent of the LSP socketpair split. Linux and macOS are unchanged: the existing process-group tests pass as before, and new tests pin that the helpers build the same attributes as the literals they replace. Signed-off-by: Eric Brooks <[email protected]>
… site The Unix implementation was selected with !windows, which also matched js/wasm and plan9, where SysProcAttr has no Setpgid or Setsid and the package failed to build. The language server spawn site still built its SysProcAttr by hand, so it was left out of the shared contract. On Windows, KillGroup ran taskkill found on PATH, flashed a console window from a GUI process, and could block cancel and teardown with no bound, where the Unix syscall never blocks. Signal leaked the handle opened by FindProcess. NewSession now returns nil when nothing is requested, so callers no longer need their own guard. The Unix KillGroup test signalled the group of a reaped child whose pid could be reused; it now targets a pid above any pid_max. Tests that only restated the implementation are removed. Co-Authored-By: Rune Agent (claude/claude-opus-5-5) <[email protected]> Signed-off-by: ernestrc <[email protected]>
9049b05 to
d411366
Compare
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Part of Windows support (#68, coordinated on #81). Third slice; graphics stay with #82.
Four call sites built
syscall.SysProcAttrliterals withSetpgid,SetsidandSetctty, and thefile scheme killed process groups with
syscall.Kill. None of these exist on Windows, soworkspace,workspacerpc,extensionv2,idedebugandvtefail to compile there.This collects them in a small
internal/procattrpackage:NewGroup,NewSession,LeadsGroup,KillGroupandSignal. The unix implementations are the old code moved unchanged, includingkillProcessGroupfrom the file scheme. On Windows a new group isCREATE_NEW_PROCESS_GROUP, agroup is killed with
taskkill /T /F(which walks the process tree, with "not found" mapped toos.ErrProcessDonelikeESRCHon unix), sessions and controlling terminals do not exist, andSignalsupportsSIGKILLonly.Linux and macOS are unchanged: the existing process-group tests in
file_scheme_testandworkspace_runner_testpass as before, and new tests pin that the helpers build exactly theattributes the literals built. The Windows-tagged tests cover the
taskkillarguments and thegroup check;
GOOS=windows go vet ./internal/procattr/passes.idelsp's singleSetpgidliteral is deliberately left alone here because #107 touches the samefile's imports; it becomes a one-line follow-up once both land.