Skip to content

Commit 4153139

Browse files
committed
Don't finalize in stmt_dealloc() if SQLite has no connection mutex
In multi-thread mode (the system SQLite on Apple platforms) there is no connection mutex: sqlite3_db_mutex() returns NULL and sqlite3_mutex_try(NULL) reports success. stmt_dealloc() then finalized the statement while another thread could be using the connection, which is not allowed in that mode. Leave the statement on the pending list in that case, so that it is finalized by a thread that is using the connection.
1 parent dd5a66a commit 4153139

1 file changed

Lines changed: 37 additions & 20 deletions

File tree

‎Modules/_sqlite/statement.c‎

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ stmt_dealloc(PyObject *op)
166166
* left on the list are finalized the next time the connection prepares or
167167
* executes a statement, or when it is closed. Delaying sqlite3_finalize() is
168168
* harmless: the statement has normally been reset already and holds no locks.
169+
*
170+
* SQLite can also run without a connection mutex ("multi-thread" mode, used
171+
* by the system SQLite on Apple platforms). A connection may then be used by
172+
* several threads, but only by one at a time, and it is up to the application
173+
* to ensure that. The application cannot know when a statement is
174+
* deallocated, so in this mode stmt_dealloc() must not call into SQLite at
175+
* all while the connection is open: it could run at the same time as the
176+
* thread that is using the connection. The statement is always left on the
177+
* list, to be finalized by a thread that is using the connection.
169178
*/
170179

171180
pysqlite_pending_stmts *
@@ -253,6 +262,30 @@ pysqlite_pending_stmts_close(pysqlite_pending_stmts *pending)
253262
pending_stmts_decref(pending);
254263
}
255264

265+
/* Remove the first statement from the list if the mutex of its database can
266+
* be taken without blocking. Return the statement, with the mutex held and
267+
* stored in *mutex. Return NULL if the list is empty or the mutex is not
268+
* available. */
269+
static pysqlite_Statement *
270+
pending_stmts_try_pop(pysqlite_pending_stmts *pending, sqlite3_mutex **mutex)
271+
{
272+
pysqlite_Statement *stmt = NULL;
273+
PyMutex_Lock(&pending->mutex);
274+
pysqlite_Statement *head = pending->head;
275+
if (head != NULL) {
276+
// The database stays valid while the statement is on the list.
277+
// The mutex is NULL in multi-thread mode: leave the statement on
278+
// the list (sqlite3_mutex_try(NULL) would report success).
279+
*mutex = sqlite3_db_mutex(sqlite3_db_handle(head->st));
280+
if (*mutex != NULL && sqlite3_mutex_try(*mutex) == SQLITE_OK) {
281+
_Py_atomic_store_ptr_relaxed(&pending->head, head->next_pending);
282+
stmt = head;
283+
}
284+
}
285+
PyMutex_Unlock(&pending->mutex);
286+
return stmt;
287+
}
288+
256289
static void
257290
stmt_dealloc(PyObject *op)
258291
{
@@ -280,28 +313,12 @@ stmt_dealloc(PyObject *op)
280313
PyMutex_Unlock(&pending->mutex);
281314

282315
// Finalize the statements on the list (normally just this one) for as
283-
// long as the database mutex is free. Taking the list lock again after
316+
// long as the database mutex is free. Checking the list again after
284317
// each one also picks up statements added by threads that found the
285318
// database mutex held by us.
286-
for (;;) {
287-
PyMutex_Lock(&pending->mutex);
288-
pysqlite_Statement *stmt = pending->head;
289-
sqlite3_mutex *mutex = NULL;
290-
if (stmt != NULL) {
291-
// The database stays valid while the statement is on the list.
292-
mutex = sqlite3_db_mutex(sqlite3_db_handle(stmt->st));
293-
if (sqlite3_mutex_try(mutex) == SQLITE_OK) {
294-
_Py_atomic_store_ptr_relaxed(&pending->head,
295-
stmt->next_pending);
296-
}
297-
else {
298-
stmt = NULL;
299-
}
300-
}
301-
PyMutex_Unlock(&pending->mutex);
302-
if (stmt == NULL) {
303-
break;
304-
}
319+
pysqlite_Statement *stmt;
320+
sqlite3_mutex *mutex;
321+
while ((stmt = pending_stmts_try_pop(pending, &mutex)) != NULL) {
305322
stmt_destroy(stmt, mutex);
306323
}
307324
pending_stmts_decref(pending);

0 commit comments

Comments
 (0)