diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-06-04 04:32:00 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-04 04:32:00 (GMT) |
commit | c7a9d96a25a646d37cb97506019e82ee7493d1b3 (patch) | |
tree | 28c1706b85c96d3b04e4a3ed56378acec7cee217 /Lib/test/test_threading.py | |
parent | f629d5fc240e674652e6d183ada7a4e2dbdf3e18 (diff) | |
download | cpython-c7a9d96a25a646d37cb97506019e82ee7493d1b3.zip cpython-c7a9d96a25a646d37cb97506019e82ee7493d1b3.tar.gz cpython-c7a9d96a25a646d37cb97506019e82ee7493d1b3.tar.bz2 |
[3.12] gh-104690 Disallow thread creation and fork at interpreter finalization (GH-104826) (#105277)
gh-104690 Disallow thread creation and fork at interpreter finalization (GH-104826)
Disallow thread creation and fork at interpreter finalization.
in the following functions, check if interpreter is finalizing and raise `RuntimeError` with appropriate message:
* `_thread.start_new_thread` and thus `threading`
* `posix.fork`
* `posix.fork1`
* `posix.forkpty`
* `_posixsubprocess.fork_exec` when a `preexec_fn=` is supplied.
---------
(cherry picked from commit ce558e69d4087dd3653207de78345fbb8a2c7835)
Co-authored-by: chgnrdv <52372310+chgnrdv@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
Diffstat (limited to 'Lib/test/test_threading.py')
-rw-r--r-- | Lib/test/test_threading.py | 44 |
1 files changed, 16 insertions, 28 deletions
diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py index 9716526..9e4972e 100644 --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -532,34 +532,6 @@ class ThreadTests(BaseTestCase): 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.assertEqual(err.rstrip(), b'child process ok') - - @support.requires_fork() def test_dummy_thread_after_fork(self): # Issue #14308: a dummy thread in the active list doesn't mess up # the after-fork mechanism. @@ -1048,6 +1020,22 @@ class ThreadTests(BaseTestCase): self.assertEqual(out, b'') self.assertEqual(err, b'') + def test_start_new_thread_at_exit(self): + code = """if 1: + import atexit + import _thread + + def f(): + print("shouldn't be printed") + + def exit_handler(): + _thread.start_new_thread(f, ()) + + atexit.register(exit_handler) + """ + _, out, err = assert_python_ok("-c", code) + self.assertEqual(out, b'') + self.assertIn(b"can't create new thread at interpreter shutdown", err) class ThreadJoinOnShutdown(BaseTestCase): |