#919

Flaky: TestPoolDrainKillsStraggler synchronizes on QueueDepth, not on the worker being tracked

OpenPierrunoYTopened 3mo ago

Version / branch / commit

main @ dc15e822. The test and pool.go are unchanged since #274, so this affects every branch.

OS and environment

Observed on CI: Smoke (windows-latest), Windows Server 2025, Go per go.mod. Not OS-specific in principle — it is a scheduling race, and Windows CI is simply where the window is widest.

Steps to reproduce

go test ./internal/daemon/ -run TestPoolDrainKillsStraggler

Intermittent. It does not reproduce on an idle machine — I ran 30× normally and 200× under GOMAXPROCS=1 on Windows 11, all green. It shows up on loaded shared CI runners.

Recent occurrence: https://github.com/Gitlawb/zero/actions/runs/32013099576/job/95336704923 (on the PR #892 branch, whose diff touches only internal/config, internal/cli, and internal/tui — nothing in internal/daemon).

Expected behavior

Drain() force-kills a worker still running after KillTimeout, so straggler.killed == 1.

Actual behavior

--- FAIL: TestPoolDrainKillsStraggler (0.00s)
    pool_test.go:231: Drain must kill a straggler still running after KillTimeout
FAIL	github.com/Gitlawb/zero/internal/daemon	0.781s

Root cause

The test waits on the wrong piece of state. It gates on the slot lease, while Drain gates on the tracked worker set, and there is a window between the two.

  • internal/daemon/pool_test.go:222 — waitFor(t, func() bool { return pool.QueueDepth() == 1 })
  • internal/daemon/pool.go:169 — QueueDepth() is len(p.slots), i.e. slot-channel occupancy, set the moment Run leases a slot.
  • internal/daemon/pool.go:246 — the handle only enters p.active later, in runOnce → p.track(id, handle), after Launcher returns.
  • internal/daemon/pool.go:336 — Drain's grace loop reads len(p.active).

So waitFor can return while p.active is still empty. If Drain() runs in that window it takes the early return at pool.go:339:

if n == 0 {
    return // all workers drained gracefully
}

and never reaches the force-kill block at pool.go:344-354. straggler.killed stays 0, which is exactly the assertion that fires.

This is a test-synchronization defect, not a Drain bug — production Drain is correct for a worker that is actually tracked.

Suggested fix

Synchronize on the state Drain actually reads. WorkerStats() (pool.go:361) is built from p.active, so:

waitFor(t, func() bool { return len(pool.WorkerStats()) == 1 })

Worth a quick grep for the same QueueDepth()-as-readiness assumption in the neighbouring pool tests.

Activity

3
jatmn3mo ago

working on this.

jatmn3mo ago

working on this.

gnanam19903mo ago

Approved. The test still waits for QueueDepth() == 1, while Drain decides from the separately populated active-worker set, leaving the reported scheduling window. Synchronizing on WorkerStats is the appropriately scoped test fix. PR #927 is already active and the issue is assigned; please coordinate there.