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:
parent
db0787bb15
commit
594c464cc2
4 changed files with 104 additions and 9 deletions
|
|
@ -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)
|
||||
|
|
|
|||
45
internal/doltserver/doltserver_windows_test.go
Normal file
45
internal/doltserver/doltserver_windows_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
45
internal/linear/synclock_windows_test.go
Normal file
45
internal/linear/synclock_windows_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue