Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 1 | """PyUnit testing that threads honor our signal semantics""" |
| 2 | |
| 3 | import unittest |
Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 4 | import signal |
| 5 | import os |
Fred Drake | 4818748 | 2004-08-03 16:14:13 +0000 | [diff] [blame] | 6 | import sys |
Victor Stinner | ff40ecd | 2017-09-14 13:07:24 -0700 | [diff] [blame] | 7 | from test import support |
Hai Shi | e80697d | 2020-05-28 06:10:27 +0800 | [diff] [blame] | 8 | from test.support import threading_helper |
Antoine Pitrou | 88c60c9 | 2017-09-18 23:50:44 +0200 | [diff] [blame] | 9 | import _thread as thread |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 10 | import time |
Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 11 | |
Christian Heimes | de0b962 | 2012-11-19 00:59:39 +0100 | [diff] [blame] | 12 | if (sys.platform[:3] == 'win'): |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 13 | raise unittest.SkipTest("Can't test signal on %s" % sys.platform) |
Michael W. Hudson | 34fba3b | 2004-08-03 15:35:29 +0000 | [diff] [blame] | 14 | |
Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 15 | process_pid = os.getpid() |
| 16 | signalled_all=thread.allocate_lock() |
| 17 | |
Victor Stinner | d5c355c | 2011-04-30 14:53:09 +0200 | [diff] [blame] | 18 | USING_PTHREAD_COND = (sys.thread_info.name == 'pthread' |
| 19 | and sys.thread_info.lock == 'mutex+cond') |
Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 21 | def registerSignals(for_usr1, for_usr2, for_alrm): |
Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 22 | usr1 = signal.signal(signal.SIGUSR1, for_usr1) |
| 23 | usr2 = signal.signal(signal.SIGUSR2, for_usr2) |
| 24 | alrm = signal.signal(signal.SIGALRM, for_alrm) |
| 25 | return usr1, usr2, alrm |
| 26 | |
| 27 | |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 28 | # The signal handler. Just note that the signal occurred and |
Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 29 | # from who. |
| 30 | def handle_signals(sig,frame): |
Tim Peters | 6db15d7 | 2004-08-04 02:36:18 +0000 | [diff] [blame] | 31 | signal_blackboard[sig]['tripped'] += 1 |
Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 32 | signal_blackboard[sig]['tripped_by'] = thread.get_ident() |
| 33 | |
| 34 | # a function that will be spawned as a separate thread. |
| 35 | def send_signals(): |
| 36 | os.kill(process_pid, signal.SIGUSR1) |
| 37 | os.kill(process_pid, signal.SIGUSR2) |
| 38 | signalled_all.release() |
| 39 | |
| 40 | class ThreadSignals(unittest.TestCase): |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 41 | |
Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 42 | def test_signals(self): |
Hai Shi | e80697d | 2020-05-28 06:10:27 +0800 | [diff] [blame] | 43 | with threading_helper.wait_threads_exit(): |
Victor Stinner | ff40ecd | 2017-09-14 13:07:24 -0700 | [diff] [blame] | 44 | # Test signal handling semantics of threads. |
| 45 | # We spawn a thread, have the thread send two signals, and |
| 46 | # wait for it to finish. Check that we got both signals |
| 47 | # and that they were run by the main thread. |
| 48 | signalled_all.acquire() |
| 49 | self.spawnSignallingThread() |
| 50 | signalled_all.acquire() |
| 51 | |
Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 52 | # the signals that we asked the kernel to send |
| 53 | # will come back, but we don't know when. |
| 54 | # (it might even be after the thread exits |
| 55 | # and might be out of order.) If we haven't seen |
| 56 | # the signals yet, send yet another signal and |
| 57 | # wait for it return. |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 58 | if signal_blackboard[signal.SIGUSR1]['tripped'] == 0 \ |
Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 59 | or signal_blackboard[signal.SIGUSR2]['tripped'] == 0: |
Victor Stinner | 9abee72 | 2017-09-19 09:36:54 -0700 | [diff] [blame] | 60 | try: |
| 61 | signal.alarm(1) |
| 62 | signal.pause() |
| 63 | finally: |
| 64 | signal.alarm(0) |
Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 65 | |
| 66 | self.assertEqual( signal_blackboard[signal.SIGUSR1]['tripped'], 1) |
Tim Peters | 6db15d7 | 2004-08-04 02:36:18 +0000 | [diff] [blame] | 67 | self.assertEqual( signal_blackboard[signal.SIGUSR1]['tripped_by'], |
Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 68 | thread.get_ident()) |
| 69 | self.assertEqual( signal_blackboard[signal.SIGUSR2]['tripped'], 1) |
Tim Peters | 6db15d7 | 2004-08-04 02:36:18 +0000 | [diff] [blame] | 70 | self.assertEqual( signal_blackboard[signal.SIGUSR2]['tripped_by'], |
Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 71 | thread.get_ident()) |
Michael W. Hudson | 574a251 | 2004-08-04 14:22:56 +0000 | [diff] [blame] | 72 | signalled_all.release() |
Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 73 | |
| 74 | def spawnSignallingThread(self): |
| 75 | thread.start_new_thread(send_signals, ()) |
Tim Peters | 6db15d7 | 2004-08-04 02:36:18 +0000 | [diff] [blame] | 76 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 77 | def alarm_interrupt(self, sig, frame): |
| 78 | raise KeyboardInterrupt |
| 79 | |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 80 | @unittest.skipIf(USING_PTHREAD_COND, |
| 81 | 'POSIX condition variables cannot be interrupted') |
Benjamin Peterson | 5b10d51 | 2018-09-12 13:48:03 -0700 | [diff] [blame] | 82 | @unittest.skipIf(sys.platform.startswith('linux') and |
| 83 | not sys.thread_info.version, |
| 84 | 'Issue 34004: musl does not allow interruption of locks ' |
| 85 | 'by signals.') |
Victor Stinner | 7d02d50 | 2014-02-18 09:19:48 +0100 | [diff] [blame] | 86 | # Issue #20564: sem_timedwait() cannot be interrupted on OpenBSD |
| 87 | @unittest.skipIf(sys.platform.startswith('openbsd'), |
| 88 | 'lock cannot be interrupted on OpenBSD') |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 89 | def test_lock_acquire_interruption(self): |
| 90 | # Mimic receiving a SIGINT (KeyboardInterrupt) with SIGALRM while stuck |
| 91 | # in a deadlock. |
Antoine Pitrou | d3cccd2 | 2011-03-13 19:14:21 +0100 | [diff] [blame] | 92 | # XXX this test can fail when the legacy (non-semaphore) implementation |
| 93 | # of locks is used in thread_pthread.h, see issue #11223. |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 94 | oldalrm = signal.signal(signal.SIGALRM, self.alarm_interrupt) |
| 95 | try: |
| 96 | lock = thread.allocate_lock() |
| 97 | lock.acquire() |
| 98 | signal.alarm(1) |
Victor Stinner | 2cf4c20 | 2018-12-17 09:36:36 +0100 | [diff] [blame] | 99 | t1 = time.monotonic() |
Antoine Pitrou | d3cccd2 | 2011-03-13 19:14:21 +0100 | [diff] [blame] | 100 | self.assertRaises(KeyboardInterrupt, lock.acquire, timeout=5) |
Victor Stinner | 2cf4c20 | 2018-12-17 09:36:36 +0100 | [diff] [blame] | 101 | dt = time.monotonic() - t1 |
Antoine Pitrou | d3cccd2 | 2011-03-13 19:14:21 +0100 | [diff] [blame] | 102 | # Checking that KeyboardInterrupt was raised is not sufficient. |
| 103 | # We want to assert that lock.acquire() was interrupted because |
| 104 | # of the signal, not that the signal handler was called immediately |
| 105 | # after timeout return of lock.acquire() (which can fool assertRaises). |
| 106 | self.assertLess(dt, 3.0) |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 107 | finally: |
Victor Stinner | 9abee72 | 2017-09-19 09:36:54 -0700 | [diff] [blame] | 108 | signal.alarm(0) |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 109 | signal.signal(signal.SIGALRM, oldalrm) |
| 110 | |
Victor Stinner | 754851f | 2011-04-19 23:58:51 +0200 | [diff] [blame] | 111 | @unittest.skipIf(USING_PTHREAD_COND, |
| 112 | 'POSIX condition variables cannot be interrupted') |
Benjamin Peterson | 5b10d51 | 2018-09-12 13:48:03 -0700 | [diff] [blame] | 113 | @unittest.skipIf(sys.platform.startswith('linux') and |
| 114 | not sys.thread_info.version, |
| 115 | 'Issue 34004: musl does not allow interruption of locks ' |
| 116 | 'by signals.') |
Victor Stinner | 7d02d50 | 2014-02-18 09:19:48 +0100 | [diff] [blame] | 117 | # Issue #20564: sem_timedwait() cannot be interrupted on OpenBSD |
| 118 | @unittest.skipIf(sys.platform.startswith('openbsd'), |
| 119 | 'lock cannot be interrupted on OpenBSD') |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 120 | def test_rlock_acquire_interruption(self): |
| 121 | # Mimic receiving a SIGINT (KeyboardInterrupt) with SIGALRM while stuck |
| 122 | # in a deadlock. |
Antoine Pitrou | d3cccd2 | 2011-03-13 19:14:21 +0100 | [diff] [blame] | 123 | # XXX this test can fail when the legacy (non-semaphore) implementation |
| 124 | # of locks is used in thread_pthread.h, see issue #11223. |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 125 | oldalrm = signal.signal(signal.SIGALRM, self.alarm_interrupt) |
| 126 | try: |
| 127 | rlock = thread.RLock() |
| 128 | # For reentrant locks, the initial acquisition must be in another |
| 129 | # thread. |
| 130 | def other_thread(): |
| 131 | rlock.acquire() |
Victor Stinner | ff40ecd | 2017-09-14 13:07:24 -0700 | [diff] [blame] | 132 | |
Hai Shi | e80697d | 2020-05-28 06:10:27 +0800 | [diff] [blame] | 133 | with threading_helper.wait_threads_exit(): |
Victor Stinner | ff40ecd | 2017-09-14 13:07:24 -0700 | [diff] [blame] | 134 | thread.start_new_thread(other_thread, ()) |
| 135 | # Wait until we can't acquire it without blocking... |
| 136 | while rlock.acquire(blocking=False): |
| 137 | rlock.release() |
| 138 | time.sleep(0.01) |
| 139 | signal.alarm(1) |
Victor Stinner | 2cf4c20 | 2018-12-17 09:36:36 +0100 | [diff] [blame] | 140 | t1 = time.monotonic() |
Victor Stinner | ff40ecd | 2017-09-14 13:07:24 -0700 | [diff] [blame] | 141 | self.assertRaises(KeyboardInterrupt, rlock.acquire, timeout=5) |
Victor Stinner | 2cf4c20 | 2018-12-17 09:36:36 +0100 | [diff] [blame] | 142 | dt = time.monotonic() - t1 |
Victor Stinner | ff40ecd | 2017-09-14 13:07:24 -0700 | [diff] [blame] | 143 | # See rationale above in test_lock_acquire_interruption |
| 144 | self.assertLess(dt, 3.0) |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 145 | finally: |
Victor Stinner | 9abee72 | 2017-09-19 09:36:54 -0700 | [diff] [blame] | 146 | signal.alarm(0) |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 147 | signal.signal(signal.SIGALRM, oldalrm) |
| 148 | |
| 149 | def acquire_retries_on_intr(self, lock): |
| 150 | self.sig_recvd = False |
| 151 | def my_handler(signal, frame): |
| 152 | self.sig_recvd = True |
Victor Stinner | ff40ecd | 2017-09-14 13:07:24 -0700 | [diff] [blame] | 153 | |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 154 | old_handler = signal.signal(signal.SIGUSR1, my_handler) |
| 155 | try: |
| 156 | def other_thread(): |
| 157 | # Acquire the lock in a non-main thread, so this test works for |
| 158 | # RLocks. |
| 159 | lock.acquire() |
| 160 | # Wait until the main thread is blocked in the lock acquire, and |
| 161 | # then wake it up with this. |
| 162 | time.sleep(0.5) |
| 163 | os.kill(process_pid, signal.SIGUSR1) |
| 164 | # Let the main thread take the interrupt, handle it, and retry |
| 165 | # the lock acquisition. Then we'll let it run. |
| 166 | time.sleep(0.5) |
| 167 | lock.release() |
Victor Stinner | ff40ecd | 2017-09-14 13:07:24 -0700 | [diff] [blame] | 168 | |
Hai Shi | e80697d | 2020-05-28 06:10:27 +0800 | [diff] [blame] | 169 | with threading_helper.wait_threads_exit(): |
Victor Stinner | ff40ecd | 2017-09-14 13:07:24 -0700 | [diff] [blame] | 170 | thread.start_new_thread(other_thread, ()) |
| 171 | # Wait until we can't acquire it without blocking... |
| 172 | while lock.acquire(blocking=False): |
| 173 | lock.release() |
| 174 | time.sleep(0.01) |
| 175 | result = lock.acquire() # Block while we receive a signal. |
| 176 | self.assertTrue(self.sig_recvd) |
| 177 | self.assertTrue(result) |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 178 | finally: |
| 179 | signal.signal(signal.SIGUSR1, old_handler) |
| 180 | |
| 181 | def test_lock_acquire_retries_on_intr(self): |
| 182 | self.acquire_retries_on_intr(thread.allocate_lock()) |
| 183 | |
| 184 | def test_rlock_acquire_retries_on_intr(self): |
| 185 | self.acquire_retries_on_intr(thread.RLock()) |
| 186 | |
| 187 | def test_interrupted_timed_acquire(self): |
| 188 | # Test to make sure we recompute lock acquisition timeouts when we |
| 189 | # receive a signal. Check this by repeatedly interrupting a lock |
| 190 | # acquire in the main thread, and make sure that the lock acquire times |
| 191 | # out after the right amount of time. |
Antoine Pitrou | 4fef555 | 2010-12-15 23:38:50 +0000 | [diff] [blame] | 192 | # NOTE: this test only behaves as expected if C signals get delivered |
| 193 | # to the main thread. Otherwise lock.acquire() itself doesn't get |
| 194 | # interrupted and the test trivially succeeds. |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 195 | self.start = None |
| 196 | self.end = None |
| 197 | self.sigs_recvd = 0 |
| 198 | done = thread.allocate_lock() |
| 199 | done.acquire() |
| 200 | lock = thread.allocate_lock() |
| 201 | lock.acquire() |
| 202 | def my_handler(signum, frame): |
| 203 | self.sigs_recvd += 1 |
| 204 | old_handler = signal.signal(signal.SIGUSR1, my_handler) |
| 205 | try: |
| 206 | def timed_acquire(): |
Victor Stinner | 2cf4c20 | 2018-12-17 09:36:36 +0100 | [diff] [blame] | 207 | self.start = time.monotonic() |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 208 | lock.acquire(timeout=0.5) |
Victor Stinner | 2cf4c20 | 2018-12-17 09:36:36 +0100 | [diff] [blame] | 209 | self.end = time.monotonic() |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 210 | def send_signals(): |
| 211 | for _ in range(40): |
Antoine Pitrou | 4fef555 | 2010-12-15 23:38:50 +0000 | [diff] [blame] | 212 | time.sleep(0.02) |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 213 | os.kill(process_pid, signal.SIGUSR1) |
| 214 | done.release() |
| 215 | |
Hai Shi | e80697d | 2020-05-28 06:10:27 +0800 | [diff] [blame] | 216 | with threading_helper.wait_threads_exit(): |
Victor Stinner | ff40ecd | 2017-09-14 13:07:24 -0700 | [diff] [blame] | 217 | # Send the signals from the non-main thread, since the main thread |
| 218 | # is the only one that can process signals. |
| 219 | thread.start_new_thread(send_signals, ()) |
| 220 | timed_acquire() |
| 221 | # Wait for thread to finish |
| 222 | done.acquire() |
| 223 | # This allows for some timing and scheduling imprecision |
| 224 | self.assertLess(self.end - self.start, 2.0) |
| 225 | self.assertGreater(self.end - self.start, 0.3) |
| 226 | # If the signal is received several times before PyErr_CheckSignals() |
| 227 | # is called, the handler will get called less than 40 times. Just |
| 228 | # check it's been called at least once. |
| 229 | self.assertGreater(self.sigs_recvd, 0) |
Antoine Pitrou | 810023d | 2010-12-15 22:59:16 +0000 | [diff] [blame] | 230 | finally: |
| 231 | signal.signal(signal.SIGUSR1, old_handler) |
| 232 | |
Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 233 | |
| 234 | def test_main(): |
Michael W. Hudson | 574a251 | 2004-08-04 14:22:56 +0000 | [diff] [blame] | 235 | global signal_blackboard |
Tim Peters | d1b7827 | 2004-08-07 06:03:09 +0000 | [diff] [blame] | 236 | |
Michael W. Hudson | 574a251 | 2004-08-04 14:22:56 +0000 | [diff] [blame] | 237 | signal_blackboard = { signal.SIGUSR1 : {'tripped': 0, 'tripped_by': 0 }, |
| 238 | signal.SIGUSR2 : {'tripped': 0, 'tripped_by': 0 }, |
| 239 | signal.SIGALRM : {'tripped': 0, 'tripped_by': 0 } } |
| 240 | |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 241 | oldsigs = registerSignals(handle_signals, handle_signals, handle_signals) |
Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 242 | try: |
Victor Stinner | ff40ecd | 2017-09-14 13:07:24 -0700 | [diff] [blame] | 243 | support.run_unittest(ThreadSignals) |
Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 244 | finally: |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 245 | registerSignals(*oldsigs) |
Michael W. Hudson | 43220ea | 2004-08-03 14:37:14 +0000 | [diff] [blame] | 246 | |
| 247 | if __name__ == '__main__': |
| 248 | test_main() |