Skip to content

gh-104324: Reinstate GIL cleanup during interpreter deletion. #104325

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion Include/internal/pycore_ceval.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure that the GIL is correctly finalized during calls to ``Py_Finalize()``.
6 changes: 4 additions & 2 deletions Python/ceval_gil.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down