From 317e9ed4363a86b1364573c5a5e30011a080ce6d Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Sat, 5 Jun 2021 16:13:27 -0700 Subject: [PATCH] bpo-44304: Ensure the sqlite3 destructor callback is always called with the GIL held (GH-26551) (GH_26552) (cherry picked from commit 6e3b7cf3af3ed7758b2c2193c1d393feb8ab8f72) Co-authored-by: Pablo Galindo --- Modules/_sqlite/connection.c | 5 +++++ Modules/_sqlite/statement.c | 2 ++ 2 files changed, 7 insertions(+) diff --git a/Modules/_sqlite/connection.c b/Modules/_sqlite/connection.c index fccffabc4b2a..8e42a36bd337 100644 --- a/Modules/_sqlite/connection.c +++ b/Modules/_sqlite/connection.c @@ -851,7 +851,12 @@ static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self) static void _destructor(void* args) { + // This function may be called without the GIL held, so we need to ensure + // that we destroy 'args' with the GIL + PyGILState_STATE gstate; + gstate = PyGILState_Ensure(); Py_DECREF((PyObject*)args); + PyGILState_Release(gstate); } /*[clinic input] diff --git a/Modules/_sqlite/statement.c b/Modules/_sqlite/statement.c index cf7fba6ce9a2..072b07d4eaba 100644 --- a/Modules/_sqlite/statement.c +++ b/Modules/_sqlite/statement.c @@ -398,7 +398,9 @@ stmt_dealloc(pysqlite_Statement *self) PyObject_ClearWeakRefs((PyObject*)self); } if (self->st) { + Py_BEGIN_ALLOW_THREADS sqlite3_finalize(self->st); + Py_END_ALLOW_THREADS self->st = 0; } tp->tp_clear((PyObject *)self); -- 2.47.3