43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package claude
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/claudehost"
|
|
)
|
|
|
|
// hostReferenceLive fails closed: an alive or uninspectable PID must never be
|
|
// signalled merely because its control socket did not answer. A stale reference
|
|
// to a recycled PID can delay recovery, but cannot kill unrelated work.
|
|
func hostReferenceLive(record claudehost.Record) bool {
|
|
for _, pid := range []int{record.HostPID, record.ClaudePID} {
|
|
if pid <= 1 {
|
|
continue
|
|
}
|
|
err := syscall.Kill(pid, 0)
|
|
if err != nil && !errors.Is(err, syscall.EPERM) {
|
|
continue
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond)
|
|
state, inspectErr := exec.CommandContext(ctx, "ps", "-o", "stat=", "-p", fmt.Sprint(pid)).Output()
|
|
cancel()
|
|
if inspectErr == nil && strings.HasPrefix(strings.TrimSpace(string(state)), "Z") {
|
|
continue // a zombie cannot own live pipes or execute work
|
|
}
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (c *Chat) removeDeadHostReference(record claudehost.Record) {
|
|
if !hostReferenceLive(record) {
|
|
claudehost.Remove(c.hostDir, record.SessionID)
|
|
}
|
|
}
|