Repository navigation
Description
Summary
TestActuator_PerAZPlacementGroup failed intermittently in CI (PR #513's first run, unrelated to that PR's actual diff): AZ-b PG created 2 times, want 1. Passed 5/5 locally (go test ./pkg/mpicohort/... -run TestActuator_PerAZPlacementGroup -count=5), consistent with a race that CI's more heavily loaded/scheduled runner exposed and a quiet laptop didn't.
Not just test flakiness — the test is correctly catching a real bug in the source under test.
Root cause
Actuator.ensurePlacementGroup (pkg/mpicohort/adapter.go):
func (a *Actuator) ensurePlacementGroup(ctx context.Context, az string) (string, error) {
name := PlacementGroupName(a.PlacementGroupPrefix, az)
a.pgMu.Lock()
if a.pgCreated == nil {
a.pgCreated = make(map[string]struct{})
}
_, done := a.pgCreated[az]
a.pgMu.Unlock() // <-- lock released here
if done {
return name, nil
}
if err := a.Client.CreatePlacementGroup(ctx, name, a.Region); err != nil { // <-- unguarded gap
return "", fmt.Errorf("create placement group %q: %w", name, err)
}
a.pgMu.Lock()
a.pgCreated[az] = struct{}{}
a.pgMu.Unlock()
return name, nil
}This is a classic check-then-act race: the lock is released between checking pgCreated[az] and calling CreatePlacementGroup. When multiple cohort members in the same AZ call ensurePlacementGroup concurrently (the normal case — a round launches N members at once, per TestActuator_PerAZPlacementGroup's own 4-member cohort), more than one can observe done == false before any of them has recorded success, and all of them call CreatePlacementGroup.
The doc comment on the interface says this is supposed to be safe (pkg/mpicohort/adapter.go:37: "CreatePlacementGroup is idempotent (already-exists is success)..."), and real EC2's CreatePlacementGroup likely tolerates a benign AlreadyExists-style double-call (worth confirming). But the test's fake launcher doesn't model that idempotency — it just increments a counter — so the test is stricter than the interface's own contract, and either:
- The test is asserting a real invariant (
ensurePlacementGroup's docstring says "creates ... at most once per AZ ... to avoid the redundant 30s availability poll for every member of a round") that the implementation doesn't actually uphold under concurrency, or - The test needs to tolerate concurrent-safe double-calls if AWS's own idempotency is what's meant to paper over this.
Given the docstring explicitly frames the once-per-AZ behavior as a performance optimization (skip the "redundant 30s availability poll"), (1) seems right: the race means concurrent members in a newly-visited AZ can EACH pay that 30s poll, defeating the purpose of ensurePlacementGroup existing at all — not just a cosmetic double-call.
Suggested fix
Hold the lock across the whole check-and-create (or use a sync.Once/singleflight per AZ) so only one goroutine actually calls CreatePlacementGroup for a given AZ, and the rest block until it's done:
func (a *Actuator) ensurePlacementGroup(ctx context.Context, az string) (string, error) {
name := PlacementGroupName(a.PlacementGroupPrefix, az)
a.pgMu.Lock()
defer a.pgMu.Unlock()
if a.pgCreated == nil {
a.pgCreated = make(map[string]struct{})
}
if _, done := a.pgCreated[az]; done {
return name, nil
}
if err := a.Client.CreatePlacementGroup(ctx, name, a.Region); err != nil {
return "", fmt.Errorf("create placement group %q: %w", name, err)
}
a.pgCreated[az] = struct{}{}
return name, nil
}Trade-off: this serializes all concurrent launches in a newly-visited AZ behind the first CreatePlacementGroup call (holding the lock across a network call) — acceptable here since it only affects the first caller per AZ, but worth a second look if CreatePlacementGroup's latency is high or unbounded. A per-AZ sync.Once/singleflight would avoid serializing unrelated AZs against each other while still deduping same-AZ callers, if the simple mutex-holding version proves too coarse.
Environment
Found via CI flake on spawn PR #513, 2026-08-18 (Test job, run 32108566102). Confirmed NOT reproducible with -count=5 locally — consistent with a genuine race rather than deterministic logic error, exposed by CI's scheduling/load rather than local test iteration alone.
Activity
fix: coalesce concurrent per-AZ placement group creation in mpicohort