summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorSam Gross <colesbury@gmail.com>2024-03-19 18:40:20 (GMT)
committerGitHub <noreply@github.com>2024-03-19 18:40:20 (GMT)
commit60e105c1c11ecca1680d03c38aa06bcc77a28714 (patch)
treeec314b15b7739ae1f0c463ebd47cff1d06b4260e /Modules
parent025ef7a5f7b424fba8713e448244b952bf897df3 (diff)
downloadcpython-60e105c1c11ecca1680d03c38aa06bcc77a28714.zip
cpython-60e105c1c11ecca1680d03c38aa06bcc77a28714.tar.gz
cpython-60e105c1c11ecca1680d03c38aa06bcc77a28714.tar.bz2
gh-113964: Don't prevent new threads until all non-daemon threads exit (#116677)
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.
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_posixsubprocess.c4
-rw-r--r--Modules/_threadmodule.c2
-rw-r--r--Modules/posixmodule.c6
3 files changed, 7 insertions, 5 deletions
diff --git a/Modules/_posixsubprocess.c b/Modules/_posixsubprocess.c
index bcbbe70..b160cd7 100644
--- a/Modules/_posixsubprocess.c
+++ b/Modules/_posixsubprocess.c
@@ -1031,7 +1031,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_PythonFinalizationError,
"preexec_fn not supported at interpreter shutdown");
return NULL;
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c
index 6889e8f..4912cd7 100644
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -1729,7 +1729,7 @@ do_start_new_thread(thread_module_state *state, PyObject *func, PyObject *args,
"thread is not supported for isolated subinterpreters");
return -1;
}
- if (interp->finalizing) {
+ if (_PyInterpreterState_GetFinalizing(interp) != NULL) {
PyErr_SetString(PyExc_PythonFinalizationError,
"can't create new thread at interpreter shutdown");
return -1;
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 7b2d366..2498b61 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -7842,7 +7842,7 @@ os_fork1_impl(PyObject *module)
pid_t pid;
PyInterpreterState *interp = _PyInterpreterState_GET();
- if (interp->finalizing) {
+ if (_PyInterpreterState_GetFinalizing(interp) != NULL) {
PyErr_SetString(PyExc_PythonFinalizationError,
"can't fork at interpreter shutdown");
return NULL;
@@ -7886,7 +7886,7 @@ os_fork_impl(PyObject *module)
{
pid_t pid;
PyInterpreterState *interp = _PyInterpreterState_GET();
- if (interp->finalizing) {
+ if (_PyInterpreterState_GetFinalizing(interp) != NULL) {
PyErr_SetString(PyExc_PythonFinalizationError,
"can't fork at interpreter shutdown");
return NULL;
@@ -8719,7 +8719,7 @@ os_forkpty_impl(PyObject *module)
pid_t pid;
PyInterpreterState *interp = _PyInterpreterState_GET();
- if (interp->finalizing) {
+ if (_PyInterpreterState_GetFinalizing(interp) != NULL) {
PyErr_SetString(PyExc_PythonFinalizationError,
"can't fork at interpreter shutdown");
return NULL;