From 9f0e0e8331638fc825dedd8b88c2ca63f742d96d Mon Sep 17 00:00:00 2001 From: Matthew Heon Date: Tue, 21 Mar 2023 09:57:56 -0400 Subject: [PATCH] Fix database locked errors with SQLite I was searching the SQLite docs for a fix, but apparently that was the wrong place; it's a common enough error with the Go frontend for SQLite that the fix is prominently listed in the API docs for go-sqlite3. Setting cache mode to 'shared' and using a maximum of 1 simultaneous open connection should fix. Performance implications of this are unclear, but cache=shared sounds like it will be a benefit, not a curse. [NO NEW TESTS NEEDED] This fixes a flake with concurrent DB access. Signed-off-by: Matthew Heon --- libpod/sqlite_state.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libpod/sqlite_state.go b/libpod/sqlite_state.go index 9082e736e5..7b9244913e 100644 --- a/libpod/sqlite_state.go +++ b/libpod/sqlite_state.go @@ -45,7 +45,7 @@ func NewSqliteState(runtime *Runtime) (_ State, defErr error) { return nil, fmt.Errorf("creating root directory: %w", err) } - conn, err := sql.Open("sqlite3", filepath.Join(basePath, "db.sql?_loc=auto")) + conn, err := sql.Open("sqlite3", filepath.Join(basePath, "db.sql?_loc=auto&cache=shared")) if err != nil { return nil, fmt.Errorf("initializing sqlite database: %w", err) } @@ -57,6 +57,9 @@ func NewSqliteState(runtime *Runtime) (_ State, defErr error) { } }() + // Necessary to avoid database locked errors. + conn.SetMaxOpenConns(1) + state.conn = conn if err := state.conn.Ping(); err != nil {