Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d3cccd2

Browse files
committed
Issue #11223: Fix test_threadsignals to fail, not hang, when the
non-semaphore implementation of locks is used under POSIX.
1 parent 0def5c6 commit d3cccd2

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

Lib/test/test_threadsignals.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ Tools/Demos
9292
Tests
9393
-----
9494

95+
- Issue #11223: Fix test_threadsignals to fail, not hang, when the
96+
non-semaphore implementation of locks is used under POSIX.
97+
9598
- Issue #10911: Add tests on CGI with non-ASCII characters. Patch written by
9699
Pierre Quentel.
97100

0 commit comments

Comments
 (0)