fix(windows): report Dolt run-state correctly in bd config drift (#4808)

* fix(windows): report Dolt server run-state correctly

isServerProbablyRunning() checked the server PID with proc.Signal(syscall.Signal(0)),
the Unix null-signal probe. Windows has no signals (os.Process.Signal rejects
everything but Kill), so the probe always errored and every live PID looked dead --
bd doctor / drift always reported the shared Dolt server as not running on Windows.

An existence-only replacement is wrong too: on Windows a terminated process's kernel
object stays openable while any handle to it lingers (an un-Wait()ed parent, AV/EDR,
a debugger, Task Manager), so it reports a dead server as alive for a window after exit.

Split the probe into a build-tagged processAlive(pid), matching internal/doltserver
and internal/linear: Unix keeps Signal(0); Windows uses
OpenProcess(SYNCHRONIZE|PROCESS_QUERY_LIMITED_INFORMATION) + WaitForSingleObject(h,0) +
CloseHandle -- a terminated process's handle is signaled (WAIT_OBJECT_0), a running one
times out (WAIT_TIMEOUT). Unix behavior is unchanged.

Adds a Windows regression test that reproduces the lingering-handle false positive
deterministically (fails on the naive OpenProcess-success probe, passes on this fix).

Agent-Signature: claude-code-opus-4.8-high on behalf of Marco Del Pin

* fix(windows): address #4808 review: WAIT_OBJECT_0 precondition, Windows PR-time test lane, naming

- Require WAIT_OBJECT_0 from WaitForSingleObject in the lingering-handle
  test precondition (the pinned x/sys returns nil error on WAIT_TIMEOUT;
  only WAIT_FAILED errors), matching the pattern of the newer Windows
  tests in internal/doltserver and internal/linear.
- Add a targeted windows-latest PR-time job (test-windows-liveness)
  running the ^TestIsServerProbablyRunning tests, wired into the CI gate:
  the //go:build windows regression test is compiled out on the Ubuntu
  jobs, and main.yml's Windows job is push-to-main build/smoke only, so
  without this lane the regression guard never executes at PR time.
- Rename h/s to processHandle/waitResult in processAlive.

Scope note (review should-fix 1): this PR fixes the bd config drift
probe only, processAlive's single production caller. bd doctor's server
run-state goes through doltserver.IsRunning, fixed separately upstream
by 594c464cc.

* fix(windows): rename processAlive to pidAlive to avoid merge-ref collision with #5004 test helper

Upstream bb9bb7487 (#5004, merged 2026-07-24) added a test-only
processAlive helper in cmd/bd/proxied_local_helpers_test.go
(//go:build cgo && unix). On the PR merge ref both declarations
coexist in package main and the cgo unix lanes fail to compile
(processAlive redeclared - Build (Embedded Dolt) + PR Core).
Renaming the production helper introduced by this PR is the
minimal fix; the #5004 test helper is untouched.

* fix(windows): check CloseHandle in pidAlive to satisfy errcheck

The PR gate went red after the maintainer merge of current main wired the
windows lint lane into ci-gate:

  cmd/bd/config_drift_windows.go:26:27: Error return value of
  windows.CloseHandle is not checked (errcheck)

failing both Build Artifacts and PR Lint (wrapper timing) on run 30422840210.

Adopt the pattern every other windows file in the tree already uses for a
deferred handle close - doltserver_windows.go:110, synclock_windows.go:14,
procid_windows.go:35/47, process_executable_windows.go:21:

  defer func() { _ = windows.CloseHandle(processHandle) }()

This file was the only defer windows.CloseHandle(...) without the wrapper.

Verified with the linter itself rather than by inspection: errcheck v1.20.0,
GOOS=windows, -tags gms_pure_go.
  fixed tree     -> 0 hits on config_drift_windows.go:26
  line reverted  -> 1 hit  on config_drift_windows.go:26
so the check demonstrably goes red on the old line and green on the new one.
Also GOOS=windows go vet ./cmd/bd clean, gofmt clean.

The three remaining bare CloseHandle calls in config_drift_windows_test.go are
deliberately untouched: .golangci.yml sets tests: false, which is why CI named
only line 26.

---------

Co-authored-by: Test <test@test.com>
Co-authored-by: matt wilkie <maphew@gmail.com>
Co-authored-by: Marco Del Pin <marco@omniaevo.it>
This commit is contained in:
Marco Del Pin 2026-08-07 19:34:32 +02:00 • committed by GitHub
commit 9200e3d5d1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 140 additions and 9 deletions

View file

@ -127,6 +127,25 @@ jobs:
run: |
go test -tags gms_pure_go -count=1 -short -run '^Test(Help|CheckRemoteSafety|FormatDestroyToken|ShouldWireInitRemote|ExtractPrefix|IsNumericID|GetWorktreeGitDir|DriftItemStatuses|RunDriftChecks|IsServerProbablyRunning|CheckHooksDriftNotGitRepo|CheckServerDriftNoBeadsDir|CheckRemoteDriftNoBeadsDir)' ./cmd/bd
# The lingering-handle liveness regression test is //go:build windows: the
# Ubuntu job above selects it by name but compiles it out, and main.yml's
# Windows job runs only on pushes to main (build/smoke, no go test). Run
# exactly the liveness pair on windows-latest so the regression guard
# actually executes at PR time.
test-windows-liveness:
name: Test (Windows) cmd/bd liveness regression
runs-on: windows-latest
steps:
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v6
- name: Set up Go
uses: actions/setup-go@924ae3a1cded613372ab5595356fb5720e22ba16 # v6
with:
go-version-file: 'go.mod'
- name: Run Windows liveness regression test
run: go test -tags gms_pure_go -count=1 -run '^TestIsServerProbablyRunning' ./cmd/bd
# Destructive worktree removal depends on native Windows path identity.
# This focused process suite requires NTFS per-directory case sensitivity
# and fails (rather than skips) when the runner cannot provide it.
@ -959,6 +978,7 @@ jobs:
- build-artifacts
- check-build-tags
- check-cmd-bd-puregeo-tests
- test-windows-liveness
- worktree-remove-windows
- check-version-consistency
- check-migration-hygiene
@ -988,6 +1008,7 @@ jobs:
BUILD_ARTIFACTS
CHECK_BUILD_TAGS
CHECK_CMD_BD_PUREGEO_TESTS
TEST_WINDOWS_LIVENESS
WORKTREE_REMOVE_WINDOWS
CHECK_VERSION_CONSISTENCY
CHECK_MIGRATION_HYGIENE
@ -1009,6 +1030,7 @@ jobs:
BUILD_ARTIFACTS: ${{ needs.build-artifacts.result }}
CHECK_BUILD_TAGS: ${{ needs.check-build-tags.result }}
CHECK_CMD_BD_PUREGEO_TESTS: ${{ needs.check-cmd-bd-puregeo-tests.result }}
TEST_WINDOWS_LIVENESS: ${{ needs.test-windows-liveness.result }}
WORKTREE_REMOVE_WINDOWS: ${{ needs.worktree-remove-windows.result }}
CHECK_VERSION_CONSISTENCY: ${{ needs.check-version-consistency.result }}
CHECK_MIGRATION_HYGIENE: ${{ needs.check-migration-hygiene.result }}

View file

@ -7,7 +7,6 @@ import (
"path/filepath"
"strconv"
"strings"
"syscall"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/beads"
@ -329,14 +328,7 @@ func isServerProbablyRunning(beadsDir string) bool {
return false
}
// Check if the process exists (signal 0 = existence check)
proc, err := os.FindProcess(pid)
if err != nil {
return false
}
// On Unix, FindProcess always succeeds; use Signal(0) to verify
err = proc.Signal(syscall.Signal(0))
return err == nil
return pidAlive(pid)
}
// printDriftItems renders drift results in human-readable format.

View file

@ -0,0 +1,21 @@
//go:build !windows
package main
import (
"os"
"syscall"
)
// pidAlive reports whether a process with the given PID is still running.
//
// On Unix os.FindProcess always succeeds (it does not touch the OS), so the only
// way to tell a live PID from a dead one is to deliver the null signal: signal 0
// performs the permission and existence checks without sending anything.
func pidAlive(pid int) bool {
proc, err := os.FindProcess(pid)
if err != nil {
return false
}
return proc.Signal(syscall.Signal(0)) == nil
}

View file

@ -0,0 +1,33 @@
//go:build windows
package main
import "golang.org/x/sys/windows"
// pidAlive reports whether a process with the given PID is still running.
//
// A bare OpenProcess / os.FindProcess success is NOT sufficient on Windows.
// The kernel process object of a terminated process lingers, and stays
// openable, for as long as any handle to it remains open anywhere on the
// system (a parent that never Wait()ed, an AV/EDR scanner, a debugger, Task
// Manager). So an existence-only probe reports a dead server as alive for a
// window after it exits.
//
// Instead, wait on the process handle with a zero timeout: a terminated
// process's handle is signaled (WaitForSingleObject returns WAIT_OBJECT_0),
// a running one times out (WAIT_TIMEOUT). This distinguishes live from dead
// even when OpenProcess still succeeds on a lingering handle.
func pidAlive(pid int) bool {
processHandle, err := windows.OpenProcess(windows.SYNCHRONIZE|windows.PROCESS_QUERY_LIMITED_INFORMATION, false, uint32(pid))
if err != nil {
// No such process, or we may not open it.
return false
}
defer func() { _ = windows.CloseHandle(processHandle) }()
waitResult, err := windows.WaitForSingleObject(processHandle, 0)
if err != nil {
return false
}
return waitResult == uint32(windows.WAIT_TIMEOUT)
}

View file

@ -0,0 +1,63 @@
//go:build windows
package main
import (
"os"
"os/exec"
"path/filepath"
"strconv"
"testing"
"golang.org/x/sys/windows"
"github.com/steveyegge/beads/internal/doltserver"
)
// TestIsServerProbablyRunningReportsDeadPIDWithLingeringHandle guards the
// Windows liveness probe against the lingering-handle false positive.
//
// On Windows a terminated process's kernel object stays openable for as long
// as any handle to it remains open anywhere on the system. An existence-only
// probe (OpenProcess / os.FindProcess succeeds) therefore reports a dead
// server as alive. This test reproduces exactly that: it starts a child that
// exits immediately and deliberately never Wait()s it, so os/exec keeps a
// lingering handle open; it confirms the child has actually terminated, then
// asserts the drift probe reports it dead.
func TestIsServerProbablyRunningReportsDeadPIDWithLingeringHandle(t *testing.T) {
cmd := exec.Command("cmd.exe", "/c", "exit")
if err := cmd.Start(); err != nil {
t.Fatalf("start child: %v", err)
}
pid := cmd.Process.Pid
// Reap the child at the end; until then cmd.Process keeps a handle open,
// which is the lingering reference that defeats an existence-only probe.
defer func() { _ = cmd.Wait() }()
// Confirm the child has really exited, via a SEPARATE probe handle so we do
// not disturb the lingering one held by cmd.Process.
probe, err := windows.OpenProcess(windows.SYNCHRONIZE, false, uint32(pid))
if err != nil {
t.Skipf("cannot open probe handle for pid %d: %v", pid, err)
}
status, err := windows.WaitForSingleObject(probe, 5000)
if err != nil {
windows.CloseHandle(probe)
t.Fatalf("waiting for child to exit: %v", err)
}
if status != windows.WAIT_OBJECT_0 {
windows.CloseHandle(probe)
t.Fatalf("wait for process %d returned status %#x, want WAIT_OBJECT_0", pid, status)
}
windows.CloseHandle(probe)
beadsDir := t.TempDir()
pidFile := filepath.Join(beadsDir, doltserver.PIDFileName)
if err := os.WriteFile(pidFile, []byte(strconv.Itoa(pid)+"\n"), 0o600); err != nil {
t.Fatalf("write pid file: %v", err)
}
if isServerProbablyRunning(beadsDir) {
t.Errorf("dead pid %d reported as running (lingering-handle false positive)", pid)
}
}