mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
[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 ce558e69d4
)
Co-authored-by: chgnrdv <52372310+chgnrdv@users.noreply.github.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
This commit is contained in:
parent
f629d5fc24
commit
c7a9d96a25
8 changed files with 97 additions and 30 deletions
|
@ -4700,6 +4700,22 @@ class ForkTests(unittest.TestCase):
|
|||
self.assertEqual(err.decode("utf-8"), "")
|
||||
self.assertEqual(out.decode("utf-8"), "")
|
||||
|
||||
def test_fork_at_exit(self):
|
||||
code = """if 1:
|
||||
import atexit
|
||||
import os
|
||||
|
||||
def exit_handler():
|
||||
pid = os.fork()
|
||||
if pid != 0:
|
||||
print("shouldn't be printed")
|
||||
|
||||
atexit.register(exit_handler)
|
||||
"""
|
||||
_, out, err = assert_python_ok("-c", code)
|
||||
self.assertEqual(b"", out)
|
||||
self.assertIn(b"can't fork at interpreter shutdown", err)
|
||||
|
||||
|
||||
# Only test if the C version is provided, otherwise TestPEP519 already tested
|
||||
# the pure Python implementation.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue