beads/cmd/bd/config_drift_unix.go
Marco Del Pin 9200e3d5d1
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>
2026-08-07 10:34:32 -07:00

21 lines
527 B
Go

//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
}