@@ -4479,19 +4479,32 @@ def check_semaphore_tracker_death(self, signum, should_die):
4479
4479
time .sleep (0.5 ) # give it time to die
4480
4480
old_stderr = sys .stderr
4481
4481
r , w = os .pipe ()
4482
+ # Make the pipe non blocking to not hang indefinitely
4483
+ if sys .platform != "win32" :
4484
+ import fcntl
4485
+ fcntl .fcntl (r , fcntl .F_SETFL ,
4486
+ fcntl .fcntl (r , fcntl .F_GETFL ) | os .O_NONBLOCK )
4487
+ sys .stderr = open (w , "bw" )
4482
4488
try :
4483
- sys .stderr = open (w , "bw" )
4484
4489
with warnings .catch_warnings (record = True ) as all_warn :
4485
4490
_semaphore_tracker .ensure_running ()
4486
4491
pid = _semaphore_tracker ._pid
4487
4492
# Wait until we receive the PONG from the child, indicating that
4488
4493
# the signal handlers have been registered. See bpo-33613 for more
4489
4494
# information.
4490
4495
_semaphore_tracker ._send ("PING" , "" )
4496
+ deadline = time .monotonic () + 5
4491
4497
with open (r , "rb" ) as pipe :
4492
- data = pipe .readline ()
4493
- if b"PONG" not in data :
4494
- raise ValueError ("Invalid data in stderr!" )
4498
+ while True :
4499
+ if time .monotonic () >= deadline :
4500
+ raise TimeoutError ("Reading data "
4501
+ "from pipe took too long" )
4502
+ data = pipe .readline ()
4503
+ if not data :
4504
+ continue
4505
+ if b"PONG" not in data :
4506
+ raise ValueError ("Invalid data in stderr!" )
4507
+ break
4495
4508
finally :
4496
4509
sys .stderr .close ()
4497
4510
sys .stderr = old_stderr
0 commit comments