diff --git a/Lib/test/test_free_threading/test_sqlite3.py b/Lib/test/test_free_threading/test_sqlite3.py index 456771726f7a8f4..0d777597f596b59 100644 --- a/Lib/test/test_free_threading/test_sqlite3.py +++ b/Lib/test/test_free_threading/test_sqlite3.py @@ -1,3 +1,4 @@ +import threading import unittest from test import support @@ -49,6 +50,55 @@ def mutator(): with support.swap_item(sqlite3.converters, NAME, Converter(0)): run_concurrently([reader] * 8 + [mutator] * 2) + def test_close_during_statement_cache_lookup(self): + # execute() must load the statement cache under the connection lock, + # since close() on another thread clears it. Params.__iter__ runs + # after execute() has checked the connection and before it loads the + # cache, so the handshake orders only the checks before close(). + class Stop(Exception): + pass + + class SQL(str): + # Raise at the cache lookup, before SQLite is used. + def __hash__(self): + raise Stop + + def run(): + cx = sqlite3.connect(":memory:", check_same_thread=False) + cu = cx.cursor() + close = cx.close + ready = threading.Lock() + ready.acquire() + raised = [] + + class Params: + def __iter__(self): + ready.release() + return iter(()) + + def reader(): + try: + cu.executemany(SQL("select 1"), Params()) + except Exception as exc: + raised.append(exc) + + t = threading.Thread(target=reader) + t.start() + try: + # Ensure we don't close too early before other thread + # has got past check_cursor() + self.assertTrue(ready.acquire(timeout=support.SHORT_TIMEOUT)) + close() + finally: + t.join() + self.assertEqual(len(raised), 1) + self.assertIsInstance(raised[0], (Stop, sqlite3.ProgrammingError)) + + # A GC pause between the two threads would also order them. + with support.disable_gc(): + for _ in range(5): + run() + if __name__ == "__main__": unittest.main() diff --git a/Lib/test/test_sqlite3/test_regression.py b/Lib/test/test_sqlite3/test_regression.py index 50cced3891d13a5..27e29f2ad1e4631 100644 --- a/Lib/test/test_sqlite3/test_regression.py +++ b/Lib/test/test_sqlite3/test_regression.py @@ -277,6 +277,18 @@ def foo(): cur.executemany("insert into b (baz) values (?)", ((i,) for i in foo())) + def test_close_during_statement_cache_lookup(self): + # The statement cache hashes the SQL string, which can run Python + # code. Closing the connection there must not crash. + with memory_database() as con: + class SQL(str): + def __hash__(self): + con.close() + return super().__hash__() + + with self.assertRaises(sqlite.ProgrammingError): + con.execute(SQL("select 1")) + def test_convert_timestamp_microsecond_padding(self): """ http://bugs.python.org/issue14720 diff --git a/Misc/NEWS.d/next/Library/2026-11-19-21-48-40.gh-issue-162288.lJZFXQ.rst b/Misc/NEWS.d/next/Library/2026-11-19-21-48-40.gh-issue-162288.lJZFXQ.rst new file mode 100644 index 000000000000000..0f6a5db98e89b16 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-19-21-48-40.gh-issue-162288.lJZFXQ.rst @@ -0,0 +1,5 @@ +Fix a crash in :mod:`sqlite3` when the connection is closed while +:meth:`~sqlite3.Cursor.execute` or :meth:`~sqlite3.Cursor.executemany` is +looking up the prepared statement, for example from the +:meth:`~object.__hash__` of a :class:`str` subclass or from another thread. +A :exc:`~sqlite3.ProgrammingError` is now raised instead. diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index f8bd3a8df35b668..0271b144bbc83b3 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -303,7 +303,8 @@ pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database, self->autocommit = autocommit; self->check_same_thread = check_same_thread; self->thread_ident = PyThread_get_thread_ident(); - self->statement_cache = statement_cache; + pysqlite_set_ref((PyObject *)self, &self->statement_cache, + statement_cache); self->blobs = blobs; pysqlite_set_ref((PyObject *)self, &self->row_factory, Py_NewRef(Py_None)); pysqlite_set_ref((PyObject *)self, &self->text_factory, @@ -410,7 +411,7 @@ static int connection_clear(PyObject *op) { pysqlite_Connection *self = _pysqlite_Connection_CAST(op); - Py_CLEAR(self->statement_cache); + pysqlite_set_ref(op, &self->statement_cache, NULL); Py_CLEAR(self->blobs); pysqlite_set_ref(op, &self->row_factory, NULL); pysqlite_set_ref(op, &self->text_factory, NULL); @@ -709,7 +710,7 @@ pysqlite_connection_close_impl(pysqlite_Connection *self) } pysqlite_close_all_blobs(self); - Py_CLEAR(self->statement_cache); + pysqlite_set_ref((PyObject *)self, &self->statement_cache, NULL); if (connection_close(self) < 0) { return NULL; } diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index ad8c434c1b2fb8f..ced0e62f7860186 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -536,9 +536,22 @@ static PyObject * get_statement_from_cache(pysqlite_Cursor *self, PyObject *operation) { PyObject *args[] = { NULL, operation, }; // Borrowed ref. - PyObject *cache = self->connection->statement_cache; + // Hold a strong reference: the call may run Python code (e.g. a str + // subclass __hash__) or let another thread run, and either may close + // the connection and clear the cache. + PyObject *cache; + Py_BEGIN_CRITICAL_SECTION(self->connection); + cache = Py_XNewRef(self->connection->statement_cache); + Py_END_CRITICAL_SECTION(); + if (cache == NULL) { + PyErr_SetString(self->connection->state->ProgrammingError, + "Cannot operate on a closed database."); + return NULL; + } size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET; - return PyObject_Vectorcall(cache, args + 1, nargsf, NULL); + PyObject *stmt = PyObject_Vectorcall(cache, args + 1, nargsf, NULL); + Py_DECREF(cache); + return stmt; } static inline int