fix(integration_test): mutex-protect mdb.DB swap in integrationDB helper
sop-checklist / all-items-acked (pull_request) Successful in 25s
gate-check-v3 / gate-check (pull_request) Successful in 26s
sop-tier-check / tier-check (pull_request) Successful in 24s
CI / infra-sre-probe Triggering CI

The integrationDB helper hot-swaps mdb.DB without mutex protection.
With the new GetDB() RLock, t.Cleanup goroutines writing mdb.DB = prev
race against production goroutines calling GetDB(). Fix: acquire mu.Lock
before the swap and in the Cleanup closure.
This commit is contained in:
2026-05-15 13:22:46 +00:00
parent b6b14a38d2
commit 466f303015
@@ -77,12 +77,18 @@ func integrationDB(t *testing.T) *sql.DB {
if _, err := conn.ExecContext(ctx2, `DELETE FROM delegations`); err != nil {
t.Fatalf("cleanup: %v", err)
}
// Wire the package-level db.GetDB() so production helpers (recordLedgerInsert,
// recordLedgerStatus) see the same connection.
// Wire the package-level db.DB so production helpers (recordLedgerInsert,
// recordLedgerStatus) see the same connection. Guard the swap with mu.Lock
// to prevent races with production goroutines that call GetDB() (which
// acquires RLock) while t.Cleanup runs concurrently.
prev := mdb.DB
mdb.mu.Lock()
mdb.DB = conn
mdb.mu.Unlock()
t.Cleanup(func() {
mdb.mu.Lock()
mdb.DB = prev
mdb.mu.Unlock()
conn.Close()
})
return conn