@@ -1370,43 +1370,75 @@ def read(self, n=-1):
13701370 def check_daemon_threads_shutdown_deadlock (self , stream_name ):
13711371 # Issue #23309: deadlocks at shutdown should be avoided when a
13721372 # daemon thread and the main thread both write to a file.
1373+ #
1374+ # Focus on I/O and interaction in locks, test_threading checks daemon
1375+ # threads exit generally. Get into the specific I/O states we care
1376+ # about by using mock I/O which block in the right locks.
13731377 code = """if 1:
1378+ import atexit
1379+ import io
13741380 import sys
1375- import time
1376- import threading
1381+ from threading import current_thread, Event, main_thread, Thread
1382+
1383+ go = Event()
1384+ entered = Event()
1385+
1386+ # Register with atexit before importing test.support which imports
1387+ # logging so that it runs last.
1388+ @atexit.register
1389+ def release_daemon_thread():
1390+ # Get daemon thread to hold BufferedWriter lock.
1391+ go.set()
1392+ entered.wait()
1393+
13771394 from test.support import SuppressCrashReport
13781395
1379- file = sys.{stream_name}
1396+ # Mock I/O that blocks daemon thread in Raw I/O write.
1397+ #
1398+ # BufferedIO holds a lock while doing I/O operations. Use a
1399+ # threading Event to simulate a thread doing a slow I/O which holds
1400+ # the lock on sys.stdout, sys.stderr blocking normal cleanup of
1401+ # those streams during interpreter finalization.
1402+ class GatedRaw(io.RawIOBase):
1403+ name = '<{stream_name}>'
1404+
1405+ def writable(self):
1406+ return True
1407+
1408+ def write(self, b):
1409+ if current_thread() is not main_thread():
1410+ # In daemon, have buffered lock. Hold it.
1411+ entered.set()
1412+ Event().wait()
1413+ return len(b)
1414+
1415+ file = io.TextIOWrapper(io.BufferedWriter(GatedRaw()),
1416+ encoding='utf-8')
1417+ sys.{stream_name} = file
13801418
13811419 def run():
1382- while True:
1383- file.write('.')
1384- file.flush()
1420+ go.wait()
1421+ file.write('.')
1422+ file.flush()
13851423
13861424 crash = SuppressCrashReport()
13871425 crash.__enter__()
13881426 # don't call __exit__(): the crash occurs at Python shutdown
13891427
1390- thread = threading. Thread(target=run)
1428+ thread = Thread(target=run)
13911429 thread.daemon = True
13921430 thread.start()
13931431
1394- time.sleep(0.5)
13951432 file.write('!')
13961433 file.flush()
1397- """ .format_map ( locals () )
1434+ """ .format ( stream_name = stream_name )
13981435 res , _ = run_python_until_end ("-c" , code )
13991436 err = res .err .decode ()
1400- if res .rc != 0 :
1401- # Failure: should be a fatal error
1402- pattern = (r"Fatal Python error: _enter_buffered_busy: "
1403- r"could not acquire lock "
1404- r"for <(_io\.)?BufferedWriter name='<{stream_name}>'> "
1405- r"at interpreter shutdown, possibly due to "
1406- r"daemon threads" .format_map (locals ()))
1407- self .assertRegex (err , pattern )
1408- else :
1409- self .assertFalse (err .strip ('.!' ))
1437+ # Should exit non-zero with fatal error.
1438+ self .assertNotEqual (res .rc , 0 )
1439+ self .assertIn ("Fatal Python error: _enter_buffered_busy" , err )
1440+ self .assertIn (f"<_io.BufferedWriter name='<{ stream_name } >'>" , err )
1441+ self .assertIn ("possibly due to daemon threads" , err )
14101442
14111443 @threading_helper .requires_working_threading ()
14121444 @support .requires_resource ('walltime' )
0 commit comments