From 478ddd95ae0f1a054037cc3e8a666b992c481396 Mon Sep 17 00:00:00 2001 From: iamdoubz <> Date: Thu, 5 Nov 2026 16:24:01 -0600 Subject: [PATCH] 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. --- parentcalendar/db.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/parentcalendar/db.py b/parentcalendar/db.py index dde3490..5c14d79 100644 --- a/parentcalendar/db.py +++ b/parentcalendar/db.py @@ -12,7 +12,9 @@ BOOL_COLUMNS = {"double_read", "all_day", "applies", "include", "no_lunch"} 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.execute("PRAGMA foreign_keys = ON") conn.execute("PRAGMA journal_mode = WAL")