Skip to content

Commit 40442ec

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 8e9a89a commit 40442ec

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
@@ -1169,39 +1169,6 @@ def noop(): pass
11691169
threading.Thread(target=noop).start()
11701170
# Thread.join() is not called
11711171

1172-
def test_import_from_another_thread(self):
1173-
# bpo-1596321: If the threading module is first import from a thread
1174-
# different than the main thread, threading._shutdown() must handle
1175-
# this case without logging an error at Python exit.
1176-
code = textwrap.dedent('''
1177-
import _thread
1178-
import sys
1179-
1180-
event = _thread.allocate_lock()
1181-
event.acquire()
1182-
1183-
def import_threading():
1184-
import threading
1185-
event.release()
1186-
1187-
if 'threading' in sys.modules:
1188-
raise Exception('threading is already imported')
1189-
1190-
_thread.start_new_thread(import_threading, ())
1191-
1192-
# wait until the threading module is imported
1193-
event.acquire()
1194-
event.release()
1195-
1196-
if 'threading' not in sys.modules:
1197-
raise Exception('threading is not imported')
1198-
1199-
# don't wait until the thread completes
1200-
''')
1201-
rc, out, err = assert_python_ok("-c", code)
1202-
self.assertEqual(out, b'')
1203-
self.assertEqual(err, b'')
1204-
12051172
def test_start_new_thread_at_exit(self):
12061173
code = """if 1:
12071174
import atexit

Lib/threading.py

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

16531653
global _SHUTTING_DOWN
16541654
_SHUTTING_DOWN = True
1655+
# Main thread
1656+
tlock = _main_thread._tstate_lock
1657+
# The main thread isn't finished yet, so its thread state lock can't have
1658+
# been released.
1659+
assert tlock is not None
1660+
if tlock.locked():
1661+
# It should have been released already by
1662+
# _PyInterpreterState_SetNotRunningMain(), but there may be
1663+
# embedders that aren't calling that yet.
1664+
tlock.release()
1665+
_main_thread._stop()
16551666

16561667
# Call registered threading atexit functions before threads are joined.
16571668
# Order is reversed, similar to atexit.
16581669
for atexit_call in reversed(_threading_atexits):
16591670
atexit_call()
16601671

1661-
# Main thread
1662-
if _main_thread.ident == get_ident():
1663-
tlock = _main_thread._tstate_lock
1664-
# The main thread isn't finished yet, so its thread state lock can't
1665-
# have been released.
1666-
assert tlock is not None
1667-
if tlock.locked():
1668-
# It should have been released already by
1669-
# _PyInterpreterState_SetNotRunningMain(), but there may be
1670-
# embedders that aren't calling that yet.
1671-
tlock.release()
1672-
_main_thread._stop()
1673-
else:
1674-
# bpo-1596321: _shutdown() must be called in the main thread.
1675-
# If the threading module was not imported by the main thread,
1676-
# _main_thread is the thread which imported the threading module.
1677-
# In this case, ignore _main_thread, similar behavior than for threads
1678-
# spawned by C libraries or using _thread.start_new_thread().
1679-
pass
1680-
16811672
# Join all non-deamon threads
16821673
while True:
16831674
with _shutdown_locks_lock:

0 commit comments

Comments
 (0)