Skip to content

Commit 36e5f09

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 98bca70 commit 36e5f09

File tree

2 files changed

+8
-50
lines changed

2 files changed

+8
-50
lines changed

Lib/test/test_threading.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -965,39 +965,6 @@ def test_debug_deprecation(self):
965965
b'is deprecated and will be removed in Python 3.12')
966966
self.assertIn(msg, err)
967967

968-
def test_import_from_another_thread(self):
969-
# bpo-1596321: If the threading module is first import from a thread
970-
# different than the main thread, threading._shutdown() must handle
971-
# this case without logging an error at Python exit.
972-
code = textwrap.dedent('''
973-
import _thread
974-
import sys
975-
976-
event = _thread.allocate_lock()
977-
event.acquire()
978-
979-
def import_threading():
980-
import threading
981-
event.release()
982-
983-
if 'threading' in sys.modules:
984-
raise Exception('threading is already imported')
985-
986-
_thread.start_new_thread(import_threading, ())
987-
988-
# wait until the threading module is imported
989-
event.acquire()
990-
event.release()
991-
992-
if 'threading' not in sys.modules:
993-
raise Exception('threading is not imported')
994-
995-
# don't wait until the thread completes
996-
''')
997-
rc, out, err = assert_python_ok("-c", code)
998-
self.assertEqual(out, b'')
999-
self.assertEqual(err, b'')
1000-
1001968

1002969
class ThreadJoinOnShutdown(BaseTestCase):
1003970

Lib/threading.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,29 +1553,20 @@ def _shutdown():
15531553

15541554
global _SHUTTING_DOWN
15551555
_SHUTTING_DOWN = True
1556+
# Main thread
1557+
tlock = _main_thread._tstate_lock
1558+
# The main thread isn't finished yet, so its thread state lock can't have
1559+
# been released.
1560+
assert tlock is not None
1561+
assert tlock.locked()
1562+
tlock.release()
1563+
_main_thread._stop()
15561564

15571565
# Call registered threading atexit functions before threads are joined.
15581566
# Order is reversed, similar to atexit.
15591567
for atexit_call in reversed(_threading_atexits):
15601568
atexit_call()
15611569

1562-
# Main thread
1563-
if _main_thread.ident == get_ident():
1564-
tlock = _main_thread._tstate_lock
1565-
# The main thread isn't finished yet, so its thread state lock can't
1566-
# have been released.
1567-
assert tlock is not None
1568-
assert tlock.locked()
1569-
tlock.release()
1570-
_main_thread._stop()
1571-
else:
1572-
# bpo-1596321: _shutdown() must be called in the main thread.
1573-
# If the threading module was not imported by the main thread,
1574-
# _main_thread is the thread which imported the threading module.
1575-
# In this case, ignore _main_thread, similar behavior than for threads
1576-
# spawned by C libraries or using _thread.start_new_thread().
1577-
pass
1578-
15791570
# Join all non-deamon threads
15801571
while True:
15811572
with _shutdown_locks_lock:

0 commit comments

Comments
 (0)