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.
This commit is contained in:
Sam Gross 2024-03-19 14:40:20 -04:00 committed by GitHub
parent 025ef7a5f7
commit 60e105c1c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 57 additions and 27 deletions

View file

@ -5357,20 +5357,21 @@ class ForkTests(unittest.TestCase):
self.assertEqual(err.decode("utf-8"), "")
self.assertEqual(out.decode("utf-8"), "")
def test_fork_at_exit(self):
def test_fork_at_finalization(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)
class AtFinalization:
def __del__(self):
print("OK")
pid = os.fork()
if pid != 0:
print("shouldn't be printed")
at_finalization = AtFinalization()
"""
_, out, err = assert_python_ok("-c", code)
self.assertEqual(b"", out)
self.assertEqual(b"OK\n", out)
self.assertIn(b"can't fork at interpreter shutdown", err)