Live push 1b: the log epoch's transaction writes first (#175 approval, finding 1)
rastrillo opens SQLite with deferred transactions, so loadLogEpoch's transaction read the row and only took the write lock at Save: two starts on one database (an overlapped deploy) that both read before one committed failed with a busy error busy_timeout does not retry, and New refused to boot, with DOCS_LIVE_PUSH off too. The transaction's first statement is now a write (INSERT ... ON CONFLICT DO NOTHING of the row), so it waits for the write lock before reading; an empty epoch marks a row no start has stamped. TestLogEpochTwoConcurrentStarts: 20 rounds of two concurrent New on one database. Failed in round 1 before (database is locked, SQLITE_BUSY).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+53
−1
internal/docs/logepoch.go+12 −1internal/docs/push_test.go+41 −0
diff --git a/internal/docs/logepoch.go b/internal/docs/logepoch.go| index c1cbace..7dc0279 100644 |
| --- a/internal/docs/logepoch.go |
| +++ b/internal/docs/logepoch.go |
| @@ -76,10 +76,21 @@ func loadLogEpoch(ctx context.Context, db *gorm.DB, dbPath string) (string, erro |
| // has a lower boot and rotates. |
| var row logEpochRow |
| err := db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { |
| + // The first statement writes, so the transaction takes SQLite's |
| + // write lock before it reads anything. rastrillo's transactions are |
| + // deferred: one that read first and wrote later would fail with |
| + // SQLITE_BUSY_SNAPSHOT, which busy_timeout does not retry, whenever |
| + // another start (an overlapped deploy) committed in between; the |
| + // write lock is waited for instead (#175 approval finding 1). It |
| + // also makes the row exist: an empty epoch is a database no start |
| + // has stamped yet. |
| + if err := tx.Exec("INSERT INTO docs_log_epoch (id, epoch, boot) VALUES (1, '', 0) ON CONFLICT(id) DO NOTHING").Error; err != nil { |
| + return fmt.Errorf("docs: claim log epoch: %w", err) |
| + } |
| if err := tx.Where("id = 1").Limit(1).Find(&row).Error; err != nil { |
| return fmt.Errorf("docs: read log epoch: %w", err) |
| } |
| - keep := row.ID == 1 && fileEpoch == row.Epoch && (fileBoot == row.Boot || fileBoot == row.Boot-1) |
| + keep := row.Epoch != "" && fileEpoch == row.Epoch && (fileBoot == row.Boot || fileBoot == row.Boot-1) |
| if !keep { |
| epoch, err := newLogEpoch() |
| if err != nil { |
diff --git a/internal/docs/push_test.go b/internal/docs/push_test.go| index 632bd3f..4828d86 100644 |
| --- a/internal/docs/push_test.go |
| +++ b/internal/docs/push_test.go |
| @@ -1386,3 +1386,44 @@ func TestTeamStreamNeverOpensWithoutALease(t *testing.T) { |
| t.Fatalf("%+v, want bye lease: the stream opened without a lease deadline", fr) |
| } |
| } |
| + |
| +// #175 approval finding 1: rastrillo's SQLite transactions are deferred, so |
| +// a transaction that reads first and writes later fails with |
| +// SQLITE_BUSY_SNAPSHOT (which busy_timeout does not retry) when another |
| +// connection commits in between. Two processes starting on one database at |
| +// once (an overlapped deploy) must both start. |
| +func TestLogEpochTwoConcurrentStarts(t *testing.T) { |
| + dir := t.TempDir() |
| + cfg := Config{DBPath: filepath.Join(dir, "docs.db"), Env: "development"} |
| + first, err := New(cfg) // migrations applied once |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + first.Close() |
| + for round := 0; round < 20; round++ { |
| + var wg sync.WaitGroup |
| + errs := make(chan error, 2) |
| + apps := make(chan *App, 2) |
| + for i := 0; i < 2; i++ { |
| + wg.Add(1) |
| + go func() { |
| + defer wg.Done() |
| + app, err := New(cfg) |
| + if err != nil { |
| + errs <- err |
| + return |
| + } |
| + apps <- app |
| + }() |
| + } |
| + wg.Wait() |
| + close(errs) |
| + close(apps) |
| + for app := range apps { |
| + app.Close() |
| + } |
| + for err := range errs { |
| + t.Fatalf("round %d: a concurrent start failed: %v", round, err) |
| + } |
| + } |
| +} |