Skip to content

Studio: gate auth.db and rag.db opens and closes against the SQLite 3.51.0-3.51.1 deadlock - #13307

Merged
danielhanchen merged 3 commits into
mainfrom
studio-sqlite-gate-auth-rag
Oct 11, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
studio-sqlite-gate-auth-rag

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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 unixLock calls unixIsSharingShmNode, which takes the process-wide VFS mutex while holding the file's inode mutex. unixClose and findReusableFd take the same two mutexes in the opposite order. SQLite 3.51.2 removed the VFS mutex from unixIsSharingShmNode.

#10069 serialized opens and closes for studio.db, but auth.db and rag.db still open with a bare sqlite3.connect. Since #9234, every authenticated request reads auth.db from 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 in sqlite3.connect() / close() across auth.db, studio.db and rag.db, with 0% CPU.

Changes

  • Move the open/close gate from storage/studio_db.py into a small storage/sqlite_gate.py so auth, rag_db and hub_settings can use it without importing studio_db. connect_studio_db is now an alias for sqlite_gate.connect, so its callers are unchanged.
  • Open through the gate in auth/storage.get_connection, rag_db.get_connection, rag_db.get_metadata_connection, and the read-only startup studio.db read in utils/hub_settings.py.
  • Extend test_studio_db_factories_serialize_connection_close to cover the auth.db and rag.db openers.

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.0 and loaded each into Python 3.13 with LD_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:

opener (threads) SQLite 3.51.1, main SQLite 3.51.1, this PR
auth.storage.get_connection (2) deadlock 3/3 (after 17-394 ops) clean 3/3 (77k-88k ops)
auth.storage.get_connection (4) deadlock 3/3 clean 3/3
rag_db.get_connection (8) deadlock 3/3 clean 3/3
auth + studio + rag + rag metadata mixed (8) deadlock 3/3 clean 3/3

Controls:

When it deadlocks on main, the faulthandler stacks sit at rag_db.py:315 (sqlite3.connect) and at conn.close(), the same frames as in the issue. auth.db throughput with 2 threads is unchanged: about 3.7k open/query/close cycles per second on both sides.

  • Negative control: with auth/storage.py and rag_db.py reverted, the extended test fails (assert 10 >= (2 * 8)).
  • Related backend suites (auth, account, API key, credential, providers, MCP, library, RAG, hub settings; 112 files): 2209 passed, 31 skipped on both main and this branch.
  • ruff check and scripts/verify_import_hoist.py pass.

@danielhanchen danielhanchen self-assigned this Oct 11, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-11T13:24:48.728546Z f7d0183 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 11, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 11, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 9a87404c87

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: f7d0183a17

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@danielhanchen

Copy link
Copy Markdown
Member Author

The red here is not from this PR. This run merged onto main at 0acf8b1717, which predates #13336. That PR fixed two problems #13308 left on main:

  • test_torchao_fqn_alias.py left a fake diffusers in sys.modules. That causes the AutoencoderKLWan / modular_pipelines / video Regex pattern did not match failures in (Python 3.13, rest).
  • _torchao_stub.py and test_torchao_fqn_alias.py were not formatted. That causes test_formatter_fixed_point in Repo tests (CPU, rest).

A rerun replays the same merge commit, so updating the branch onto current main should clear both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Studio: server deadlocks with every thread blocked in sqlite3.connect() / close() and stops accepting connections

1 participant