fix(windows): detect exited processes in liveness helpers (#4980) (#4981)

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
This commit is contained in:
ecuthiell 2026-07-23 19:24:49 -06:00 • committed by GitHub
commit 594c464cc2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 104 additions and 9 deletions

View file

@ -98,16 +98,19 @@ func isProcessInDir(pid int, dir string) bool {
return false
}
// isProcessAlive checks if a process with the given PID is running.
// Uses OpenProcess with PROCESS_QUERY_LIMITED_INFORMATION — returns error if
// the process doesn't exist.
// isProcessAlive checks if a process with the given PID is running. Opening a
// process is not sufficient: an exited process remains openable while another
// handle refers to it. A zero-timeout wait distinguishes that signaled process
// from a running process.
func isProcessAlive(pid int) bool {
h, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, uint32(pid))
h, err := windows.OpenProcess(windows.SYNCHRONIZE, false, uint32(pid))
if err != nil {
return false
}
windows.CloseHandle(h)
return true
defer func() { _ = windows.CloseHandle(h) }()
status, err := windows.WaitForSingleObject(h, 0)
return err == nil && status == uint32(windows.WAIT_TIMEOUT)
}
// gracefulStop terminates a process on Windows. Uses TerminateProcess (hard kill)

View file

@ -0,0 +1,45 @@
//go:build windows
package doltserver
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)
}
}

View file

@ -7,10 +7,12 @@ import (
)
func isProcessAlive(pid int) bool {
h, err := windows.OpenProcess(windows.PROCESS_QUERY_LIMITED_INFORMATION, false, uint32(pid))
h, err := windows.OpenProcess(windows.SYNCHRONIZE, false, uint32(pid))
if err != nil {
return false
}
windows.CloseHandle(h)
return true
defer func() { _ = windows.CloseHandle(h) }()
status, err := windows.WaitForSingleObject(h, 0)
return err == nil && status == uint32(windows.WAIT_TIMEOUT)
}

View file

@ -0,0 +1,45 @@
//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)
}
}