Artifact 4e57b0e3e9327e161d0b932efa97b91ab9fec199d2581385c52a425aa98250de:
- File
db/driver/modernc/modernc.go
— part of check-in
[202576c58c]
at
2026-05-13 19:09:49
on branch trunk
— fix: DSN _txlock=immediate to serialize writers at BEGIN (#33) (#34)
Two concurrent libfossil.Repo handles on the same SQLite file fail their writes with SQLITE_BUSY immediately, well under the configured busy_timeout=5000 window. Reproduces ~50% of the time on contended manifest.Checkin paths.
Root cause: db.WithTx opens BEGIN DEFERRED. Under WAL, a deferred transaction acquires SHARED on first read, then upgrades SHARED→RESERVED on first write. If two connections race that upgrade, SQLite's deadlock-avoidance returns SQLITE_BUSY *immediately*, bypassing busy_timeout entirely (documented behavior, sqlite.org/c3ref/busy_timeout.html).
Fix: prepend _txlock=immediate to the DSN in both modernc and ncruces drivers. Every BEGIN now acquires RESERVED upfront, so the second writer blocks at BEGIN — where busy_timeout *does* apply — and the two transactions serialize cleanly.
Regression tests added to both driver packages: - TestBuildDSN_TxlockImmediate covers the DSN shape across empty/non-empty pragma maps. - TestConcurrentWritersDoNotRace reproduces the issue-#33 contention pattern (two conns, SELECT then INSERT under DEFERRED) over 25 iterations and asserts zero SQLITE_BUSY. Without the fix, this test fails 25/25; with the fix, 0/25.
Closes #33. (user: [email protected], size: 872) [annotate] [blame] [check-ins using]
package modernc import ( "fmt" "strings" "github.com/danmestas/libfossil/db" _ "modernc.org/sqlite" ) func init() { db.Register(db.DriverConfig{ Name: "sqlite", BuildDSN: buildDSN, }) } func buildDSN(path string, pragmas map[string]string) string { if path == "" { panic("modernc.buildDSN: path must not be empty") } // _txlock=immediate forces BEGIN IMMEDIATE so concurrent writers // serialize at BEGIN (where busy_timeout applies) instead of racing // on the SHARED→RESERVED upgrade (where SQLite returns SQLITE_BUSY // immediately to avoid deadlock, bypassing busy_timeout entirely). // See https://www.sqlite.org/c3ref/busy_timeout.html parts := []string{"_txlock=immediate"} for k, v := range pragmas { parts = append(parts, fmt.Sprintf("_pragma=%s(%s)", k, v)) } return fmt.Sprintf("file:%s?%s", path, strings.Join(parts, "&")) }