mirror of
https://tangled.org/evan.jarrett.net/at-container-registry
synced 2026-09-26 04:04:15 +00:00
A PRAGMA is per-connection, and database/sql opens one connection per concurrent caller. hold_db.go and scan_broadcaster.go each set busy_timeout = 5000 with a one-shot query against the pool, which configures whichever connection happened to serve it and leaves every other writer at zero. Probing the live pools showed exactly that: conn 0 at 5000, conns 1 and 2 at 0. That is the database is locked on COMMIT, and the carstore had no busy_timeout and no WAL on any connection at all. Opening a database now goes through a connector that runs the PRAGMA on every connection the pool creates, ported from the appview, which had already solved this. Applied to the carstore, the shared hold DB, the records index, the event broadcaster and the scan broadcaster. In-memory databases are left unwrapped, since libsql gives each connection its own, and the embedded replica path still skips PRAGMAs because a remote rejects them. WAL is not a new risk: production was already WAL, because journal mode is a persistent property of the file and OpenHoldDB has been setting it. This makes the carstore's own opener agree rather than leaving standalone and test databases in rollback-journal mode. A database that refuses WAL logs a warning instead of failing boot, so a network mount cannot stop the hold starting. NewHoldPDS's file mode opened the same file a second time for the records index; it now shares the carstore's pool. Two pools on one file buy nothing, since SQLite's write lock is per database, so the second only adds contenders. Production already shared a pool and was unaffected by that half. Note for anyone reading OpenHoldDB: its foreign_keys pragma has the same one-connection scope, but libsql reports foreign_keys on for every fresh connection anyway, so it is decorative rather than broken. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01U1Km3N3uUmeGaj7VbaM8PF
94 lines
2.6 KiB
Go
94 lines
2.6 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
// assertPoolSettings checks journal mode and busy_timeout on several
|
|
// simultaneously held connections. Holding them at once forces database/sql to
|
|
// open distinct connections: busy_timeout is per-connection, so a regression to
|
|
// a one-shot "PRAGMA busy_timeout" on the pool shows up on conn 1 onward.
|
|
func assertPoolSettings(t *testing.T, label string, db *sql.DB, wantJournal string, wantBusy int) {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
|
|
const n = 4
|
|
conns := make([]*sql.Conn, 0, n)
|
|
defer func() {
|
|
for _, c := range conns {
|
|
_ = c.Close()
|
|
}
|
|
}()
|
|
for i := 0; i < n; i++ {
|
|
c, err := db.Conn(ctx)
|
|
if err != nil {
|
|
t.Fatalf("%s: open conn %d: %v", label, i, err)
|
|
}
|
|
conns = append(conns, c)
|
|
}
|
|
|
|
for i, c := range conns {
|
|
var journal string
|
|
if err := c.QueryRowContext(ctx, "PRAGMA journal_mode").Scan(&journal); err != nil {
|
|
t.Fatalf("%s conn %d: read journal_mode: %v", label, i, err)
|
|
}
|
|
if journal != wantJournal {
|
|
t.Errorf("%s conn %d: journal_mode = %q, want %q", label, i, journal, wantJournal)
|
|
}
|
|
|
|
var busy int
|
|
if err := c.QueryRowContext(ctx, "PRAGMA busy_timeout").Scan(&busy); err != nil {
|
|
t.Fatalf("%s conn %d: read busy_timeout: %v", label, i, err)
|
|
}
|
|
if busy != wantBusy {
|
|
t.Errorf("%s conn %d: busy_timeout = %d, want %d", label, i, busy, wantBusy)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestOpenLocalDBSettingsOnEveryConnection(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "db.sqlite3")
|
|
db, err := OpenLocalDB(path)
|
|
if err != nil {
|
|
t.Fatalf("OpenLocalDB: %v", err)
|
|
}
|
|
defer db.Close()
|
|
assertPoolSettings(t, "OpenLocalDB", db, "wal", DefaultBusyTimeoutMs)
|
|
}
|
|
|
|
func TestOpenHoldDBSettingsOnEveryConnection(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "db.sqlite3")
|
|
h, err := OpenHoldDB(path, LibsqlConfig{})
|
|
if err != nil {
|
|
t.Fatalf("OpenHoldDB: %v", err)
|
|
}
|
|
defer h.Close()
|
|
assertPoolSettings(t, "OpenHoldDB", h.DB, "wal", DefaultBusyTimeoutMs)
|
|
}
|
|
|
|
func TestCarstoreSettingsOnEveryConnection(t *testing.T) {
|
|
sqs, err := NewSqliteStore(t.TempDir())
|
|
if err != nil {
|
|
t.Fatalf("NewSqliteStore: %v", err)
|
|
}
|
|
defer sqs.Close()
|
|
assertPoolSettings(t, "carstore", sqs.DB(), "wal", DefaultBusyTimeoutMs)
|
|
}
|
|
|
|
// TestOpenLocalDBMemory documents that ":memory:" is left unwrapped: libsql
|
|
// gives each connection its own private database, so there is no shared lock to
|
|
// wait on and WAL does not apply.
|
|
func TestOpenLocalDBMemory(t *testing.T) {
|
|
db, err := OpenLocalDB(":memory:")
|
|
if err != nil {
|
|
t.Fatalf("OpenLocalDB(:memory:): %v", err)
|
|
}
|
|
defer db.Close()
|
|
if err := db.Ping(); err != nil {
|
|
t.Fatalf("ping in-memory db: %v", err)
|
|
}
|
|
}
|