gh-136003: Execute pre-finalization callbacks in a loop (GH-136004)

This commit is contained in:
Peter Bierma 2025-09-18 08:29:12 -04:00 committed by GitHub
parent d6a6fe2a5b
commit 2191497933
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 281 additions and 43 deletions

View file

@ -429,7 +429,7 @@ force_done(void *arg)
static int
ThreadHandle_start(ThreadHandle *self, PyObject *func, PyObject *args,
PyObject *kwargs)
PyObject *kwargs, int daemon)
{
// Mark the handle as starting to prevent any other threads from doing so
PyMutex_Lock(&self->mutex);
@ -453,7 +453,8 @@ ThreadHandle_start(ThreadHandle *self, PyObject *func, PyObject *args,
goto start_failed;
}
PyInterpreterState *interp = _PyInterpreterState_GET();
boot->tstate = _PyThreadState_New(interp, _PyThreadState_WHENCE_THREADING);
uint8_t whence = daemon ? _PyThreadState_WHENCE_THREADING_DAEMON : _PyThreadState_WHENCE_THREADING;
boot->tstate = _PyThreadState_New(interp, whence);
if (boot->tstate == NULL) {
PyMem_RawFree(boot);
if (!PyErr_Occurred()) {
@ -1916,7 +1917,7 @@ do_start_new_thread(thread_module_state *state, PyObject *func, PyObject *args,
add_to_shutdown_handles(state, handle);
}
if (ThreadHandle_start(handle, func, args, kwargs) < 0) {
if (ThreadHandle_start(handle, func, args, kwargs, daemon) < 0) {
if (!daemon) {
remove_from_shutdown_handles(handle);
}