Skip to content

Commit 0601a6d

Browse files
committed
Add a test.
1 parent d54bca9 commit 0601a6d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Lib/test/test_atexit.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import unittest
55
from test import support
66
from test.support import script_helper
7+
from test.support import threading_helper
78

89

910
class GeneralTest(unittest.TestCase):
@@ -46,6 +47,34 @@ def test_atexit_instances(self):
4647
self.assertEqual(res.out.decode().splitlines(), ["atexit2", "atexit1"])
4748
self.assertFalse(res.err)
4849

50+
@threading_helper.requires_working_threading()
51+
@support.requires_resource("cpu")
52+
@unittest.skipUnless(support.Py_GIL_DISABLED, "only meaningful without the GIL")
53+
def test_atexit_thread_safety(self):
54+
# GH-126907: atexit was not thread safe on the free-threaded build
55+
56+
# I'm not certain this needs to be in a script runner, but
57+
# let's do it anyway.
58+
code = textwrap.dedent("""
59+
from threading import Thread
60+
61+
def dummy():
62+
pass
63+
64+
65+
def thready():
66+
for _ in range(100):
67+
atexit.register(dummy)
68+
atexit._clear()
69+
atexit.register(dummy)
70+
atexit.unregister(dummy)
71+
72+
73+
for x in range(100):
74+
Thread(target=thready, args=()).start()
75+
""")
76+
script_helper.assert_python_ok("-c", code)
77+
4978

5079
@support.cpython_only
5180
class SubinterpreterTest(unittest.TestCase):

0 commit comments

Comments
 (0)