Skip to content

Commit 912407f

Browse files
committed
00371: Revert "bpo-1596321: Fix threading._shutdown() for the main thread (pythonGH-28549) (pythonGH-28589)"
This reverts commit 38c6773. It introduced regression causing FreeIPA's tests to fail. For more info see: https://bodhi.fedoraproject.org/updates/FEDORA-2021-e152ce5f31 GrahamDumpleton/mod_wsgi#730
1 parent ce3e485 commit 912407f

File tree

2 files changed

+8
-50
lines changed

2 files changed

+8
-50
lines changed

Lib/test/test_threading.py

-33
Original file line numberDiff line numberDiff line change
@@ -928,39 +928,6 @@ def test_debug_deprecation(self):
928928
b'is deprecated and will be removed in Python 3.12')
929929
self.assertIn(msg, err)
930930

931-
def test_import_from_another_thread(self):
932-
# bpo-1596321: If the threading module is first import from a thread
933-
# different than the main thread, threading._shutdown() must handle
934-
# this case without logging an error at Python exit.
935-
code = textwrap.dedent('''
936-
import _thread
937-
import sys
938-
939-
event = _thread.allocate_lock()
940-
event.acquire()
941-
942-
def import_threading():
943-
import threading
944-
event.release()
945-
946-
if 'threading' in sys.modules:
947-
raise Exception('threading is already imported')
948-
949-
_thread.start_new_thread(import_threading, ())
950-
951-
# wait until the threading module is imported
952-
event.acquire()
953-
event.release()
954-
955-
if 'threading' not in sys.modules:
956-
raise Exception('threading is not imported')
957-
958-
# don't wait until the thread completes
959-
''')
960-
rc, out, err = assert_python_ok("-c", code)
961-
self.assertEqual(out, b'')
962-
self.assertEqual(err, b'')
963-
964931

965932
class ThreadJoinOnShutdown(BaseTestCase):
966933

Lib/threading.py

+8-17
Original file line numberDiff line numberDiff line change
@@ -1530,29 +1530,20 @@ def _shutdown():
15301530

15311531
global _SHUTTING_DOWN
15321532
_SHUTTING_DOWN = True
1533+
# Main thread
1534+
tlock = _main_thread._tstate_lock
1535+
# The main thread isn't finished yet, so its thread state lock can't have
1536+
# been released.
1537+
assert tlock is not None
1538+
assert tlock.locked()
1539+
tlock.release()
1540+
_main_thread._stop()
15331541

15341542
# Call registered threading atexit functions before threads are joined.
15351543
# Order is reversed, similar to atexit.
15361544
for atexit_call in reversed(_threading_atexits):
15371545
atexit_call()
15381546

1539-
# Main thread
1540-
if _main_thread.ident == get_ident():
1541-
tlock = _main_thread._tstate_lock
1542-
# The main thread isn't finished yet, so its thread state lock can't
1543-
# have been released.
1544-
assert tlock is not None
1545-
assert tlock.locked()
1546-
tlock.release()
1547-
_main_thread._stop()
1548-
else:
1549-
# bpo-1596321: _shutdown() must be called in the main thread.
1550-
# If the threading module was not imported by the main thread,
1551-
# _main_thread is the thread which imported the threading module.
1552-
# In this case, ignore _main_thread, similar behavior than for threads
1553-
# spawned by C libraries or using _thread.start_new_thread().
1554-
pass
1555-
15561547
# Join all non-deamon threads
15571548
while True:
15581549
with _shutdown_locks_lock:

0 commit comments

Comments
 (0)