From 069416df74b1a560480546a934cfde9ee8161ed7 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 21:37:29 +0000 Subject: [PATCH 1/3] fix(api): begin SQLite read-write transactions with IMMEDIATE lock A deferred SQLite transaction that reads first and writes later cannot wait for the write lock in WAL mode: when another connection committed in between, SQLite returns SQLITE_BUSY at once and never calls the busy handler, so busy_timeout does not help. bootstrap-admin, run through kubectl exec next to a live API, hit this in e2e as 'update user: database is locked (5) (SQLITE_BUSY)' and 'migrate: seed telemetry state: insert: database is locked'. Add _txlock=immediate to the SQLite DSN unless the DSN already sets _txlock, so modernc sqlite opens read-write transactions with BEGIN IMMEDIATE and waits on busy_timeout. Read-only transactions stay deferred. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Claude --- api/internal/db/db.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/api/internal/db/db.go b/api/internal/db/db.go index f0bf20acb..98acf9ff1 100644 --- a/api/internal/db/db.go +++ b/api/internal/db/db.go @@ -56,6 +56,12 @@ func Open(ctx context.Context, driver, dsn string) (*Store, error) { // Add a busy timeout to the DSN if not already present, so concurrent // processes (e.g., API and bootstrap-admin) waiting for locks don't fail immediately. dsn = withSQLiteBusyTimeout(dsn) + // Take the write lock when a read-write transaction begins. A deferred + // transaction that reads first and writes later cannot wait in WAL mode: + // if another process wrote in between, SQLite returns SQLITE_BUSY at once + // and skips busy_timeout (seen with bootstrap-admin next to a live API). + // Read-only transactions stay deferred. + dsn = withSQLiteImmediateTxLock(dsn) db, err := sql.Open("sqlite", dsn) if err != nil { @@ -399,6 +405,20 @@ func withSQLiteBusyTimeout(dsn string) string { return dsn + "&_pragma=busy_timeout(5000)" } +// withSQLiteImmediateTxLock appends _txlock=immediate to the DSN unless it +// already sets _txlock, so the driver opens read-write transactions with +// BEGIN IMMEDIATE and they wait on busy_timeout instead of failing. +func withSQLiteImmediateTxLock(dsn string) string { + idx := strings.IndexByte(dsn, '?') + if idx == -1 { + return dsn + "?_txlock=immediate" + } + if params, err := url.ParseQuery(dsn[idx+1:]); err == nil && params.Has("_txlock") { + return dsn + } + return dsn + "&_txlock=immediate" +} + // adoptLegacySQLite renames kestrel.db to the target DSN path if the target // doesn't exist but the legacy file does. This handles Kestrel → Gameplane // upgrades where the DSN changed from kestrel.db to gameplane.db. From bf95149c1b6a70aa0f4ed33b53d027acf44504c0 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 21:37:53 +0000 Subject: [PATCH 2/3] docs(changelog): add entry for #1028 Co-Authored-By: Claude Opus 5.5 Signed-off-by: Claude --- changelog/unreleased/1028-sqlite-bootstrap-admin-lock.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 changelog/unreleased/1028-sqlite-bootstrap-admin-lock.md diff --git a/changelog/unreleased/1028-sqlite-bootstrap-admin-lock.md b/changelog/unreleased/1028-sqlite-bootstrap-admin-lock.md new file mode 100644 index 000000000..4d2215700 --- /dev/null +++ b/changelog/unreleased/1028-sqlite-bootstrap-admin-lock.md @@ -0,0 +1,6 @@ +# bootstrap-admin no longer fails with "database is locked" on SQLite + +Running `bootstrap-admin` while the API is serving could fail at once with +`database is locked (SQLITE_BUSY)`, even with the busy timeout set. SQLite +read-write transactions now take the write lock when they begin, so they wait +for the lock instead of failing. No action is needed. [PR #1028](https://github.com/GameplanePanel/Gameplane/pull/1028) From 6b4dc32dfa943a0c3f8fa9ac1383482a4db38efb Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 21:42:26 +0000 Subject: [PATCH 3/3] fix(api): append _txlock without new branches withSQLiteBusyTimeout always leaves a query string, and modernc sqlite reads the first _txlock value, so appending &_txlock=immediate keeps a configured _txlock and needs no helper. Drops the untested branches the changed-lines coverage gate flagged. Co-Authored-By: Claude Opus 5.5 Signed-off-by: Claude --- api/internal/db/db.go | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/api/internal/db/db.go b/api/internal/db/db.go index 98acf9ff1..fcf6421e1 100644 --- a/api/internal/db/db.go +++ b/api/internal/db/db.go @@ -60,8 +60,10 @@ func Open(ctx context.Context, driver, dsn string) (*Store, error) { // transaction that reads first and writes later cannot wait in WAL mode: // if another process wrote in between, SQLite returns SQLITE_BUSY at once // and skips busy_timeout (seen with bootstrap-admin next to a live API). - // Read-only transactions stay deferred. - dsn = withSQLiteImmediateTxLock(dsn) + // Read-only transactions stay deferred. withSQLiteBusyTimeout always + // leaves a query string, and the driver reads the first _txlock value, + // so a _txlock already in the configured DSN still wins. + dsn += "&_txlock=immediate" db, err := sql.Open("sqlite", dsn) if err != nil { @@ -405,20 +407,6 @@ func withSQLiteBusyTimeout(dsn string) string { return dsn + "&_pragma=busy_timeout(5000)" } -// withSQLiteImmediateTxLock appends _txlock=immediate to the DSN unless it -// already sets _txlock, so the driver opens read-write transactions with -// BEGIN IMMEDIATE and they wait on busy_timeout instead of failing. -func withSQLiteImmediateTxLock(dsn string) string { - idx := strings.IndexByte(dsn, '?') - if idx == -1 { - return dsn + "?_txlock=immediate" - } - if params, err := url.ParseQuery(dsn[idx+1:]); err == nil && params.Has("_txlock") { - return dsn - } - return dsn + "&_txlock=immediate" -} - // adoptLegacySQLite renames kestrel.db to the target DSN path if the target // doesn't exist but the legacy file does. This handles Kestrel → Gameplane // upgrades where the DSN changed from kestrel.db to gameplane.db.