carlos / platform Public

tests: turn the store's background passes off in HA activator tests

CI's race step caught litestream v0.5.14 closing a store while its own
snapshot goroutine still read the database: with the store ready inside
the wake (#1290), the Open snapshot pass now overlaps haService's
teardown. lease.SetStoreMonitorsOffForTest (counted, process-global)
turns compaction, snapshot and retention passes off; haService uses it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev dd6765d0496cd2dc0b6d451b4706c36056b8704e parent 8fce664
2 files changed, +27 −0
  • internal/activator/harelease_test.go +3 −0
  • internal/lease/lease.go +24 −0
diff --git a/internal/activator/harelease_test.go b/internal/activator/harelease_test.go
index eac8136f6..a566322b0 100644
--- a/internal/activator/harelease_test.go
+++ b/internal/activator/harelease_test.go
@@ -124,6 +124,9 @@ type haSvc struct {
func haService(t *testing.T, host string) *haSvc {
t.Helper()
+ // The store's background passes off (lease.SetStoreMonitorsOffForTest):
+ // they would race the request counts and the teardown below.
+ t.Cleanup(lease.SetStoreMonitorsOffForTest())
b, rt := haBackend(t, host)
h := haFixOf(b)
// A short run directory, so the unix sockets beneath it fit whatever
diff --git a/internal/lease/lease.go b/internal/lease/lease.go
index c963b9398..1ed56bbef 100644
--- a/internal/lease/lease.go
+++ b/internal/lease/lease.go
@@ -545,6 +545,12 @@ func (h *Held) newStore(dbPath string, syncInterval time.Duration) (*litestream.
h.mu.Lock()
oneShot := h.oneShot
h.mu.Unlock()
+ if storeMonitorsOff.Load() > 0 {
+ // SetStoreMonitorsOffForTest: no background compaction, snapshot
+ // or retention pass.
+ store.CompactionMonitorEnabled = false
+ store.L0RetentionCheckInterval = 0
+ }
if oneShot {
// A checkpoint writes its L0 sync and nothing else. Each monitor
// runs one pass at store.Open, concurrently with the sync, and
@@ -1034,6 +1040,24 @@ func SetShutdownSyncTimeoutForTest(d time.Duration) (restore func()) {
return func() { shutdownSyncTimeout.Store(prev) }
}
+// storeMonitorsOff counts the callers that turned off the background
+// passes of stores created from now on (SetStoreMonitorsOffForTest).
+var storeMonitorsOff atomic.Int32
+
+// SetStoreMonitorsOffForTest turns off the background compaction,
+// snapshot and L0 retention passes of the stores of replicators created
+// from now on, until every caller has run its restore. A test seam: with
+// the store ready inside a wake (replicate before start, #1290), those
+// passes can run while a test counts the wake's requests or tears its
+// replicator down, and litestream v0.5.14 lets a store close while its
+// own snapshot is still reading the database (a race the race detector
+// reports). Process-global, counted, so parallel callers nest.
+func SetStoreMonitorsOffForTest() (restore func()) {
+ storeMonitorsOff.Add(1)
+ var once sync.Once
+ return func() { once.Do(func() { storeMonitorsOff.Add(-1) }) }
+}
+
// Restore pulls the newest replicated state of s3://bucket/prefix into
// dbPath via litestream's restore API — the cold-wake path when the local DB
// is missing. prefix is the instance's location: the replica is the