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