carlos / platform Public

review: Astra brief for race-activator-1249, round 4

Refs #1249
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 377e3702ac60008a35662ad750b7655feae99eff parent 5b4c932
1 file changed, +288 −0
  • docs/superpowers/reviews/REVIEW-BRIEF-race-activator-1249-r4.md +288 −0

CI cancelled — superseded by a newer push — run details

diff --git a/docs/superpowers/reviews/REVIEW-BRIEF-race-activator-1249-r4.md b/docs/superpowers/reviews/REVIEW-BRIEF-race-activator-1249-r4.md
new file mode 100644
index 000000000..3c541689a
--- /dev/null
+++ b/docs/superpowers/reviews/REVIEW-BRIEF-race-activator-1249-r4.md
@@ -0,0 +1,288 @@
+# Review brief: branch race-activator-1249, round 4 of 4 (final, on Paul's go)
+
+Repo carlos/platform, branch `race-activator-1249`, commit 5b4c932a407c92993b43d1fa1090d1127bc926ca.
+Under review: the diff `origin/main...5b4c932a407c92993b43d1fa1090d1127bc926ca`, mainly what changed since
+round 3 (71470f23, which merged main into 3579a282). Authorities: amadan
+#1249 (the last three posts), AGENTS.md ("The census", "Stopping rules",
+the `70-race` paragraph). Paul approved this one further round on
+2026-11-19 ("1 is good"). There is no round 5: what you find is fixed
+or cut.
+
+The invariant (the fault family across all rounds): every goroutine that
+reads a package-level setting must have finished, with a happens-before
+edge, before a test restores that setting.
+
+## The CI race on 71470f23
+
+The race step (shard internal_activator@2) reported:
+
+```
+WARNING: DATA RACE
+Write by goroutine 2914: setKnob[...].func2() knobrestore_test.go:96 (the restore)
+ runKnobRestores() knobrestore_test.go:129
+Previous read by goroutine 2917:
+ (*Service).awaitWake() activator.go:2094
+ (*Service).begin() activator.go:2024
+ (*Service).beginRequest() activator.go:2061
+ TestTakeBoundOnAFirstWake.func1.3() hatake_test.go:508
+Goroutine 2917 (finished) created at: TestTakeBoundOnAFirstWake.func1() hatake_test.go:504
+```
+
+Reproduced locally before the fix: `go test -race ./internal/activator
+-run TestTakeBoundOnAFirstWake -count=50` gave 2 races in 50 runs (one
+in each subtest, `an instance` through beginRequest, `a worker` through
+WakeWorker).
+
+## Root cause
+
+The goroutine was started by the test, not by production. It called
+beginRequest, which waits on the hand-installed first wake; the test's
+cleanup closes `e.ready`, the waiter reads `wakeWaitBound` in awaitWake
+and ends. `joinProductionGoroutines` saw it gone from the stacks, and
+`syncWithServices` loads `a.background` and takes `a.mu`, but neither
+orders this goroutine's read: it is not a wake (no background
+decrement), and awaitWake's read of the bound comes after its last
+`a.mu` section. So nothing ordered the read before the restore. Round
+3's comment on syncWithServices claimed every reader ends under `a.mu`;
+that holds, if at all, only for goroutines production starts.
+
+## The fix
+
+All in `internal/activator`'s tests; no production file changed.
+
+1. `goTracked(f func())` (knobrestore_test.go) starts f on its own
+ goroutine and records it in `knobRestores.goroutines`. The goroutine's
+ last act (deferred, so also after Goexit) removes itself from that set
+ under knobRestores' lock. The join reads the set under the same lock,
+ so everything such a goroutine did happens before the restores. It
+ takes no `t`: the record carries the call site (file:line) for the
+ leak report, and helpers and fake methods without a `t` can use it.
+ `afterTracked(d, f)` is the same for a timer: tracked from the call,
+ finished after f runs, so a pending timer is waited for too.
+2. `joinProductionGoroutines` is now `joinGoroutines`: it polls until the
+ production stacks are idle AND no goTracked goroutine is running,
+ both together (either may be waiting on the other). Past the 10 s
+ bound it reports each kind (goTracked ones by start site) and
+ abandons them, so one leak fails one test. runKnobRestores is
+ otherwise unchanged: join, endLeaseWatches, join, syncWithServices,
+ restore.
+3. The comments on runKnobRestores and syncWithServices now state the
+ actual rule: each kind of goroutine has its own ordering edge
+ (goTracked's locked exit; Service.leaseWork for lease watches and the
+ kills a loss starts; the background decrement for wakes; a last
+ `a.mu` section for any other goroutine production starts), and a
+ test's goroutine that calls into production must be goTracked because
+ it has no such last act. setKnob's doc and AGENTS.md's `70-race`
+ paragraph say the same.
+
+### Enforcement in one place
+
+`knobguard_test.go`, `TestKnobTestsStartNoUntrackedGoroutine`: an AST
+scan of the package's `*_test.go` files (package `activator`). It
+computes, by name:
+
+- the functions that change a setting: they mention `setKnob`, or a
+ function that does (closure over the test files), which covers rigs
+ and helpers that call setKnob for the test;
+- everything those reach (closure over identifiers in their bodies),
+ plus every method declared in a test file and what it reaches, since
+ production calls fakes' methods from any test.
+
+In that set it fails on any `go` statement, any `time.AfterFunc` call,
+and any `.Go(` method call (`sync.WaitGroup.Go`, errgroup). Reach is by
+identifier name, so function values, interface calls and shared method
+names all count: it over-counts, which only makes it stricter.
+Exemptions are a named map with a reason each (the two helpers;
+setKnob's own probe `trySetKnob`; `endLeaseWatches`' waiter; two fake
+leases' expiry timers that only mark a fake lease lost; `admitHeld`'s
+stopped bound timer that only cancels a context; three socket servers
+with a fixed response), and the test fails if an exemption goes stale.
+`TestKnobGoroutineGuardFindsUntrackedStarts` runs the scan on a small
+source and checks it finds a direct start, one in a helper reached
+through a rig, an AfterFunc, a WaitGroup.Go and one in a method, and
+not goTracked or a test that changes no setting. Reverting
+hatake_test.go:504 to `go func` makes the guard fail naming that line
+(checked by hand).
+
+Deliberately not added: a refusal, like the Service rule, for a subtest
+that changes a setting while its parent's goTracked goroutine runs. The
+race detector reports an unordered write and read whatever the timing,
+and the subtest's restore joins the parent's goroutine (or reports it at
+the bound), so that case is already loud; the Service rule exists
+because a parked watch can wake later and read silently.
+
+### Sweep: converted goroutine starts (116)
+
+112 `go` statements became `goTracked`, and 4 `time.AfterFunc` calls
+became `afterTracked` (none of those timers was stopped). By file and
+function, with the line of each call:
+
+- activator_test.go `TestWorkerTokenIsLiveDuringTheSettleWindow`: 3701
+- activator_test.go `TestWorkerReadyEndsSettleWindowEarly`: 3771
+- backend_exec_test.go `TestAnOlderExitCannotRemoveANewOwnersGroup`: 1210
+- backend_exec_test.go `TestAPendingTransitionRacesARefusal`: 1284
+- backend_exec_test.go `TestSwitchedOffSpawnsTakeNoNewLock`: 1391, 1409
+- backend_unit_test.go `TestUnitTenantWakeServesHeldRequest`: 382
+- childwait_test.go `ctxUntil`: 110
+- cmd_test.go `TestALostCmdResponseStopsTheCmdRoleBeforeReplicationCloses`: 624
+- escrow_linux_test.go `TestEscrowReadRefusesLinkFIFOAndOversize`: 73
+- escrow_linux_test.go `TestFallbackIgnoresARefusedTrioPath`: 124
+- escrow_test.go `TestSweepRacesNoLaunchOrWorker`: 1255
+- hadrain_test.go `TestDrainedDropOutlivesAPausedRead`: 171
+- hadrainmove_test.go `TestDrainQuietWaitsForAnAcquireBeforeItsPUT`: 122
+- hadrainmove_test.go `TestDrainHoldStats`: 186
+- haduties_test.go `TestHADutyVisitorCoalescing`: 229, 235
+- hafiles_test.go `TestTrioPublishedWithinOneSecond`: 115, 164
+- hafiles_test.go `TestIncompleteSnapshotRunsAgainOverAnUnchangedTree`: 247
+- hafiles_test.go `TestPlatformPublishesDuringASnapshot`: 280
+- haforward_test.go `TestForwardTargetStalledReadIsBounded`: 479
+- haleaseloss_test.go `TestHALostRecoveryShutdownDoesNotWaitForWake`: 514
+- haleaseloss_test.go `TestHALostQueuedStopWaiterRefusesOnLoss`: 607
+- handover_test.go `waitAsync`: 40
+- handover_test.go `TestMoveTicketsPausePoints`: 348, 371, 388, 412, 420
+- handover_test.go `TestConfirmMoveHolderCoalesced`: 492
+- haplacement_test.go `TestPlacementSingleFlight`: 243, 247
+- harelease_shutdown_test.go `TestHAShutdownFinalDeadlineReservesMarker`: 95
+- harelease_shutdown_test.go `TestHAShutdownStuckFinishWritesNoMarker`: 122
+- harelease_shutdown_test.go `TestHAShutdownJoinsMarker`: 207
+- harelease_test.go `TestNoWriteAfterMarker`: 475
+- hasecrets_test.go `TestFirstWakeCrashBeforePublishRemints`: 369
+- hasuspect_test.go `(method) concurrently`: 63
+- hasuspect_test.go `TestSuspectWindowRevalidationTimeout`: 616
+- hatake_test.go `TestTakeBoundOnAFirstWake`: 504, 514
+- heldstream_e2e_test.go `(method) connect`: 399
+- holdran_event_test.go `TestAFirstWakeEndedAfterItsExecReportsThatItRan`: 53
+- movestreams_test.go `wantStreamEnded`: 199
+- movestreams_test.go `TestMoveKeepsTheBoundForAnOrdinaryRequest`: 284
+- opset_test.go `plantMember`: 42
+- outcome_fence_test.go `TestAFirstWakeObserverDeliversItsOutcomeBeforeAdmissionReopens`: 223
+- pendingwake_linux_test.go `TestPendingWakeDoesNotBootAStaleCopy`: 275
+- pendingwake_linux_test.go `TestRetirementFencesAPendingWake`: 381
+- pendingwake_linux_test.go `TestUnlabelledWakeFencedWhenLabelledWhileWaitingForTheLease`: 938
+- pendingwake_linux_test.go `(method) shutdownSpent`: 1744
+- rawsecret_test.go `TestEachRawRevisionIsReportedOnce`: 230
+- replicadeploy_test.go `TestReplicaSwapRetiresAfterRoutedRequests`: 634
+- rootfs_cmd_test.go `TestRootfsRunPollsAndStopsTheRootfsUnit`: 429
+- rootfs_ctl_harness_test.go `newRootfsCtlHarness`: 166, 199
+- rootfs_release_backend_test.go `TestReleasePollsTheRootfsCmdUnit`: 183
+- rootfs_resident_test.go `TestResidentRequestAndTickRaceOneWake`: 722
+- rootfs_swap_release_test.go `(method) swapAsync`: 220
+- rootfs_swap_release_test.go `TestSwapReleaseLeaseLossRacesTheSweep`: 760, 761
+- rootfs_wake_release_test.go `(method) wake`: 315
+- rootfsrelease_test.go `(method) run`: 651
+- rootfsrelease_test.go `TestReleaseKeepsRunningWhileTheLeaseRenews`: 1123
+- rootfsrelease_test.go `TestReleaseLeaseLossRacesExit`: 1239, 1240
+- rootfsrelease_test.go `(method) start`: 1608
+- rootfsworkers_backend_test.go `TestRootfsWorkerStopAnswers`: 504
+- seed_rollback_stop_test.go `(method) wake`: 175
+- settle_test.go `TestSwapCanarySettle`: 95
+- settle_test.go `TestSwapIntentionalStopDuringSettleIsNotAFailure`: 120, 127, 137
+- statelessrelease_test.go `prepareReplica`: 109
+- swap_executed_test.go `TestReplaceWorkerLearnsItsRespawnsExecutionWhenItsCallerLeaves`: 314
+- swap_hold_test.go `TestSwapHoldCanarySettles`: 110
+- swap_hold_test.go `TestSwapHoldReleasesItsCallerWhileTheStopIsPending`: 568
+- swap_review_test.go `TestSwapNeverHealthyAppKeepsTheIncumbentServing`: 194, 213
+- swap_sim_test.go `(method) exitLater`: 621
+- swap_sim_test.go `simulateSwapOnce`: 1009, 1026, 1046, 1072, 1095, 1119, 1148
+- wakeplan_test.go `TestFirstWakeWaitersGetTheLongerBudget`: 488, 497
+- wakeplan_test.go `TestAFailedWorkerFirstWakeFallsBackOnlyOnceItsCgroupIsEmpty`: 865
+- wakeplan_test.go `TestAFailedWorkerFirstWakeFallsBackToTheGoodBuild`: 892
+- wakeslots_test.go `TestWakeSlotQueuedFirstWakeStillExecsTheCandidate`: 215
+- workerswap_test.go `TestReplaceWorkerFallsBackToTheGoodBuild`: 72
+- workerswap_test.go `TestACrashInsideAReplaceWorkerObservationIsCoalesced`: 115
+- workerswap_test.go `TestReplaceWorkerInHoldModeStaysDown`: 149
+- workerswap_test.go `TestReplaceWorkerRestartFailureRespawnsTheServingBuild`: 174
+- workerswap_test.go `TestReplaceWorkerOneShotCleanExitIsReady`: 196
+- workerswap_test.go `TestWakeWorkerWaitsOutAFirstWakeFallback`: 262
+- workerswap_test.go `TestReplaceWorkerReChecksAtTheSpawnAndBeforeTheResult`: 302, 332
+- workerswap_test.go `TestReplaceWorkerWithoutOwnershipSpawnsNothing`: 448
+- workerswap_test.go `TestReplaceWorkerAnIntentionalStopDuringTheObservationIsNoVerdict`: 517
+- workerswap_test.go `TestALateWakePublicationNeverOverwritesItsReplacement`: 632
+- workerswap_test.go `TestAWorkerFallbackWithoutOwnershipSpawnsNothing`: 693
+- workerswap_test.go `TestAWorkerFallbackPublishesOnlyWhenReleased`: 769, 775
+- workerswap_test.go `TestAFallbackWakeRecordsTheFailureBeforeItPublishes`: 826, 831
+- workerswap_test.go `TestAnExitedOneShotIsGoodOnlyAfterACleanPark`: 960
+- writefence_test.go `TestWriteFenceRequestAndWatcherStopOnce`: 303
+- writefence_test.go `TestWriteFenceDuringABlockedPublisherOpen`: 487, 533
+- writefence_test.go `TestWriteFenceWakingAnswersAtOnce`: 689
+- writefence_test.go `TestWriteFenceWaitingCallerGetsTheLossAnswer`: 735
+- writefence_test.go `TestWriteFenceWatcherEndsOnCleanHibernation`: 752
+
+Out of scope, by the guard: the remaining `go` statements in test code
+no setting-changing test reaches (parallel tests, pure helpers).
+
+## Test results
+
+Run on docs-t1 (shared, loaded), `TMPDIR=/tmp/g` for the socket tests.
+
+- Repro before the fix (71470f23 merged with main 365a79ca):
+ `go test -race ./internal/activator -run TestTakeBoundOnAFirstWake
+ -count=50`: FAIL, 2 data races (`an instance` and `a worker`).
+- The same after the fix: ok (168.7 s), 0 races.
+- Full package under -race, one race-built binary, the 2043 top-level
+ tests split alternately into two halves:
+ - half 1 (1022 tests, 27.6 min): 0 races, 0 goTracked leaks, one
+ failure: `TestWakeWorkerAWakeFailingInsideTheExtensionStartsNoSecond`
+ ("the waiter never extended its bound", a 50 ms wakeWaitBound under
+ load; the join then reported the wake the test never released). The
+ test is untouched by this branch and starts no goroutine of its own;
+ it passed 20 of 20 under -race on rerun.
+ - half 2 (1021 tests, 22.3 min): PASS, 0 races.
+- The halves ran with the four timer sites as goTracked plus a sleep;
+ code-health refused the new sleeps, so they became `afterTracked`
+ (a tracked time.AfterFunc). Their tests rerun under -race after that:
+ `TestFirstWakeWaitersGetTheLongerBudget`,
+ `TestWakeWorkerWaitsOutAFirstWakeFallback`, `TestSwapInvariants`,
+ `TestSimDeathIsOneTransition`, `TestTakeBoundOnAFirstWake` and both
+ guard tests: PASS.
+- Guard negative check: putting hatake_test.go:504 back to `go func`
+ fails `TestKnobTestsStartNoUntrackedGoroutine` naming that line.
+- `go vet ./internal/activator`, gofmt: clean.
+- `go test ./internal/provision -run Census`: ok.
+- `make code-health`: PASS (no record change).
+- `sh .amadan/ci.d/80-gitleaks`: clean.
+
+## What to check
+
+1. The invariant now holds for goroutines tests start: is there a test
+ goroutine, in code a setting-changing test reaches, that can still
+ read a setting after its test's restore without an ordering edge? In
+ particular: the locked exit and the locked read of the set are a real
+ happens-before chain, including when the goroutine leaves the set
+ before the join first looks (no edge is lost to another reader).
+2. The guard cannot be bypassed by an ordinary test: a test or rig that
+ changes a setting and starts a goroutine by a means the scan misses
+ (a starter outside this package's test files, a goroutine started
+ inside a package-level func value, a method value passed to
+ production, a build-tagged file). Is the name-based reach sound as
+ an over-approximation, and are the exemptions each true?
+3. The join cannot now hang where it did not before: a goTracked
+ goroutine that only ends when a cleanup registered before the first
+ setKnob runs would wait out the 10 s bound. Both full -race halves
+ ran; say if you see such an ordering the runs could have missed.
+4. No production behaviour change: `git diff origin/main...5b4c932a407c92993b43d1fa1090d1127bc926ca --
+ ':!*_test.go' ':!docs' ':!AGENTS.md'` is unchanged from round 3
+ (leaseWatchStop and leaseWork, already approved), so no census unit
+ changed in this round.
+5. The new comments on runKnobRestores and syncWithServices state the
+ rule correctly, without the round 3 overclaim.
+
+## Census
+
+No production file changed since round 3, so the round 3 seal stands
+(`docs/superpowers/notes/census-proposal-race-activator-1249.md`, approved
+in round 3: `type Service`, `Service.residentLeaseShort`, the
+`internal/activator` envelope). `go test ./internal/provision -run Census`
+passes (see results).
+
+## Severity
+
+- **P1**: unsafe, would not compile, a test fails, or it would not work.
+- **P2**: wrong or missing. Fixed without reopening this review.
+- **P3**: clarity or polish. Follow-up list only.
+
+## Output
+
+Findings ranked P1, P2, P3, each with file:line, the failure scenario and
+the fix. End with exactly one of: READY, READY AFTER FIXES, NOT READY.
+Do not edit files, spawn reviewers or run full suites.