summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_threading.py
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-11-16 14:20:34 (GMT)
committerGitHub <noreply@github.com>2020-11-16 14:20:34 (GMT)
commit5909a494cd3ba43143b28bd439773ed85a485dfc (patch)
tree0b56888e59bd79cc18ade15404f231d6d700e551 /Lib/test/test_threading.py
parent3df5c68487df9d1d20ab0cd06e7942a4f96d40a4 (diff)
downloadcpython-5909a494cd3ba43143b28bd439773ed85a485dfc.zip
cpython-5909a494cd3ba43143b28bd439773ed85a485dfc.tar.gz
cpython-5909a494cd3ba43143b28bd439773ed85a485dfc.tar.bz2
bpo-42350: Fix Thread._reset_internal_locks() (GH-23268)
Fix the threading.Thread class at fork: do nothing if the thread is already stopped (ex: fork called at Python exit). Previously, an error was logged in the child process.
Diffstat (limited to 'Lib/test/test_threading.py')
-rw-r--r--Lib/test/test_threading.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py
index db440d4..864cea3 100644
--- a/Lib/test/test_threading.py
+++ b/Lib/test/test_threading.py
@@ -469,6 +469,35 @@ class ThreadTests(BaseTestCase):
t = threading.Thread(daemon=True)
self.assertTrue(t.daemon)
+ @unittest.skipUnless(hasattr(os, 'fork'), 'needs os.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
+ sys.exit()
+ 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')
+
@unittest.skipUnless(hasattr(os, 'fork'), 'test needs fork()')
def test_dummy_thread_after_fork(self):
# Issue #14308: a dummy thread in the active list doesn't mess up