Skip to content

test(cni): fix the data race under -race in the drain pool-close test - #595

Merged
bpalermo merged 1 commit into
mainfrom
fix/554-race-pod-terminating
Jul 26, 2026
Merged

bpalermo merged 1 commit into
mainfrom
fix/554-race-pod-terminating

Conversation

@bpalermo

Copy link
Copy Markdown
Owner

Triage

bazel test --@rules_go//go/config:race=true //agent/internal/cni/server:server_test fails deterministically (not intermittently) on TestHandlePodTerminatingPoolClosePhase:

WARNING: DATA RACE
Read at 0x00c0008282a0 by goroutine 65:
  runtime.mapaccess2_faststr()
  github.com/bpalermo/aether/agent/storage.(*MockStorage[...]).GetResource()
      agent/storage/mock.go:78
  github.com/bpalermo/aether/agent/internal/cni/server.(*CNIServer).schedulePoolClose()
      agent/internal/cni/server/termination.go:208
  ...handlePodTerminating.gowrap2()
      agent/internal/cni/server/termination.go:145

Previous write at 0x00c0008282a0 by goroutine 59:
  runtime.mapdelete_faststr()
  github.com/bpalermo/aether/agent/storage.(*MockStorage[...]).RemoveResource()
      agent/storage/mock.go:70
  ...server.TestHandlePodTerminatingPoolClosePhase()
      agent/internal/cni/server/termination_test.go:205
  testing.tRunner()

Verdict: racy test synchronisation — not a production data race.

The read side is production code, but it is correctly synchronised: schedulePoolClose takes lifecycleMu before GetResource (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 calls MockStorage.RemoveResource directly at termination_test.go:205 to simulate a CNI DEL, outside lifecycleMu. The racing memory is the mock's backing map, not any production state.

Two defects behind it:

  1. The double doesn't match the contract it doubles. LocalStorage is documented "safe for concurrent access using RWMutex"; MockStorage had 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.
  2. The test modelled a CNI DEL that cannot happen. An unlocked storage delete can land inside schedulePoolClose's critical section, between its existence check and the re-registration — an interleaving a real DEL (which removes storage under lifecycleMu) 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: guard resources with a sync.RWMutex, matching LocalStorage's concurrency contract. The configurable *Func hooks are invoked outside the lock so a hook may re-enter the mock.
  • termination_test.go: perform the simulated DEL under lifecycleMu (as RemovePod does), widen the drain delay so "DEL wins" is actually the interleaving exercised, and replace the bare time.Sleep + assert.Equal with a polling assert.Never that spans the drain deadline.

No production code changed. No sleeps added, nothing skipped or short-gated.

Verification

  • --runs_per_test=20 with --@rules_go//go/config:race=true: 20/20 PASSED (max 15.2s, min 14.5s, avg 14.9s).
  • All of //agent/... under -race: 27/27 pass — this was the only race blocker in the tree.
  • Normal mode bazel test //agent/...: 27/27 pass.
  • bazel run //:format clean, bazel build --config=ci //agent/... (lint) green, make gazelle produced no BUILD changes.

Follow-up recommendation (not in this PR)

The repo already has test:race --@rules_go//go/config:race in .bazelrc, and //agent/... is now fully green under it — so a race leg would pass today. Recommendation: add a nightly (or e2e.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

…#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
@bpalermo
bpalermo merged commit 4389a45 into main Jul 26, 2026
15 checks passed
@bpalermo
bpalermo deleted the fix/554-race-pod-terminating branch July 26, 2026 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

flaky: TestHandlePodTerminatingPoolClosePhase fails under -race on main

1 participant