Allow SQLite connections to cross FastAPI worker threads

FastAPI can open a request's connection in one threadpool thread and run the endpoint in another, which SQLite rejects by default and which surfaced as intermittent 500s. Each connection still serves one request at a time.
This commit is contained in:
iamdoubz committed 2026-11-05 16:24:01 -06:00
1 parent 57b4d63bc4
commit 478ddd95ae
1 file changed
+3 -1
+3 -1
View File
@@ -12,7 +12,9 @@ BOOL_COLUMNS = {"double_read", "all_day", "applies", "include", "no_lunch"}
def connect(path=None): def connect(path=None):
conn = sqlite3.connect(path or config.DB_PATH, timeout=30) # FastAPI may open a request's connection in one worker thread and run the endpoint in another.
# Each connection still serves one request (or the worker) at a time, so crossing threads is safe.
conn = sqlite3.connect(path or config.DB_PATH, timeout=30, check_same_thread=False)
conn.row_factory = sqlite3.Row conn.row_factory = sqlite3.Row
conn.execute("PRAGMA foreign_keys = ON") conn.execute("PRAGMA foreign_keys = ON")
conn.execute("PRAGMA journal_mode = WAL") conn.execute("PRAGMA journal_mode = WAL")