Repository navigation
Studio: gate auth.db and rag.db opens and closes against the SQLite 3.51.0-3.51.1 deadlock - #13307
Conversation
….51.0-3.51.1 deadlock
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex security review |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
The red here is not from this PR. This run merged onto main at
A rerun replays the same merge commit, so updating the branch onto current main should clear both. |
Fixes #9008
Problem
SQLite 3.51.0 and 3.51.1 can deadlock when one thread closes a WAL connection while another thread opens or closes a connection to the same file. In those releases
unixLockcallsunixIsSharingShmNode, which takes the process-wide VFS mutex while holding the file's inode mutex.unixCloseandfindReusableFdtake the same two mutexes in the opposite order. SQLite 3.51.2 removed the VFS mutex fromunixIsSharingShmNode.#10069 serialized opens and closes for
studio.db, butauth.dbandrag.dbstill open with a baresqlite3.connect. Since #9234, every authenticated request readsauth.dbfrom the threadpool, so two concurrent requests are enough to race. Once two threads deadlock on any one file, the VFS mutex stays held, and every database in the process hangs with it. This matches the stacks in #9008: threads parked insqlite3.connect()/close()acrossauth.db,studio.dbandrag.db, with 0% CPU.Changes
storage/studio_db.pyinto a smallstorage/sqlite_gate.pysoauth,rag_dbandhub_settingscan use it without importingstudio_db.connect_studio_dbis now an alias forsqlite_gate.connect, so its callers are unchanged.auth/storage.get_connection,rag_db.get_connection,rag_db.get_metadata_connection, and the read-only startupstudio.dbread inutils/hub_settings.py.test_studio_db_factories_serialize_connection_closeto cover theauth.dbandrag.dbopeners.The gate is the same reentrant lock as before, so there is still one lock for the whole process. Only open and close are serialized. Queries and transactions still run concurrently.
Verification
I compiled the SQLite 3.51.1 and 3.51.2 amalgamations as
libsqlite3.so.0and loaded each into Python 3.13 withLD_LIBRARY_PATH. Then I ran N threads that each loop the real backend opener, run one query and close, and flagged a deadlock if no thread made progress for 10 s. Results are 3 runs per cell, 30 s each:auth.storage.get_connection(2)auth.storage.get_connection(4)rag_db.get_connection(8)Controls:
studio_db.get_app_setting, which was already gated by fix(studio): serialize SQLite connection closes (#10022) #10069, stays clean on 3.51.1 on main.When it deadlocks on main, the faulthandler stacks sit at
rag_db.py:315(sqlite3.connect) and atconn.close(), the same frames as in the issue.auth.dbthroughput with 2 threads is unchanged: about 3.7k open/query/close cycles per second on both sides.auth/storage.pyandrag_db.pyreverted, the extended test fails (assert 10 >= (2 * 8)).ruff checkandscripts/verify_import_hoist.pypass.