fix(supervision): reap owned session workers after monitor crash
Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
1 parent
d7397ed6cf
commit
e9b70b00b6
7 files changed
+206
-10
No files matched your search
@@ -668,18 +668,31 @@ func TestManagedEntrypointSupervision(t *testing.T) {
|
||||
cmd.Env = append(os.Environ(), "PATH="+root+":"+os.Getenv("PATH"), "LOG="+log, "MODE="+mode, "ZAPRET_CONFIG="+filepath.Join(root, "config"), "SS_PASSWORD="+strings.Repeat("a", 32))
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||
var shutdownSnapshot string
|
||||
stopMonitorSession := func() {
|
||||
ready, _ := os.ReadFile(filepath.Join(config, "monitor-ready"))
|
||||
sid, err := strconv.Atoi(strings.TrimSpace(string(ready)))
|
||||
if err != nil || sid <= 1 || sid == os.Getpid() || sid == cmd.Process.Pid {
|
||||
return
|
||||
}
|
||||
// Deadline cleanup must cover the fixture's verified private SID,
|
||||
// including separate check PGIDs, not just the entrypoint PGID.
|
||||
_ = exec.Command("/bin/sh", "-c", `sid=$1; helper=$2; shift 2; . "$helper"; signal_session "$sid" KILL`, "cleanup", strconv.Itoa(sid), helper).Run()
|
||||
}
|
||||
cmd.Cancel = func() error {
|
||||
// Snapshot only this fixture's process group before deadline cleanup;
|
||||
// no environment or private credential/config contents are collected.
|
||||
processes, _ := exec.Command("ps", "-eo", "pid,ppid,pgid,stat,wchan:24,args").Output()
|
||||
ready, _ := os.ReadFile(filepath.Join(config, "monitor-ready"))
|
||||
ownedSID := strings.TrimSpace(string(ready))
|
||||
processes, _ := exec.Command("ps", "-eo", "pid,ppid,pgid,sid,stat,wchan:24,args").Output()
|
||||
for _, line := range strings.Split(string(processes), "\n") {
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) >= 3 && fields[2] == strconv.Itoa(cmd.Process.Pid) {
|
||||
if len(fields) >= 4 && (fields[2] == strconv.Itoa(cmd.Process.Pid) || fields[3] == ownedSID) {
|
||||
shutdownSnapshot += line + "\n"
|
||||
}
|
||||
}
|
||||
fixtureLog, _ := os.ReadFile(log)
|
||||
shutdownSnapshot += "fixture log:\n" + string(fixtureLog)
|
||||
stopMonitorSession()
|
||||
return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||
}
|
||||
var output bytes.Buffer
|
||||
@@ -690,7 +703,16 @@ func TestManagedEntrypointSupervision(t *testing.T) {
|
||||
done := make(chan error, 1)
|
||||
exited := make(chan struct{})
|
||||
go func() { done <- cmd.Wait(); close(exited) }()
|
||||
t.Cleanup(func() { syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL); <-exited })
|
||||
t.Cleanup(func() {
|
||||
select {
|
||||
case <-exited:
|
||||
return
|
||||
default:
|
||||
stopMonitorSession()
|
||||
syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||
<-exited
|
||||
}
|
||||
})
|
||||
if strings.HasPrefix(mode, "term") {
|
||||
for {
|
||||
b, _ := os.ReadFile(log)
|
||||
|
||||
Reference in new issue
Block a user