Repository navigation
Description
Problem
A long SQLite statement holds one of Swift's cooperative threads for its whole run: a COUNT(*) over 10M rows, a 1M-row read, VACUUM, an export whose writer is slower than SQLite, or a wait on a database another process has locked. The cooperative pool has about one thread per core (about 10 on an M4). With no long statement the cost is nil. Once as many long SQLite operations run as there are cores, async work across the whole app stops until one finishes: other connections' results, schema loads and autocomplete. How often that happens is not measured.
With the busy timeout at 0, a locked database keeps its thread until the user presses Stop.
Cause
SQLiteLocalBackendis a plain actor with no custom executor (apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift:287), so its methods run on the global cooperative executor. macOS uses it for every local file connection (apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift:185-193).runStatementsteps the statement to its end synchronously on the actor (SQLiteExecutionBackend.swift:491).streamcallsspill.deliversynchronously, and its comment says why: an await would let another call reach the handle mid-statement (:602-650,:639-640).PluginRowSpill.deliverblocks its thread past the spool budget and is documented as "for a producer on its own thread" (apple/Plugins/SharedSpill/PluginRowSpill.swift:91-96). Past the budget it polls withusleep(2_000)(:281-292), thenPluginRowStream.deliverBlockingpolls again (apple/Plugins/TableProPluginKit/PluginStreamAbort.swift:99-125,:174).sqliteBusyHandlercallsusleepon the same thread (SQLiteExecutionBackend.swift:196-201), andshouldRetryreturns true forever whentimeoutMillisecondsis 0 (:187-193).- libSQL's local backend has the same shape (
apple/Plugins/LibSQLDriverPlugin/SQLiteLocalBackend.swift:34). - PostgreSQL and MySQL avoid this by running their C libraries on their own serial queue through
pluginDispatchAsync(apple/Plugins/TableProPluginKit/PluginConcurrencySupport.swift:3-31).
Overlaps #4444: its note that SQLiteAgentBackend calls the blocking PluginRowSpill.deliver from the cooperative pool (apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift:174) covers the remote agent path. Overlaps #4462: WI14 covers waking a blocked producer instead of polling every 2 ms. This issue adds the local backends and the busy handler.
Change
- Confine
SQLiteLocalBackend's state to a private serialDispatchQueueand enter it throughpluginDispatchAsync, the pattern the PostgreSQL and MySQL plugins already use and test. The "no await mid-statement" guarantee stays, because a statement runs start to end on that queue. (A customSerialExecutoralso works on the macOS 13 floor, but itsenqueue(UnownedJob)form is deprecated from macOS 14; the queue pattern avoids that.) - Apply the same change to libSQL's
SQLiteLocalBackend. - The busy handler's waits then run on the backend's queue, not the shared pool. Stop keeps working through the existing cancel flag in
SQLiteBusyState. PluginRowSpill.deliveris then called from a thread that owns its wait, as its documentation requires. Replacing the 2 ms polling with a signal stays with WI14 in perf(storage): Server Dashboard reads and gated idle items left from #4435 #4462.
Measure
- Start 12 concurrent
SELECT COUNT(*) FROM trips_10mon separate SQLite connections and time a trivial round trip on another connection (a PostgreSQLSELECT 1tab), before and after. Target: unchanged from the idle case. - Instruments Swift Concurrency template during the same run: no cooperative thread blocked in
sqlite3_steporusleep. - Lock a database from another process with the busy timeout at 0: the app's other connections stay responsive, and Stop ends the wait.
Part of the performance plan.
Activity