Skip to content
Merged
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
Prev Previous commit
Next Next commit
Suggested changes:
* removed excess test case from `test_threading`
* changed NEWS entry to not mention internal function
* allowed `subprocess.Popen` at shutdown if `preexec_fn` is `None`
  • Loading branch information
chgnrdv committed May 26, 2023
commit bbc8ca4ae24dc5ba296a2923a355b77aeaac7626
28 changes: 0 additions & 28 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,34 +531,6 @@ def test_daemon_param(self):
t = threading.Thread(daemon=True)
self.assertTrue(t.daemon)

@support.requires_fork()
def test_fork_at_exit(self):
# bpo-42350: Calling os.fork() after threading._shutdown() must
# not log an error.
code = textwrap.dedent("""
import atexit
import os
import sys
from test.support import wait_process

# Import the threading module to register its "at fork" callback
import threading

def exit_handler():
pid = os.fork()
if not pid:
print("child process ok", file=sys.stderr, flush=True)
# child process
else:
wait_process(pid, exitcode=0)

# exit_handler() will be called after threading._shutdown()
atexit.register(exit_handler)
""")
_, out, err = assert_python_ok("-c", code)
self.assertEqual(out, b'')
self.assertIn(b"can't fork at interpreter shutdown", err)

@support.requires_fork()
def test_dummy_thread_after_fork(self):
# Issue #14308: a dummy thread in the active list doesn't mess up
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Starting new threads and process creation through fork/vfork at interpreter exit is no longer supported, as it can lead to race condition between main Python thread freeing existing thread states and :func:`_thread.start_new_thread` trying to allocate and use state of just created thread or fork/vfork trying to use main thread state in child process.
Starting new threads and process creation through fork/vfork at interpreter exit is no longer supported, as it can lead to race condition between main Python thread freeing existing thread states and internal :mod:`threading` routines trying to allocate and use state of just created thread or fork/vfork trying to use main thread state in child process.
4 changes: 2 additions & 2 deletions Modules/_posixsubprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -944,9 +944,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 (interp->finalizing) {
if ((preexec_fn != Py_None) && interp->finalizing) {
PyErr_SetString(PyExc_RuntimeError,
"can't create child process at interpreter shutdown");
"preexec_fn not supported at interpreter shutdown");
return NULL;
}
if ((preexec_fn != Py_None) && (interp != PyInterpreterState_Main())) {
Expand Down