From 507dd9a85e29e0801f972e933df319f0dd2fe372 Mon Sep 17 00:00:00 2001 From: Andrew Rogers Date: Tue, 9 May 2023 15:13:12 +0100 Subject: [PATCH 1/2] Reinstate GIL cleanup during interpreter deletion. --- Include/internal/pycore_ceval.h | 2 +- Python/ceval_gil.c | 6 ++++-- Python/pylifecycle.c | 11 ----------- Python/pystate.c | 2 +- 4 files changed, 6 insertions(+), 15 deletions(-) diff --git a/Include/internal/pycore_ceval.h b/Include/internal/pycore_ceval.h index 3c8b368bd2af4e..20541f99635b72 100644 --- a/Include/internal/pycore_ceval.h +++ b/Include/internal/pycore_ceval.h @@ -22,7 +22,7 @@ struct _ceval_runtime_state; extern void _Py_FinishPendingCalls(PyThreadState *tstate); extern void _PyEval_InitState(PyInterpreterState *, PyThread_type_lock); -extern void _PyEval_FiniState(struct _ceval_state *ceval); +extern void _PyEval_FiniState(PyInterpreterState *interp); PyAPI_FUNC(void) _PyEval_SignalReceived(PyInterpreterState *interp); PyAPI_FUNC(int) _PyEval_AddPendingCall( PyInterpreterState *interp, diff --git a/Python/ceval_gil.c b/Python/ceval_gil.c index 42e1436bc9130d..0633551d809b42 100644 --- a/Python/ceval_gil.c +++ b/Python/ceval_gil.c @@ -964,13 +964,15 @@ _PyEval_InitState(PyInterpreterState *interp, PyThread_type_lock pending_lock) } void -_PyEval_FiniState(struct _ceval_state *ceval) +_PyEval_FiniState(PyInterpreterState *interp) { - struct _pending_calls *pending = &ceval->pending; + struct _pending_calls *pending = &interp->ceval.pending; if (pending->lock != NULL) { PyThread_free_lock(pending->lock); pending->lock = NULL; } + + _PyEval_FiniGIL(interp); } /* Handle signals, pending calls, GIL drop request diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 61f87c5eba60ed..5fdb4e5c21e6d8 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -589,11 +589,6 @@ init_interp_create_gil(PyThreadState *tstate, int own_gil) { PyStatus status; - /* finalize_interp_delete() comment explains why _PyEval_FiniGIL() is - only called here. */ - // XXX This is broken with a per-interpreter GIL. - _PyEval_FiniGIL(tstate->interp); - /* Auto-thread-state API */ status = _PyGILState_SetTstate(tstate); if (_PyStatus_EXCEPTION(status)) { @@ -1737,12 +1732,6 @@ finalize_interp_delete(PyInterpreterState *interp) /* Cleanup auto-thread-state */ _PyGILState_Fini(interp); - /* We can't call _PyEval_FiniGIL() here because destroying the GIL lock can - fail when it is being awaited by another running daemon thread (see - bpo-9901). Instead pycore_create_interpreter() destroys the previously - created GIL, which ensures that Py_Initialize / Py_FinalizeEx can be - called multiple times. */ - PyInterpreterState_Delete(interp); } diff --git a/Python/pystate.c b/Python/pystate.c index 26debf1f88b94a..59e8b9c1897a08 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -948,7 +948,7 @@ PyInterpreterState_Delete(PyInterpreterState *interp) zapthreads(interp); - _PyEval_FiniState(&interp->ceval); + _PyEval_FiniState(interp); // XXX These two calls should be done at the end of clear_interpreter(), // but currently some objects get decref'ed after that. From d5961c6b2903a9ed50abc4323dcd2383f132d9c0 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 9 May 2023 14:27:43 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2023-05-09-14-27-43.gh-issue-104324.anqapB.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2023-05-09-14-27-43.gh-issue-104324.anqapB.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2023-05-09-14-27-43.gh-issue-104324.anqapB.rst b/Misc/NEWS.d/next/Core and Builtins/2023-05-09-14-27-43.gh-issue-104324.anqapB.rst new file mode 100644 index 00000000000000..5d50d6fd0fe4c6 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2023-05-09-14-27-43.gh-issue-104324.anqapB.rst @@ -0,0 +1 @@ +Ensure that the GIL is correctly finalized during calls to ``Py_Finalize()``.