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