@@ -2048,6 +2048,48 @@ def reenter(_canonical: str, *, timeout: float, blocking: bool) -> None:
20482048 assert (lock .is_locked , lock .lock_counter ) == (False , 0 )
20492049
20502050
2051+ @NEEDS_PROMPT_SIGNAL_DELIVERY # pragma: needs prompt-signal-delivery
2052+ @pytest .mark .parametrize ("thread_local" , [pytest .param (True , id = "thread_local" ), pytest .param (False , id = "shared" )])
2053+ @pytest .mark .parametrize (
2054+ ("timeout" , "blocking" , "expected" ),
2055+ [
2056+ pytest .param (- 1 , True , RuntimeError , id = "indefinite" ),
2057+ pytest .param (1 , True , Timeout , id = "finite" ),
2058+ pytest .param (- 1 , False , Timeout , id = "nonblocking" ),
2059+ ],
2060+ )
2061+ def test_handler_acquiring_peer_during_poll_detects_deadlock (
2062+ tmp_path : Path , mocker : MockerFixture , thread_local : bool , timeout : float , blocking : bool , expected : type [Exception ]
2063+ ) -> None :
2064+ path = str (tmp_path / "a" )
2065+ outer = SoftFileLock (path , thread_local = thread_local , is_singleton = False )
2066+ peer = SoftFileLock (path , thread_local = thread_local , is_singleton = False )
2067+ original = outer ._acquire_with_fork_tracking
2068+ pending = [signal .SIGINT ]
2069+
2070+ def signal_then_acquire () -> None :
2071+ signals = pending .copy ()
2072+ pending .clear ()
2073+ list (map (signal .raise_signal , signals ))
2074+ original ()
2075+
2076+ def handoff (_signum : int , _frame : FrameType | None ) -> None :
2077+ peer .acquire (blocking = False )
2078+
2079+ mocker .patch .object (outer , "_acquire_with_fork_tracking" , side_effect = signal_then_acquire , autospec = True )
2080+ previous = signal .signal (signal .SIGINT , handoff )
2081+ try :
2082+ with pytest .raises (expected ):
2083+ outer .acquire (timeout = timeout , blocking = blocking , cancel_check = lambda : True )
2084+ assert (outer .is_locked , peer .is_locked ) == (False , True )
2085+ with pytest .raises (RuntimeError , match = "already held by a different" ):
2086+ SoftFileLock (path , is_singleton = False ).acquire (cancel_check = lambda : True )
2087+ finally :
2088+ signal .signal (signal .SIGINT , previous )
2089+ peer .release ()
2090+ outer .release ()
2091+
2092+
20512093@NEEDS_PROMPT_SIGNAL_DELIVERY # pragma: needs prompt-signal-delivery
20522094def test_acquire_from_a_signal_handler_during_an_acquire_fails_fast (tmp_path : Path , mocker : MockerFixture ) -> None :
20532095 lock : Final [SoftFileLock ] = SoftFileLock (str (tmp_path / "a" ))
0 commit comments