8000 gh-104341: Wait Completely at threading._shutdown() by ericsnowcurrently · Pull Request #104672 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-104341: Wait Completely at threading._shutdown() #104672

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

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Release the shutdown lock *after* releasing the GIL.
  • Loading branch information
ericsnowcurrently committed May 23, 2023
commit 99ffe4be609885be90c138a3d777aef59be98aab
2 changes: 1 addition & 1 deletion Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ struct _ts {
* weakref-to-lock (on_delete_data) argument, and release_sentinel releases
* the indirectly held lock.
*/
void (*on_delete)(void *);
int (*on_delete)(void *);
void *on_delete_data;

int coroutine_origin_tracking_depth;
Expand Down
3 changes: 2 additions & 1 deletion Modules/_threadmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ yet finished.\n\
This function is meant for internal and specialized purposes only.\n\
In most applications `threading.enumerate()` should be used instead.");

static void
static int
release_sentinel(void *wr_raw)
{
PyObject *wr = _PyObject_CAST(wr_raw);
Expand All @@ -1326,6 +1326,7 @@ release_sentinel(void *wr_raw)
/* Deallocating a weakref with a NULL callback only calls
PyObject_GC_Del(), which can't call any Python code. */
Py_DECREF(wr);
return 0;
}

static PyObject *
Expand Down
13 changes: 9 additions & 4 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1490,10 +1490,6 @@ PyThreadState_Clear(PyThreadState *tstate)

Py_CLEAR(tstate->context);

if (tstate->on_delete != NULL) {
tstate->on_delete(tstate->on_delete_data);
}

tstate->_status.cleared = 1;

// XXX Call _PyThreadStateSwap(runtime, NULL) here if "current".
Expand Down Expand Up @@ -1565,6 +1561,15 @@ void
_PyThreadState_DeleteCurrent(PyThreadState *tstate)
{
_Py_EnsureTstateNotNULL(tstate);
/* We ignore the return value. A non-zero value means there were
too many pending calls already queued up. This case is unlikely,
and, at worst, we'll leak on_delete_data.
Also, note that the pending call will be run the next time the
GIL is taken by one of this interpreter's threads. So it won't
happen until after the _PyEval_ReleaseLock() call below. */
(void)_PyEval_AddPendingCall(tstate->interp,
tstate->on_delete, tstate->on_delete_data);

tstate_delete_common(tstate);
current_fast_clear(tstate->interp->runtime);
_PyEval_ReleaseLock(tstate);
Expand Down
0