TestAStartWhoseRequestEndsWhileWaitingForTheLockLaysNothingOut is racy on main (from d34c0a72). The test is wrong, not the production code.
Measured on a loaded box, 50 runs at each of GOMAXPROCS 1, 2, 4 and 16:
| | fails | |---|---| | main a64f5a41 | 71/200 | | instance-egress-sizes-328-329 | 76/200 |
At GOMAXPROCS=1 it fails about 90% of the time on both. That branch doesn't touch rootfs.go or rootfs_unitlock.go.
Cause. The test assumes the server notices start B's client hang-up (a 100 ms client deadline) before start A finishes and releases the unit's lock. B's request context only ends when net/http's background read sees the closed connection, and when that happens is up to the scheduler. If A's release (rootfs.go:680) wakes B first, B takes the lock with a live context. It then passes both checks (rootfs_unitlock.go:108 and rootfs.go:762) and starts the unit a second time. A server cannot act on a hang-up it has not seen yet (the #292 unseen-cancel race), so the production code is right.
The http: panic serving ...: the removal's files() path failed line is unrelated. It comes from TestAPanicDuringARemovalLeavesTheUnitsLockFree's deliberate panic.
Fix (test only). Order B's end explicitly instead of timing it with a client deadline:
- Send B in a goroutine with a cancellable context.
- Wait until the unit's lock entry has two references (A's hold and B's wait).
- Cancel B's context and wait for
Doto return. - Wait until the entry is back to one reference before the hook returns, so A carries on.
Each wait polls every 1 ms with a 5 s deadline. Add a test-only helper next to rootfsLockCount:
func (s *Server) rootfsLockRefs(unit string) int {
s.rootfsLocksMu.Lock()
defer s.rootfsLocksMu.Unlock()
if e := s.rootfsLocks[unit]; e != nil {
return e.refs
}
return 0
}Checked in scratch:
- With a listener that notices hang-ups 500 ms late, the current hook fails 3/3 and the ordered hook passes.
- The ordered hook passed 60/60 at -cpu 1,4,16.
- It still fails when
holdRootfsUnitignores its context, so it still guards fix L3.