You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 94e1b91
Browse filesBrowse the repository at this point in the historyBrowse files
gh-162919: Never call into SQLite from deallocators while the connection is open
On the free-threaded build, a statement or cursor can be deallocated by any
thread at any point (queued decrefs from non-owner threads, delayed decrefs,
the GC), so the deallocating thread may not be allowed to use the connection
at that moment. The previous commit relied on the connection mutex being
NULL in multi-thread mode, but with SQLITE_ENABLE_MULTITHREADED_CHECKS (as
in the system SQLite on Apple platforms) the connection has a mutex whose
only purpose is to detect misuse: any contention on it is reported as
"illegal multi-threaded access to database connection" and the process
aborts.
- stmt_dealloc() never finalizes while the connection is open: the dead
statement always goes on the pending list. This removes the try-lock
path. Dead statements are already reset and hold no locks.
- cursor_clear() resets an unfinished cursor's statement directly if the
connection is open and either restricted to the current thread
(check_same_thread and this is its thread), or SQLite serializes access
to it and the current thread is the last one that used it (recorded in
pysqlite_check_thread()). Concurrent use is allowed in serialized mode,
so the second check is not needed for safety: it only makes it unlikely
that the deallocator waits for the connection mutex. Otherwise the
statement goes on a second pending list and is reset by the next thread
that executes a statement on the connection, commits or rolls back.
Resetting releases the read lock, so the common cases keep doing it right
away; a connection pool's reset-on-return releases it otherwise.
- Two cursors can use the same statement (a failed executemany() resets a
cached statement that another cursor is using, and a third cursor can
then take it from the cache). A statement already on the reset list is
not pushed again: the deallocator only drops its reference.
- After close(), statements are reset and finalized by whichever thread
deallocates them. One thread at a time processes the pending lists (a
processing flag in the shared object): a deallocator that finds them being
processed, by another thread or by its own thread further up the stack,
pushes the statement and returns, and the processing thread empties the
lists before clearing the flag. close() processes the lists and calls
sqlite3_close_v2() as the processing thread. A plain lock would deadlock:
Python code can run inside sqlite3_reset() (an aggregate's finalize()) and
drop another busy cursor of the same connection.
0 commit comments