[3.12] gh-113964: Don't prevent new threads until all non-daemon threads exit (GH-116677) (#117029)

Starting in Python 3.12, we prevented calling fork() and starting new threads
during interpreter finalization (shutdown). This has led to a number of
regressions and flaky tests. We should not prevent starting new threads
(or `fork()`) until all non-daemon threads exit and finalization starts in
earnest.

This changes the checks to use `_PyInterpreterState_GetFinalizing(interp)`,
which is set immediately before terminating non-daemon threads.

(cherry picked from commit 60e105c1c1)
This commit is contained in:
Sam Gross 2024-03-19 15:22:42 -04:00 committed by GitHub
parent 4be9fa8961
commit 92564331de
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 57 additions and 27 deletions

View file

@ -946,7 +946,9 @@ subprocess_fork_exec_impl(PyObject *module, PyObject *process_args,
Py_ssize_t fds_to_keep_len = PyTuple_GET_SIZE(py_fds_to_keep);
PyInterpreterState *interp = PyInterpreterState_Get();
if ((preexec_fn != Py_None) && interp->finalizing) {
if ((preexec_fn != Py_None) &&
_PyInterpreterState_GetFinalizing(interp) != NULL)
{
PyErr_SetString(PyExc_RuntimeError,
"preexec_fn not supported at interpreter shutdown");
return NULL;