Skip to content

fix(studio): serialize SQLite connection closes (#10022) - #10069

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
Biotrioo:fix/10022-studio-sqlite-close-deadlock
Oct 5, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
Biotrioo:fix/10022-studio-sqlite-close-deadlock

Conversation

@Biotrioo

@Biotrioo Biotrioo commented Aug 31, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Serialize opening and closing studio.db connections behind one process-wide lock, so Studio no longer hangs on SQLite 3.51.0 and 3.51.1.

Those SQLite releases can deadlock when one thread closes a WAL connection while another thread opens or closes a connection to the same file: unixIsSharingShmNode takes the VFS mutex while holding the inode mutex, the reverse order of unixClose and findReusableFd. SQLite fixed it in 3.51.2, but Python builds bundling 3.51.0/3.51.1 are still in use (the reporter is on 3.51.1).

Changes

  • storage.studio_db.connect_studio_db opens connections under the lock and returns a sqlite3.Connection subclass whose close() takes the same lock. The lock is reentrant and reset in forked children.
  • Every studio.db opener uses it: studio_db, providers_db, mcp_servers_db, credential_secrets, library_db, and the model grant reads/writes in account_access.
  • Ordinary queries and transactions stay concurrent.

Testing

C stress test (threads looping open, read, close on one WAL file), 3 runs per row:

SQLite gate result
3.51.1 none deadlock 6/6
3.51.1 close only deadlock 5/6
3.51.1 open + close clean 6/6
3.51.2 none clean 3/3
  • test_studio_db_factories_serialize_connection_close checks every factory goes through the gate for both open and close, with at most one holder at a time; it fails when only close is gated.
  • Related backend suites: same failing test IDs on main and this branch.
  • get_connection open/close with 8 threads: about 0.9 ms per op vs 1.1 to 1.2 ms on main; single thread unchanged.

Fixes #10022

Gate studio.db opens as well as closes: SQLite 3.51.0-3.51.1 also deadlocks a WAL close
against a concurrent open (findReusableFd), so a close-only gate still hangs. Route the
library_db and account_access studio.db openers added on main through the same gate,
make it reentrant and reset it after fork, and drop the setup-failure refactor.
@danielhanchen

Copy link
Copy Markdown
Member

Pushed 57e6d1f: merged current main (conflicts resolved) and reworked the gate.

The hang in #10022 is an upstream SQLite bug in 3.51.0 and 3.51.1, fixed in 3.51.2 (unixIsSharingShmNode takes the VFS mutex while holding the inode mutex, the reverse of unixClose and findReusableFd). A C stress test on 3.51.1 (open, read, close loop on one WAL file) deadlocks within seconds:

SQLite gate result (3 runs each)
3.51.1 none deadlock 6/6 (4 and 16 threads)
3.51.1 close only (this PR as submitted) deadlock 5/6
3.51.1 open + close clean 6/6
3.51.2 none clean 3/3

So closing alone is not enough: connect_studio_db now takes the same lock around sqlite3.connect. The lock is reentrant (a finalizer can close a pooled connection inside a gated connect) and is reset after fork. The two studio.db openers added on main since this PR (library_db.get_connection and the model grant reads/writes in account_access) go through it too. The setup-failure refactor and its tests are dropped to keep this scoped to the deadlock.

Related backend suites show the same failing test IDs on main and on this branch (pre-existing, missing fastmcp here). Open/close cost with 8 threads: about 1.1 to 1.2 ms on main vs 0.86 to 0.91 ms here, so no slowdown.

@danielhanchen

Copy link
Copy Markdown
Member

Review record for #10069, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#395.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror cf0d3afcd (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] (on the mirror)

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: cf0d3afcd6

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror cf0d3afcd (no equivalent commit found on this PR).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

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

Projects

None yet

2 participants