@@ -73,18 +73,29 @@ def alarm_interrupt(self, sig, frame):
7373 def test_lock_acquire_interruption (self ):
7474 # Mimic receiving a SIGINT (KeyboardInterrupt) with SIGALRM while stuck
7575 # in a deadlock.
76+ # XXX this test can fail when the legacy (non-semaphore) implementation
77+ # of locks is used in thread_pthread.h, see issue #11223.
7678 oldalrm = signal .signal (signal .SIGALRM , self .alarm_interrupt )
7779 try :
7880 lock = thread .allocate_lock ()
7981 lock .acquire ()
8082 signal .alarm (1 )
81- self .assertRaises (KeyboardInterrupt , lock .acquire )
83+ t1 = time .time ()
84+ self .assertRaises (KeyboardInterrupt , lock .acquire , timeout = 5 )
85+ dt = time .time () - t1
86+ # Checking that KeyboardInterrupt was raised is not sufficient.
87+ # We want to assert that lock.acquire() was interrupted because
88+ # of the signal, not that the signal handler was called immediately
89+ # after timeout return of lock.acquire() (which can fool assertRaises).
90+ self .assertLess (dt , 3.0 )
8291 finally :
8392 signal .signal (signal .SIGALRM , oldalrm )
8493
8594 def test_rlock_acquire_interruption (self ):
8695 # Mimic receiving a SIGINT (KeyboardInterrupt) with SIGALRM while stuck
8796 # in a deadlock.
97+ # XXX this test can fail when the legacy (non-semaphore) implementation
98+ # of locks is used in thread_pthread.h, see issue #11223.
8899 oldalrm = signal .signal (signal .SIGALRM , self .alarm_interrupt )
89100 try :
90101 rlock = thread .RLock ()
@@ -98,7 +109,11 @@ def other_thread():
98109 rlock .release ()
99110 time .sleep (0.01 )
100111 signal .alarm (1 )
101- self .assertRaises (KeyboardInterrupt , rlock .acquire )
112+ t1 = time .time ()
113+ self .assertRaises (KeyboardInterrupt , rlock .acquire , timeout = 5 )
114+ dt = time .time () - t1
115+ # See rationale above in test_lock_acquire_interruption
116+ self .assertLess (dt , 3.0 )
102117 finally :
103118 signal .signal (signal .SIGALRM , oldalrm )
104119
0 commit comments