Skip to content

The only tool you need to know what is happening and how to fix it.

fix(db): cap in-memory SQLite at a single connection (#235)

FrameAutomata1f1cf9cc9725
diff --git a/backend/app/db/db_sqlite.go b/backend/app/db/db_sqlite.go
index 5e084add..ca12e3aa 100644
--- a/backend/app/db/db_sqlite.go
+++ b/backend/app/db/db_sqlite.go
@@ -99,7 +99,9 @@ func openSQLite(path string, telemetry bool) (*sql.DB, error) {
}
}
- if telemetry {
+ if path == ":memory:" {
+ d.SetMaxOpenConns(1)
+ } else if telemetry {
// WAL allows concurrent readers; SQLite still serializes writes at the
// file level and busy_timeout absorbs short contention windows.
d.SetMaxOpenConns(4)
diff --git a/backend/app/db/db_sqlite_pool_test.go b/backend/app/db/db_sqlite_pool_test.go
new file mode 100644
index 00000000..8afde0b9
--- /dev/null
+++ b/backend/app/db/db_sqlite_pool_test.go
@@ -0,0 +1,51 @@
+//go:build !pgch
+
+package db
+
+import (
+ "context"
+ "sync"
+ "testing"
+)
+
+func TestOpenSQLite_MemoryTelemetryTableVisibleUnderConcurrency(t *testing.T) {
+ d, err := openSQLite(":memory:", true)
+ if err != nil {
+ t.Fatalf("openSQLite: %v", err)
+ }
+ defer d.Close()
+
+ if _, err := d.Exec("CREATE TABLE probe (id INTEGER)"); err != nil {
+ t.Fatalf("create table: %v", err)
+ }
+
+ const goroutines = 32
+ const iterations = 50
+ ctx := context.Background()
+
+ var wg sync.WaitGroup
+ errCh := make(chan error, goroutines)
+ for g := 0; g < goroutines; g++ {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ for i := 0; i < iterations; i++ {
+ if _, err := d.ExecContext(ctx, "INSERT INTO probe (id) VALUES (1)"); err != nil {
+ errCh <- err
+ return
+ }
+ var n int
+ if err := d.QueryRowContext(ctx, "SELECT count(*) FROM probe").Scan(&n); err != nil {
+ errCh <- err
+ return
+ }
+ }
+ }()
+ }
+ wg.Wait()
+ close(errCh)
+
+ for err := range errCh {
+ t.Fatalf("concurrent op hit a pooled connection without the table (separate in-memory DB): %v", err)
+ }
+}