Repository navigation
test(cni): fix the data race under -race in the drain pool-close test - #595
Merged
Merged
Conversation
…#554) `bazel test --@rules_go//go/config:race=true //agent/internal/cni/server:server_test` failed deterministically on `TestHandlePodTerminatingPoolClosePhase`. Triage says test-side, not a production bug: the read stack is `schedulePoolClose` holding `lifecycleMu` (as documented), and the write stack is the test body calling `MockStorage.RemoveResource` directly, outside that critical section. The racing memory is the mock's backing map. `LocalStorage` — the implementation `MockStorage` stands in for — is explicitly safe for concurrent access (RWMutex). The double was not, so any test that drives a production path with background goroutines (liveness loop, ghost sweep, drain timers) races on the map instead of testing the code. Guard `resources` with an RWMutex, calling the configurable Func hooks outside the lock. The test also simulated CNI DEL in a way no real DEL can: an unlocked storage delete with only a 10ms margin over the drain timer. Take `lifecycleMu` across the removal the way `RemovePod` does, widen the delay so the interleaving under test is the one exercised, and replace the bare sleep with a polling `assert.Never`. Verified: 20/20 green under `--runs_per_test=20` with the race detector, and all of `//agent/...` now passes under `-race` (27/27). Closes #554 Co-Authored-By: Claude <[email protected]> Claude-Session: https://claude.ai/code/session_01EioJVuoepwStjHoRLB52WR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Triage
bazel test --@rules_go//go/config:race=true //agent/internal/cni/server:server_testfails deterministically (not intermittently) onTestHandlePodTerminatingPoolClosePhase:Verdict: racy test synchronisation — not a production data race.
The read side is production code, but it is correctly synchronised:
schedulePoolClosetakeslifecycleMubeforeGetResource(termination.go:206-208), exactly as its comment promises, and every production mutation of that storage (RemovePod, the liveness loop, the ghost sweep) holds the same mutex. The write side is test scaffolding: the test body callsMockStorage.RemoveResourcedirectly at termination_test.go:205 to simulate a CNI DEL, outsidelifecycleMu. The racing memory is the mock's backing map, not any production state.Two defects behind it:
LocalStorageis documented "safe for concurrent access using RWMutex";MockStoragehad a bare map. Any test that drives a production path with background goroutines (liveness loop, ghost sweep, drain timers) therefore races on the map rather than testing the code under test. The sibling double in this same file (unregisterRecordingRegistry) is mutex-guarded — the storage one was simply missed.schedulePoolClose's critical section, between its existence check and the re-registration — an interleaving a real DEL (which removes storage underlifecycleMu) can never produce. It also had only a ~10ms margin over the drain timer, so the ordering it claims to test was whatever the scheduler picked.Fix
agent/storage/mock.go: guardresourceswith async.RWMutex, matchingLocalStorage's concurrency contract. The configurable*Funchooks are invoked outside the lock so a hook may re-enter the mock.termination_test.go: perform the simulated DEL underlifecycleMu(asRemovePoddoes), widen the drain delay so "DEL wins" is actually the interleaving exercised, and replace the baretime.Sleep+assert.Equalwith a pollingassert.Neverthat spans the drain deadline.No production code changed. No sleeps added, nothing skipped or short-gated.
Verification
--runs_per_test=20with--@rules_go//go/config:race=true: 20/20 PASSED (max 15.2s, min 14.5s, avg 14.9s).//agent/...under-race: 27/27 pass — this was the only race blocker in the tree.bazel test //agent/...: 27/27 pass.bazel run //:formatclean,bazel build --config=ci //agent/...(lint) green,make gazelleproduced no BUILD changes.Follow-up recommendation (not in this PR)
The repo already has
test:race --@rules_go//go/config:racein.bazelrc, and//agent/...is now fully green under it — so a race leg would pass today. Recommendation: add a nightly (ore2e.yaml-style scheduled)bazel test --config=race //...job rather than a per-PR leg. A per-PR leg would double the compile+cache footprint of the impacted-target legs (race builds a separate configuration, so it shares no cache with the normal build) and inflate every PR's critical path for a class of bug that is usually latent rather than newly introduced; a nightly catches it within a day, when it is still cheap to attribute. Without something, this class stays invisible — which is exactly why #554 sat parked.Closes #554
🤖 Generated with Claude Code
https://claude.ai/code/session_01EioJVuoepwStjHoRLB52WR