Skip to content

Commit e2c733e

Browse files
hrnciarbefeleme
authored andcommitted
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 08a7c3e commit e2c733e

File tree

2 files changed

+11
-53
lines changed

2 files changed

+11
-53
lines changed

Lib/test/test_threading.py

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,39 +1056,6 @@ def noop(): pass
10561056
threading.Thread(target=noop).start()
10571057
# Thread.join() is not called
10581058

1059-
def test_import_from_another_thread(self):
1060-
# bpo-1596321: If the threading module is first import from a thread
1061-
# different than the main thread, threading._shutdown() must handle
1062-
# this case without logging an error at Python exit.
1063-
code = textwrap.dedent('''
1064-
import _thread
1065-
import sys
1066-
1067-
event = _thread.allocate_lock()
1068-
event.acquire()
1069-
1070-
def import_threading():
1071-
import threading
1072-
event.release()
1073-
1074-
if 'threading' in sys.modules:
1075-
raise Exception('threading is already imported')
1076-
1077-
_thread.start_new_thread(import_threading, ())
1078-
1079-
# wait until the threading module is imported
1080-
event.acquire()
1081-
event.release()
1082-
1083-
if 'threading' not in sys.modules:
1084-
raise Exception('threading is not imported')
1085-
1086-
# don't wait until the thread completes
1087-
''')
1088-
rc, out, err = assert_python_ok("-c", code)
1089-
self.assertEqual(out, b'')
1090-
self.assertEqual(err, b'')
1091-
10921059
def test_start_new_thread_at_exit(self):
10931060
code = """if 1:
10941061
import atexit

Lib/threading.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,32 +1616,23 @@ def _shutdown():
16161616

16171617
global _SHUTTING_DOWN
16181618
_SHUTTING_DOWN = True
1619+
# Main thread
1620+
tlock = _main_thread._tstate_lock
1621+
# The main thread isn't finished yet, so its thread state lock can't have
1622+
# been released.
1623+
assert tlock is not None
1624+
if tlock.locked():
1625+
# It should have been released already by
1626+
# _PyInterpreterState_SetNotRunningMain(), but there may be
1627+
# embedders that aren't calling that yet.
1628+
tlock.release()
1629+
_main_thread._stop()
16191630

16201631
# Call registered threading atexit functions before threads are joined.
16211632
# Order is reversed, similar to atexit.
16221633
for atexit_call in reversed(_threading_atexits):
16231634
atexit_call()
16241635

1625-
# Main thread
1626-
if _main_thread.ident == get_ident():
1627-
tlock = _main_thread._tstate_lock
1628-
# The main thread isn't finished yet, so its thread state lock can't
1629-
# have been released.
1630-
assert tlock is not None
1631-
if tlock.locked():
1632-
# It should have been released already by
1633-
# _PyInterpreterState_SetNotRunningMain(), but there may be
1634-
# embedders that aren't calling that yet.
1635-
tlock.release()
1636-
_main_thread._stop()
1637-
else:
1638-
# bpo-1596321: _shutdown() must be called in the main thread.
1639-
# If the threading module was not imported by the main thread,
1640-
# _main_thread is the thread which imported the threading module.
1641-
# In this case, ignore _main_thread, similar behavior than for threads
1642-
# spawned by C libraries or using _thread.start_new_thread().
1643-
pass
1644-
16451636
# Join all non-deamon threads
16461637
while True:
16471638
with _shutdown_locks_lock:

0 commit comments

Comments
 (0)