From 38c67738c64304928c68d5c2bd78bbb01d979b94 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Mon, 27 Sep 2021 14:39:35 -0700 Subject: bpo-1596321: Fix threading._shutdown() for the main thread (GH-28549) Fix the threading._shutdown() function when the threading module was imported first from a thread different than the main thread: no longer log an error at Python exit. (cherry picked from commit 95d31370829b7d729667588e0a9943217401ea5b) Co-authored-by: Victor Stinner --- Lib/test/test_threading.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'Lib/test') diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py index c51de6f4b8..c54806e594 100644 --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -928,6 +928,39 @@ class ThreadTests(BaseTestCase): b'is deprecated and will be removed in Python 3.12') self.assertIn(msg, err) + def test_import_from_another_thread(self): + # bpo-1596321: If the threading module is first import from a thread + # different than the main thread, threading._shutdown() must handle + # this case without logging an error at Python exit. + code = textwrap.dedent(''' + import _thread + import sys + + event = _thread.allocate_lock() + event.acquire() + + def import_threading(): + import threading + event.release() + + if 'threading' in sys.modules: + raise Exception('threading is already imported') + + _thread.start_new_thread(import_threading, ()) + + # wait until the threading module is imported + event.acquire() + event.release() + + if 'threading' not in sys.modules: + raise Exception('threading is not imported') + + # don't wait until the thread completes + ''') + rc, out, err = assert_python_ok("-c", code) + self.assertEqual(out, b'') + self.assertEqual(err, b'') + class ThreadJoinOnShutdown(BaseTestCase): -- cgit v1.2.1