-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
gh-145040: Fix crash in sqlite3 when connection is closed from within a callback #145041
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
da5c4a6
1ecb567
05086f0
c7dbc7e
eb69460
6f07252
36ea504
c7707be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| Fixed a crash in the :mod:`sqlite3` module caused by closing the database | ||
| connection from within a callback function invoked during | ||
| ``sqlite3_step()`` (e.g., an aggregate ``step``, a user-defined function | ||
| via :meth:`~sqlite3.Connection.create_function`, a progress handler, or a | ||
| collation callback). Raise :exc:`~sqlite3.ProgrammingError` instead of | ||
| crashing. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,7 @@ | |
| #include "pycore_pyerrors.h" // _PyErr_ChainExceptions1() | ||
| #include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing() | ||
| #include "pycore_unicodeobject.h" // _PyUnicode_AsUTF8NoNUL | ||
| #include "pycore_weakref.h" | ||
|
|
||
| #include <stdbool.h> | ||
|
|
||
|
|
@@ -283,10 +284,17 @@ pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database, | |
| goto error; | ||
| } | ||
|
|
||
| /* Create lists of weak references to blobs */ | ||
| /* Create lists of weak references to cursors and blobs */ | ||
| PyObject *cursors = PyList_New(0); | ||
| if (cursors == NULL) { | ||
| Py_DECREF(statement_cache); | ||
| goto error; | ||
| } | ||
|
|
||
| PyObject *blobs = PyList_New(0); | ||
| if (blobs == NULL) { | ||
| Py_DECREF(statement_cache); | ||
| Py_DECREF(cursors); | ||
| goto error; | ||
| } | ||
|
|
||
|
|
@@ -299,7 +307,9 @@ pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database, | |
| self->check_same_thread = check_same_thread; | ||
| self->thread_ident = PyThread_get_thread_ident(); | ||
| self->statement_cache = statement_cache; | ||
| self->cursors = cursors; | ||
| self->blobs = blobs; | ||
| self->close_attempted_in_callback = 0; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of adding this flag, did you try to just raise in
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (The exception should propagate through the existing (slightly weird) callback exception handling in |
||
| self->row_factory = Py_NewRef(Py_None); | ||
| self->text_factory = Py_NewRef(&PyUnicode_Type); | ||
| self->trace_ctx = NULL; | ||
|
|
@@ -381,6 +391,7 @@ connection_traverse(PyObject *op, visitproc visit, void *arg) | |
| pysqlite_Connection *self = _pysqlite_Connection_CAST(op); | ||
| Py_VISIT(Py_TYPE(self)); | ||
| Py_VISIT(self->statement_cache); | ||
| Py_VISIT(self->cursors); | ||
| Py_VISIT(self->blobs); | ||
| Py_VISIT(self->row_factory); | ||
| Py_VISIT(self->text_factory); | ||
|
|
@@ -405,6 +416,7 @@ connection_clear(PyObject *op) | |
| { | ||
| pysqlite_Connection *self = _pysqlite_Connection_CAST(op); | ||
| Py_CLEAR(self->statement_cache); | ||
| Py_CLEAR(self->cursors); | ||
| Py_CLEAR(self->blobs); | ||
| Py_CLEAR(self->row_factory); | ||
| Py_CLEAR(self->text_factory); | ||
|
|
@@ -655,6 +667,32 @@ pysqlite_connection_close_impl(pysqlite_Connection *self) | |
| return NULL; | ||
| } | ||
|
|
||
| /* Check if any cursor is locked (actively executing a query); | ||
| * closing during a callback is illegal per the SQLite C API docs. */ | ||
| assert(PyList_CheckExact(self->cursors)); | ||
| Py_ssize_t n = PyList_GET_SIZE(self->cursors); | ||
| for (Py_ssize_t i = 0; i < n; i++) { | ||
| PyObject *weakref = PyList_GET_ITEM(self->cursors, i); | ||
| if (_PyWeakref_IsDead(weakref)) { | ||
| continue; | ||
| } | ||
| PyObject *obj; | ||
| if (!PyWeakref_GetRef(weakref, &obj)) { | ||
| continue; | ||
| } | ||
| int locked = ((pysqlite_Cursor *)obj)->locked; | ||
| Py_DECREF(obj); | ||
| if (locked) { | ||
| self->close_attempted_in_callback = 1; | ||
| PyTypeObject *tp = Py_TYPE(self); | ||
| pysqlite_state *state = pysqlite_get_state_by_type(tp); | ||
| PyErr_SetString(state->ProgrammingError, | ||
| "Cannot close the database connection " | ||
| "from within a callback function."); | ||
| return NULL; | ||
| } | ||
| } | ||
|
|
||
| pysqlite_close_all_blobs(self); | ||
| Py_CLEAR(self->statement_cache); | ||
| if (connection_close(self) < 0) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need
pycore_weakref.h; we should be fine with the public API functions here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See
blob.cfor how to iterate through the weak refs using the public API.