OpenProcess success does not prove a process is running: Windows keeps a terminated process object openable while any handle remains. Probe with SYNCHRONIZE and a zero-timeout wait instead. This corrects the production Dolt-server probe and the matching existing Linear helper. The Linear helper has no production caller today; its change is latent correctness and regression coverage, not a current Linear runtime fix. The wait-based design and regression strategy build on Marco Del Pin's contributor work in #4808. Co-authored-by: Marco Del Pin <marco.delpin@gmail.com> Agent-Signature: codex-tui-gpt-5.6-sol-ultra on behalf of Ewen Cuthiell
45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
//go:build windows
|
|
|
|
package linear
|
|
|
|
import (
|
|
"os/exec"
|
|
"testing"
|
|
|
|
"golang.org/x/sys/windows"
|
|
)
|
|
|
|
// Keep the parent-owned process handle open after exit to reproduce the state
|
|
// where OpenProcess succeeds for a process that is no longer running.
|
|
func TestIsProcessAliveReportsExitedProcessWithLingeringHandleAsDead(t *testing.T) {
|
|
cmd := exec.Command("cmd.exe", "/c", "exit", "0")
|
|
if err := cmd.Start(); err != nil {
|
|
t.Fatalf("start child process: %v", err)
|
|
}
|
|
t.Cleanup(func() {
|
|
if err := cmd.Wait(); err != nil {
|
|
t.Errorf("reap child process: %v", err)
|
|
}
|
|
})
|
|
|
|
pid := cmd.Process.Pid
|
|
probe, err := windows.OpenProcess(windows.SYNCHRONIZE, false, uint32(pid))
|
|
if err != nil {
|
|
t.Fatalf("open probe handle for process %d: %v", pid, err)
|
|
}
|
|
status, waitErr := windows.WaitForSingleObject(probe, 5000)
|
|
closeErr := windows.CloseHandle(probe)
|
|
if waitErr != nil {
|
|
t.Fatalf("wait for process %d to exit: %v", pid, waitErr)
|
|
}
|
|
if closeErr != nil {
|
|
t.Fatalf("close probe handle for process %d: %v", pid, closeErr)
|
|
}
|
|
if status != windows.WAIT_OBJECT_0 {
|
|
t.Fatalf("wait for process %d returned status %#x, want WAIT_OBJECT_0", pid, status)
|
|
}
|
|
|
|
if IsProcessAlive(pid) {
|
|
t.Fatalf("exited process %d reported alive while its parent handle remained open", pid)
|
|
}
|
|
}
|