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:
1 parent
57b4d63bc4
commit
478ddd95ae
1 file changed
+3
-1
@@ -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")
|
||||
|
||||
Reference in new issue
Block a user