mirror of
https://github.com/python/cpython.git
synced 2025-11-01 06:01:29 +00:00
gh-136003: Skip non-daemon threads when exceptions occur during finalization (GH-139129)
During finalization, we need to mark all non-daemon threads as daemon to quickly shut down threads when sending CTRL^C to the process. This was a minor regression from GH-136004.
This commit is contained in:
parent
293b05c09b
commit
3eec897752
3 changed files with 51 additions and 6 deletions
|
|
@ -3548,6 +3548,27 @@ Py_ExitStatusException(PyStatus status)
|
|||
}
|
||||
|
||||
|
||||
static void
|
||||
handle_thread_shutdown_exception(PyThreadState *tstate)
|
||||
{
|
||||
assert(tstate != NULL);
|
||||
assert(_PyErr_Occurred(tstate));
|
||||
PyInterpreterState *interp = tstate->interp;
|
||||
assert(interp->threads.head != NULL);
|
||||
_PyEval_StopTheWorld(interp);
|
||||
|
||||
// We don't have to worry about locking this because the
|
||||
// world is stopped.
|
||||
_Py_FOR_EACH_TSTATE_UNLOCKED(interp, tstate) {
|
||||
if (tstate->_whence == _PyThreadState_WHENCE_THREADING) {
|
||||
tstate->_whence = _PyThreadState_WHENCE_THREADING_DAEMON;
|
||||
}
|
||||
}
|
||||
|
||||
_PyEval_StartTheWorld(interp);
|
||||
PyErr_FormatUnraisable("Exception ignored on threading shutdown");
|
||||
}
|
||||
|
||||
/* Wait until threading._shutdown completes, provided
|
||||
the threading module was imported in the first place.
|
||||
The shutdown routine will wait until all non-daemon
|
||||
|
|
@ -3559,14 +3580,14 @@ wait_for_thread_shutdown(PyThreadState *tstate)
|
|||
PyObject *threading = PyImport_GetModule(&_Py_ID(threading));
|
||||
if (threading == NULL) {
|
||||
if (_PyErr_Occurred(tstate)) {
|
||||
PyErr_FormatUnraisable("Exception ignored on threading shutdown");
|
||||
handle_thread_shutdown_exception(tstate);
|
||||
}
|
||||
/* else: threading not imported */
|
||||
return;
|
||||
}
|
||||
result = PyObject_CallMethodNoArgs(threading, &_Py_ID(_shutdown));
|
||||
if (result == NULL) {
|
||||
PyErr_FormatUnraisable("Exception ignored on threading shutdown");
|
||||
handle_thread_shutdown_exception(tstate);
|
||||
}
|
||||
else {
|
||||
Py_DECREF(result);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue