mirror of
https://github.com/Twigpine/zero.git
synced 2026-11-10 05:01:28 -10:00
* fix(daemon): clean up child after startup timeout * fix(daemon): terminate startup process tree * test(background): accept reaped-later zombie children * fix(background): retain configured process group target * fix(background): address termination review feedback * fix(daemon): use launch-time group identity, fix zombie/reap classification Addresses jatmn's three #774 review findings: - TerminateCommand rediscovered the process group via execution.TerminateProcessTree's Getpgid(pid) lookup. On Darwin, that lookup can return ESRCH once an unreaped group leader has already exited, even though live descendants remain in the group it configured at launch — silently falling back to signalling only the (already dead) leader PID and leaving descendants running (the required macOS smoke job's failure). Add execution.TerminateProcessGroup, which signals the negative PID directly using the launch-time Setpgid invariant instead of rediscovering it, and route TerminateCommand through it. - signalTargetRunning's zombie check matched the individual-PID fallback target (a process that is not its own group leader) against the process table by PGID. That target's actual PGID differs from its own PID by definition, so no row ever matched, unknown resulted every time, and a zombie individual-PID target was always conservatively reported as still running. Branch on the sign of the target: group members by PGID, individual PIDs by PID. - TerminateCommand returned the termination error even when the subsequent reap succeeded — a successful cmd.Wait is authoritative proof the leader is gone and collected, regardless of how the kill attempt itself went (e.g. taskkill /T racing an already-exited PID on Windows). Classify a successful reap as success. All three regression tests are verified to fail against the pre-fix code (confirmed by temporarily reverting each fix and rerunning). Co-Authored-By: Claude Sonnet 5 <[email protected]> * fix(daemon): harden command termination inputs Amp-Thread-ID: https://ampcode.com/threads/T-019fa018-abb8-73de-9489-51e9e6514c6b Co-authored-by: Pierre Bruno <[email protected]> * fix(background): preserve process tree failures Amp-Thread-ID: https://ampcode.com/threads/T-019fa7a0-1223-701d-9529-48ba5d7cf8c8 Co-authored-by: Pierre Bruno <[email protected]> * docs(daemon): clarify boundary-probe and Setsid-guard comments per PR #774 review Co-authored-by: Cursor <[email protected]> --------- Co-authored-by: Claude Sonnet 5 <[email protected]> Co-authored-by: Amp <[email protected]> Co-authored-by: Cursor <[email protected]>
64 lines
1.6 KiB
Go
64 lines
1.6 KiB
Go
//go:build windows
|
|
|
|
package execution
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
func ConfigureProcessGroup(cmd *exec.Cmd) {}
|
|
|
|
// KillProcessTree asks taskkill /T to kill pid and descendants it can still
|
|
// discover from that live root. If taskkill fails, it falls back to killing pid
|
|
// alone but still returns the tree error because descendants may have survived.
|
|
// Neither path can recover descendants after the root has exited.
|
|
func KillProcessTree(pid int) error {
|
|
return killProcessTree(pid, runTaskkill, killRootProcess)
|
|
}
|
|
|
|
func killProcessTree(pid int, killTree, killRoot func(int) error) error {
|
|
treeErr := killTree(pid)
|
|
if treeErr == nil {
|
|
return nil
|
|
}
|
|
rootErr := killRoot(pid)
|
|
if rootErr != nil {
|
|
return errors.Join(
|
|
fmt.Errorf("kill process tree rooted at %d: %w", pid, treeErr),
|
|
fmt.Errorf("kill root process %d: %w", pid, rootErr),
|
|
)
|
|
}
|
|
return fmt.Errorf("kill process tree rooted at %d (root process killed directly): %w", pid, treeErr)
|
|
}
|
|
|
|
func runTaskkill(pid int) error {
|
|
return exec.Command(taskkillPath(), "/T", "/F", "/PID", strconv.Itoa(pid)).Run()
|
|
}
|
|
|
|
func killRootProcess(pid int) error {
|
|
process, err := os.FindProcess(pid)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return process.Kill()
|
|
}
|
|
|
|
func TerminateProcessTree(pid int, _, _ time.Duration) error { return KillProcessTree(pid) }
|
|
|
|
func taskkillPath() string {
|
|
systemRoot := os.Getenv("SystemRoot")
|
|
if systemRoot == "" {
|
|
systemRoot = os.Getenv("windir")
|
|
}
|
|
if systemRoot == "" {
|
|
systemRoot = `C:\Windows`
|
|
}
|
|
return filepath.Join(systemRoot, "System32", "taskkill.exe")
|
|
}
|