Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Lib/test/test_free_threading/test_sqlite3.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import threading
import unittest

from test import support
Expand Down Expand Up @@ -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()
12 changes: 12 additions & 0 deletions Lib/test/test_sqlite3/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 4 additions & 3 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
17 changes: 15 additions & 2 deletions Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think we need to re-check the connection here is open.

return stmt;
}

static inline int
Expand Down
Loading