Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Raymond Hettinger | 722d8c3 | 2009-06-18 22:21:03 +0000 | [diff] [blame] | 3 | from contextlib import closing |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 4 | import gc |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 5 | import pickle |
| 6 | import select |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 7 | import signal |
Victor Stinner | d49b1f1 | 2011-05-08 02:03:15 +0200 | [diff] [blame] | 8 | import struct |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 9 | import subprocess |
| 10 | import traceback |
Christian Heimes | c06950e | 2008-02-28 21:17:00 +0000 | [diff] [blame] | 11 | import sys, os, time, errno |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 12 | from test.script_helper import assert_python_ok |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 13 | try: |
| 14 | import threading |
| 15 | except ImportError: |
| 16 | threading = None |
Christian Heimes | c06950e | 2008-02-28 21:17:00 +0000 | [diff] [blame] | 17 | |
Brian Curtin | 912443c | 2010-09-06 16:10:04 +0000 | [diff] [blame] | 18 | if sys.platform in ('os2', 'riscos'): |
| 19 | raise unittest.SkipTest("Can't test signal on %s" % sys.platform) |
Christian Heimes | c06950e | 2008-02-28 21:17:00 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | cc5a91d | 1997-04-16 00:29:15 +0000 | [diff] [blame] | 21 | |
Armin Rigo | 8b2cbfd | 2004-08-07 21:27:43 +0000 | [diff] [blame] | 22 | class HandlerBCalled(Exception): |
| 23 | pass |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 24 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 25 | |
| 26 | def exit_subprocess(): |
| 27 | """Use os._exit(0) to exit the current subprocess. |
| 28 | |
| 29 | Otherwise, the test catches the SystemExit and continues executing |
| 30 | in parallel with the original test, so you wind up with an |
| 31 | exponential number of tests running concurrently. |
| 32 | """ |
| 33 | os._exit(0) |
| 34 | |
| 35 | |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 36 | def ignoring_eintr(__func, *args, **kwargs): |
| 37 | try: |
| 38 | return __func(*args, **kwargs) |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 39 | except EnvironmentError as e: |
| 40 | if e.errno != errno.EINTR: |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 41 | raise |
| 42 | return None |
| 43 | |
| 44 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 45 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 46 | class InterProcessSignalTests(unittest.TestCase): |
| 47 | MAX_DURATION = 20 # Entire test should last at most 20 sec. |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 48 | |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 49 | def setUp(self): |
| 50 | self.using_gc = gc.isenabled() |
| 51 | gc.disable() |
| 52 | |
| 53 | def tearDown(self): |
| 54 | if self.using_gc: |
| 55 | gc.enable() |
| 56 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 57 | def format_frame(self, frame, limit=None): |
| 58 | return ''.join(traceback.format_stack(frame, limit=limit)) |
| 59 | |
| 60 | def handlerA(self, signum, frame): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 61 | self.a_called = True |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 62 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 63 | def handlerB(self, signum, frame): |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 64 | self.b_called = True |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 65 | raise HandlerBCalled(signum, self.format_frame(frame)) |
Neal Norwitz | 9730bcb | 2006-01-23 07:50:06 +0000 | [diff] [blame] | 66 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 67 | def wait(self, child): |
| 68 | """Wait for child to finish, ignoring EINTR.""" |
| 69 | while True: |
| 70 | try: |
| 71 | child.wait() |
| 72 | return |
| 73 | except OSError as e: |
| 74 | if e.errno != errno.EINTR: |
| 75 | raise |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 76 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 77 | def run_test(self): |
| 78 | # Install handlers. This function runs in a sub-process, so we |
| 79 | # don't worry about re-setting the default handlers. |
| 80 | signal.signal(signal.SIGHUP, self.handlerA) |
| 81 | signal.signal(signal.SIGUSR1, self.handlerB) |
| 82 | signal.signal(signal.SIGUSR2, signal.SIG_IGN) |
| 83 | signal.signal(signal.SIGALRM, signal.default_int_handler) |
Michael W. Hudson | 5c26e86 | 2004-06-11 18:09:28 +0000 | [diff] [blame] | 84 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 85 | # Variables the signals will modify: |
| 86 | self.a_called = False |
| 87 | self.b_called = False |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 88 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 89 | # Let the sub-processes know who to send signals to. |
| 90 | pid = os.getpid() |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 91 | |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 92 | child = ignoring_eintr(subprocess.Popen, ['kill', '-HUP', str(pid)]) |
| 93 | if child: |
| 94 | self.wait(child) |
| 95 | if not self.a_called: |
| 96 | time.sleep(1) # Give the signal time to be delivered. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 97 | self.assertTrue(self.a_called) |
| 98 | self.assertFalse(self.b_called) |
| 99 | self.a_called = False |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 100 | |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 101 | # Make sure the signal isn't delivered while the previous |
| 102 | # Popen object is being destroyed, because __del__ swallows |
| 103 | # exceptions. |
| 104 | del child |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 105 | try: |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 106 | child = subprocess.Popen(['kill', '-USR1', str(pid)]) |
| 107 | # This wait should be interrupted by the signal's exception. |
| 108 | self.wait(child) |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 109 | time.sleep(1) # Give the signal time to be delivered. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 110 | self.fail('HandlerBCalled exception not thrown') |
| 111 | except HandlerBCalled: |
| 112 | self.assertTrue(self.b_called) |
| 113 | self.assertFalse(self.a_called) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 114 | |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 115 | child = ignoring_eintr(subprocess.Popen, ['kill', '-USR2', str(pid)]) |
| 116 | if child: |
| 117 | self.wait(child) # Nothing should happen. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 118 | |
| 119 | try: |
| 120 | signal.alarm(1) |
| 121 | # The race condition in pause doesn't matter in this case, |
| 122 | # since alarm is going to raise a KeyboardException, which |
| 123 | # will skip the call. |
| 124 | signal.pause() |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 125 | # But if another signal arrives before the alarm, pause |
| 126 | # may return early. |
| 127 | time.sleep(1) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 128 | except KeyboardInterrupt: |
Victor Stinner | 3a7f0f0 | 2011-05-08 02:10:36 +0200 | [diff] [blame] | 129 | pass |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 130 | except: |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 131 | self.fail("Some other exception woke us from pause: %s" % |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 132 | traceback.format_exc()) |
| 133 | else: |
Benjamin Peterson | ad9d48d | 2008-04-02 21:49:44 +0000 | [diff] [blame] | 134 | self.fail("pause returned of its own accord, and the signal" |
| 135 | " didn't arrive after another second.") |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 136 | |
R. David Murray | 44546f8 | 2010-04-21 01:59:28 +0000 | [diff] [blame] | 137 | # Issue 3864, unknown if this affects earlier versions of freebsd also |
| 138 | @unittest.skipIf(sys.platform=='freebsd6', |
| 139 | 'inter process signals not reliable (do not mix well with threading) ' |
| 140 | 'on freebsd6') |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 141 | def test_main(self): |
| 142 | # This function spawns a child process to insulate the main |
| 143 | # test-running process from all the signals. It then |
| 144 | # communicates with that child process over a pipe and |
| 145 | # re-raises information about any exceptions the child |
| 146 | # throws. The real work happens in self.run_test(). |
| 147 | os_done_r, os_done_w = os.pipe() |
Raymond Hettinger | 686057b | 2009-06-04 00:11:54 +0000 | [diff] [blame] | 148 | with closing(os.fdopen(os_done_r, 'rb')) as done_r, \ |
| 149 | closing(os.fdopen(os_done_w, 'wb')) as done_w: |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 150 | child = os.fork() |
| 151 | if child == 0: |
| 152 | # In the child process; run the test and report results |
| 153 | # through the pipe. |
| 154 | try: |
| 155 | done_r.close() |
| 156 | # Have to close done_w again here because |
| 157 | # exit_subprocess() will skip the enclosing with block. |
| 158 | with closing(done_w): |
| 159 | try: |
| 160 | self.run_test() |
| 161 | except: |
| 162 | pickle.dump(traceback.format_exc(), done_w) |
| 163 | else: |
| 164 | pickle.dump(None, done_w) |
| 165 | except: |
| 166 | print('Uh oh, raised from pickle.') |
| 167 | traceback.print_exc() |
| 168 | finally: |
| 169 | exit_subprocess() |
| 170 | |
| 171 | done_w.close() |
| 172 | # Block for up to MAX_DURATION seconds for the test to finish. |
| 173 | r, w, x = select.select([done_r], [], [], self.MAX_DURATION) |
| 174 | if done_r in r: |
| 175 | tb = pickle.load(done_r) |
| 176 | if tb: |
| 177 | self.fail(tb) |
| 178 | else: |
| 179 | os.kill(child, signal.SIGKILL) |
| 180 | self.fail('Test deadlocked after %d seconds.' % |
| 181 | self.MAX_DURATION) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 182 | |
| 183 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 184 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 185 | class PosixTests(unittest.TestCase): |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 186 | def trivial_signal_handler(self, *args): |
| 187 | pass |
| 188 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 189 | def test_out_of_range_signal_number_raises_error(self): |
| 190 | self.assertRaises(ValueError, signal.getsignal, 4242) |
| 191 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 192 | self.assertRaises(ValueError, signal.signal, 4242, |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 193 | self.trivial_signal_handler) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 194 | |
| 195 | def test_setting_signal_handler_to_none_raises_error(self): |
| 196 | self.assertRaises(TypeError, signal.signal, |
| 197 | signal.SIGUSR1, None) |
| 198 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 199 | def test_getsignal(self): |
| 200 | hup = signal.signal(signal.SIGHUP, self.trivial_signal_handler) |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 201 | self.assertEqual(signal.getsignal(signal.SIGHUP), |
| 202 | self.trivial_signal_handler) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 203 | signal.signal(signal.SIGHUP, hup) |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 204 | self.assertEqual(signal.getsignal(signal.SIGHUP), hup) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 205 | |
| 206 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 207 | @unittest.skipUnless(sys.platform == "win32", "Windows specific") |
| 208 | class WindowsSignalTests(unittest.TestCase): |
| 209 | def test_issue9324(self): |
Brian Curtin | eccd4d9 | 2010-10-01 15:09:53 +0000 | [diff] [blame] | 210 | # Updated for issue #10003, adding SIGBREAK |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 211 | handler = lambda x, y: None |
Brian Curtin | eccd4d9 | 2010-10-01 15:09:53 +0000 | [diff] [blame] | 212 | for sig in (signal.SIGABRT, signal.SIGBREAK, signal.SIGFPE, |
| 213 | signal.SIGILL, signal.SIGINT, signal.SIGSEGV, |
| 214 | signal.SIGTERM): |
| 215 | # Set and then reset a handler for signals that work on windows |
| 216 | signal.signal(sig, signal.signal(sig, handler)) |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 217 | |
| 218 | with self.assertRaises(ValueError): |
| 219 | signal.signal(-1, handler) |
Brian Curtin | 80cd4bf | 2010-08-07 03:52:38 +0000 | [diff] [blame] | 220 | |
| 221 | with self.assertRaises(ValueError): |
| 222 | signal.signal(7, handler) |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 223 | |
| 224 | |
| 225 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 226 | class WakeupSignalTests(unittest.TestCase): |
| 227 | TIMEOUT_FULL = 10 |
| 228 | TIMEOUT_HALF = 5 |
| 229 | |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 230 | def handler(self, signum, frame): |
| 231 | pass |
| 232 | |
Victor Stinner | 2082268 | 2011-05-31 22:31:09 +0200 | [diff] [blame] | 233 | def check_signum(self, *signals): |
Victor Stinner | d49b1f1 | 2011-05-08 02:03:15 +0200 | [diff] [blame] | 234 | data = os.read(self.read, len(signals)+1) |
| 235 | raised = struct.unpack('%uB' % len(data), data) |
Victor Stinner | 2082268 | 2011-05-31 22:31:09 +0200 | [diff] [blame] | 236 | # We don't care of the signal delivery order (it's not portable or |
| 237 | # reliable) |
| 238 | raised = set(raised) |
| 239 | signals = set(signals) |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 240 | self.assertEqual(raised, signals) |
Victor Stinner | d49b1f1 | 2011-05-08 02:03:15 +0200 | [diff] [blame] | 241 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 242 | def test_wakeup_fd_early(self): |
| 243 | import select |
| 244 | |
| 245 | signal.alarm(1) |
| 246 | before_time = time.time() |
| 247 | # We attempt to get a signal during the sleep, |
| 248 | # before select is called |
| 249 | time.sleep(self.TIMEOUT_FULL) |
| 250 | mid_time = time.time() |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 251 | self.assertTrue(mid_time - before_time < self.TIMEOUT_HALF) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 252 | select.select([self.read], [], [], self.TIMEOUT_FULL) |
| 253 | after_time = time.time() |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 254 | self.assertTrue(after_time - mid_time < self.TIMEOUT_HALF) |
Victor Stinner | d49b1f1 | 2011-05-08 02:03:15 +0200 | [diff] [blame] | 255 | self.check_signum(signal.SIGALRM) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 256 | |
| 257 | def test_wakeup_fd_during(self): |
| 258 | import select |
| 259 | |
| 260 | signal.alarm(1) |
| 261 | before_time = time.time() |
| 262 | # We attempt to get a signal during the select call |
| 263 | self.assertRaises(select.error, select.select, |
| 264 | [self.read], [], [], self.TIMEOUT_FULL) |
| 265 | after_time = time.time() |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 266 | self.assertTrue(after_time - before_time < self.TIMEOUT_HALF) |
Victor Stinner | d49b1f1 | 2011-05-08 02:03:15 +0200 | [diff] [blame] | 267 | self.check_signum(signal.SIGALRM) |
| 268 | |
| 269 | def test_signum(self): |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 270 | old_handler = signal.signal(signal.SIGUSR1, self.handler) |
Victor Stinner | d49b1f1 | 2011-05-08 02:03:15 +0200 | [diff] [blame] | 271 | self.addCleanup(signal.signal, signal.SIGUSR1, old_handler) |
| 272 | os.kill(os.getpid(), signal.SIGUSR1) |
| 273 | os.kill(os.getpid(), signal.SIGALRM) |
| 274 | self.check_signum(signal.SIGUSR1, signal.SIGALRM) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 275 | |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 276 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 277 | 'need signal.pthread_sigmask()') |
| 278 | @unittest.skipUnless(hasattr(signal, 'pthread_kill'), |
| 279 | 'need signal.pthread_kill()') |
| 280 | def test_pending(self): |
| 281 | signum1 = signal.SIGUSR1 |
| 282 | signum2 = signal.SIGUSR2 |
| 283 | tid = threading.current_thread().ident |
| 284 | |
| 285 | old_handler = signal.signal(signum1, self.handler) |
| 286 | self.addCleanup(signal.signal, signum1, old_handler) |
| 287 | old_handler = signal.signal(signum2, self.handler) |
| 288 | self.addCleanup(signal.signal, signum2, old_handler) |
| 289 | |
| 290 | signal.pthread_sigmask(signal.SIG_BLOCK, (signum1, signum2)) |
| 291 | signal.pthread_kill(tid, signum1) |
| 292 | signal.pthread_kill(tid, signum2) |
| 293 | # Unblocking the 2 signals calls the C signal handler twice |
| 294 | signal.pthread_sigmask(signal.SIG_UNBLOCK, (signum1, signum2)) |
| 295 | |
Victor Stinner | 2082268 | 2011-05-31 22:31:09 +0200 | [diff] [blame] | 296 | self.check_signum(signum1, signum2) |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 297 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 298 | def setUp(self): |
| 299 | import fcntl |
| 300 | |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 301 | self.alrm = signal.signal(signal.SIGALRM, self.handler) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 302 | self.read, self.write = os.pipe() |
| 303 | flags = fcntl.fcntl(self.write, fcntl.F_GETFL, 0) |
| 304 | flags = flags | os.O_NONBLOCK |
| 305 | fcntl.fcntl(self.write, fcntl.F_SETFL, flags) |
| 306 | self.old_wakeup = signal.set_wakeup_fd(self.write) |
| 307 | |
| 308 | def tearDown(self): |
| 309 | signal.set_wakeup_fd(self.old_wakeup) |
| 310 | os.close(self.read) |
| 311 | os.close(self.write) |
| 312 | signal.signal(signal.SIGALRM, self.alrm) |
| 313 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 314 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 315 | class SiginterruptTest(unittest.TestCase): |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 316 | |
| 317 | def setUp(self): |
| 318 | """Install a no-op signal handler that can be set to allow |
| 319 | interrupts or not, and arrange for the original signal handler to be |
| 320 | re-installed when the test is finished. |
| 321 | """ |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 322 | self.signum = signal.SIGUSR1 |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 323 | oldhandler = signal.signal(self.signum, lambda x,y: None) |
| 324 | self.addCleanup(signal.signal, self.signum, oldhandler) |
| 325 | |
| 326 | def readpipe_interrupted(self): |
| 327 | """Perform a read during which a signal will arrive. Return True if the |
| 328 | read is interrupted by the signal and raises an exception. Return False |
| 329 | if it returns normally. |
| 330 | """ |
| 331 | # Create a pipe that can be used for the read. Also clean it up |
| 332 | # when the test is over, since nothing else will (but see below for |
| 333 | # the write end). |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 334 | r, w = os.pipe() |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 335 | self.addCleanup(os.close, r) |
| 336 | |
| 337 | # Create another process which can send a signal to this one to try |
| 338 | # to interrupt the read. |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 339 | ppid = os.getpid() |
| 340 | pid = os.fork() |
| 341 | |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 342 | if pid == 0: |
| 343 | # Child code: sleep to give the parent enough time to enter the |
| 344 | # read() call (there's a race here, but it's really tricky to |
| 345 | # eliminate it); then signal the parent process. Also, sleep |
| 346 | # again to make it likely that the signal is delivered to the |
| 347 | # parent process before the child exits. If the child exits |
| 348 | # first, the write end of the pipe will be closed and the test |
| 349 | # is invalid. |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 350 | try: |
| 351 | time.sleep(0.2) |
| 352 | os.kill(ppid, self.signum) |
| 353 | time.sleep(0.2) |
| 354 | finally: |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 355 | # No matter what, just exit as fast as possible now. |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 356 | exit_subprocess() |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 357 | else: |
| 358 | # Parent code. |
| 359 | # Make sure the child is eventually reaped, else it'll be a |
| 360 | # zombie for the rest of the test suite run. |
| 361 | self.addCleanup(os.waitpid, pid, 0) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 362 | |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 363 | # Close the write end of the pipe. The child has a copy, so |
| 364 | # it's not really closed until the child exits. We need it to |
| 365 | # close when the child exits so that in the non-interrupt case |
| 366 | # the read eventually completes, otherwise we could just close |
| 367 | # it *after* the test. |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 368 | os.close(w) |
| 369 | |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 370 | # Try the read and report whether it is interrupted or not to |
| 371 | # the caller. |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 372 | try: |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 373 | d = os.read(r, 1) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 374 | return False |
| 375 | except OSError as err: |
| 376 | if err.errno != errno.EINTR: |
| 377 | raise |
| 378 | return True |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 379 | |
| 380 | def test_without_siginterrupt(self): |
Victor Stinner | 3a7f0f0 | 2011-05-08 02:10:36 +0200 | [diff] [blame] | 381 | # If a signal handler is installed and siginterrupt is not called |
| 382 | # at all, when that signal arrives, it interrupts a syscall that's in |
| 383 | # progress. |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 384 | i = self.readpipe_interrupted() |
| 385 | self.assertTrue(i) |
| 386 | # Arrival of the signal shouldn't have changed anything. |
| 387 | i = self.readpipe_interrupted() |
| 388 | self.assertTrue(i) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 389 | |
| 390 | def test_siginterrupt_on(self): |
Victor Stinner | 3a7f0f0 | 2011-05-08 02:10:36 +0200 | [diff] [blame] | 391 | # If a signal handler is installed and siginterrupt is called with |
| 392 | # a true value for the second argument, when that signal arrives, it |
| 393 | # interrupts a syscall that's in progress. |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 394 | signal.siginterrupt(self.signum, 1) |
| 395 | i = self.readpipe_interrupted() |
| 396 | self.assertTrue(i) |
| 397 | # Arrival of the signal shouldn't have changed anything. |
| 398 | i = self.readpipe_interrupted() |
| 399 | self.assertTrue(i) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 400 | |
| 401 | def test_siginterrupt_off(self): |
Victor Stinner | 3a7f0f0 | 2011-05-08 02:10:36 +0200 | [diff] [blame] | 402 | # If a signal handler is installed and siginterrupt is called with |
| 403 | # a false value for the second argument, when that signal arrives, it |
| 404 | # does not interrupt a syscall that's in progress. |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 405 | signal.siginterrupt(self.signum, 0) |
| 406 | i = self.readpipe_interrupted() |
| 407 | self.assertFalse(i) |
| 408 | # Arrival of the signal shouldn't have changed anything. |
| 409 | i = self.readpipe_interrupted() |
| 410 | self.assertFalse(i) |
| 411 | |
| 412 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 413 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 414 | class ItimerTest(unittest.TestCase): |
| 415 | def setUp(self): |
| 416 | self.hndl_called = False |
| 417 | self.hndl_count = 0 |
| 418 | self.itimer = None |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 419 | self.old_alarm = signal.signal(signal.SIGALRM, self.sig_alrm) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 420 | |
| 421 | def tearDown(self): |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 422 | signal.signal(signal.SIGALRM, self.old_alarm) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 423 | if self.itimer is not None: # test_itimer_exc doesn't change this attr |
| 424 | # just ensure that itimer is stopped |
| 425 | signal.setitimer(self.itimer, 0) |
| 426 | |
| 427 | def sig_alrm(self, *args): |
| 428 | self.hndl_called = True |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 429 | |
| 430 | def sig_vtalrm(self, *args): |
| 431 | self.hndl_called = True |
| 432 | |
| 433 | if self.hndl_count > 3: |
| 434 | # it shouldn't be here, because it should have been disabled. |
| 435 | raise signal.ItimerError("setitimer didn't disable ITIMER_VIRTUAL " |
| 436 | "timer.") |
| 437 | elif self.hndl_count == 3: |
| 438 | # disable ITIMER_VIRTUAL, this function shouldn't be called anymore |
| 439 | signal.setitimer(signal.ITIMER_VIRTUAL, 0) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 440 | |
| 441 | self.hndl_count += 1 |
| 442 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 443 | def sig_prof(self, *args): |
| 444 | self.hndl_called = True |
| 445 | signal.setitimer(signal.ITIMER_PROF, 0) |
| 446 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 447 | def test_itimer_exc(self): |
| 448 | # XXX I'm assuming -1 is an invalid itimer, but maybe some platform |
| 449 | # defines it ? |
| 450 | self.assertRaises(signal.ItimerError, signal.setitimer, -1, 0) |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 451 | # Negative times are treated as zero on some platforms. |
| 452 | if 0: |
| 453 | self.assertRaises(signal.ItimerError, |
| 454 | signal.setitimer, signal.ITIMER_REAL, -1) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 455 | |
| 456 | def test_itimer_real(self): |
| 457 | self.itimer = signal.ITIMER_REAL |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 458 | signal.setitimer(self.itimer, 1.0) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 459 | signal.pause() |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 460 | self.assertEqual(self.hndl_called, True) |
| 461 | |
R. David Murray | 44546f8 | 2010-04-21 01:59:28 +0000 | [diff] [blame] | 462 | # Issue 3864, unknown if this affects earlier versions of freebsd also |
Gregory P. Smith | 397cd8a | 2010-10-17 04:23:21 +0000 | [diff] [blame] | 463 | @unittest.skipIf(sys.platform in ('freebsd6', 'netbsd5'), |
| 464 | 'itimer not reliable (does not mix well with threading) on some BSDs.') |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 465 | def test_itimer_virtual(self): |
| 466 | self.itimer = signal.ITIMER_VIRTUAL |
| 467 | signal.signal(signal.SIGVTALRM, self.sig_vtalrm) |
| 468 | signal.setitimer(self.itimer, 0.3, 0.2) |
| 469 | |
Mark Dickinson | 7837347 | 2009-10-31 10:39:21 +0000 | [diff] [blame] | 470 | start_time = time.time() |
Stefan Krah | f1da97304 | 2010-04-20 08:15:14 +0000 | [diff] [blame] | 471 | while time.time() - start_time < 60.0: |
Mark Dickinson | 9507887 | 2009-10-04 18:47:48 +0000 | [diff] [blame] | 472 | # use up some virtual time by doing real work |
| 473 | _ = pow(12345, 67890, 10000019) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 474 | if signal.getitimer(self.itimer) == (0.0, 0.0): |
| 475 | break # sig_vtalrm handler stopped this itimer |
Stefan Krah | f1da97304 | 2010-04-20 08:15:14 +0000 | [diff] [blame] | 476 | else: # Issue 8424 |
Benjamin Peterson | 9b140f6 | 2010-05-15 18:00:56 +0000 | [diff] [blame] | 477 | self.skipTest("timeout: likely cause: machine too slow or load too " |
| 478 | "high") |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 479 | |
| 480 | # virtual itimer should be (0.0, 0.0) now |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 481 | self.assertEqual(signal.getitimer(self.itimer), (0.0, 0.0)) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 482 | # and the handler should have been called |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 483 | self.assertEqual(self.hndl_called, True) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 484 | |
R. David Murray | 44546f8 | 2010-04-21 01:59:28 +0000 | [diff] [blame] | 485 | # Issue 3864, unknown if this affects earlier versions of freebsd also |
| 486 | @unittest.skipIf(sys.platform=='freebsd6', |
| 487 | 'itimer not reliable (does not mix well with threading) on freebsd6') |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 488 | def test_itimer_prof(self): |
| 489 | self.itimer = signal.ITIMER_PROF |
| 490 | signal.signal(signal.SIGPROF, self.sig_prof) |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 491 | signal.setitimer(self.itimer, 0.2, 0.2) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 492 | |
Mark Dickinson | 7837347 | 2009-10-31 10:39:21 +0000 | [diff] [blame] | 493 | start_time = time.time() |
Stefan Krah | f1da97304 | 2010-04-20 08:15:14 +0000 | [diff] [blame] | 494 | while time.time() - start_time < 60.0: |
Mark Dickinson | 7837347 | 2009-10-31 10:39:21 +0000 | [diff] [blame] | 495 | # do some work |
| 496 | _ = pow(12345, 67890, 10000019) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 497 | if signal.getitimer(self.itimer) == (0.0, 0.0): |
| 498 | break # sig_prof handler stopped this itimer |
Stefan Krah | f1da97304 | 2010-04-20 08:15:14 +0000 | [diff] [blame] | 499 | else: # Issue 8424 |
Benjamin Peterson | 9b140f6 | 2010-05-15 18:00:56 +0000 | [diff] [blame] | 500 | self.skipTest("timeout: likely cause: machine too slow or load too " |
| 501 | "high") |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 502 | |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 503 | # profiling itimer should be (0.0, 0.0) now |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 504 | self.assertEqual(signal.getitimer(self.itimer), (0.0, 0.0)) |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 505 | # and the handler should have been called |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 506 | self.assertEqual(self.hndl_called, True) |
| 507 | |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 508 | |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 509 | class PendingSignalsTests(unittest.TestCase): |
| 510 | """ |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 511 | Test pthread_sigmask(), pthread_kill(), sigpending() and sigwait() |
| 512 | functions. |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 513 | """ |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 514 | def setUp(self): |
| 515 | self.has_pthread_kill = hasattr(signal, 'pthread_kill') |
| 516 | |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 517 | def handler(self, signum, frame): |
| 518 | 1/0 |
| 519 | |
| 520 | def read_sigmask(self): |
| 521 | return signal.pthread_sigmask(signal.SIG_BLOCK, []) |
| 522 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 523 | def can_test_blocked_signals(self, skip): |
| 524 | """ |
| 525 | Check if a blocked signal can be raised to the main thread without |
| 526 | calling its signal handler. We need pthread_kill() or exactly one |
| 527 | thread (the main thread). |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 528 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 529 | Return True if it's possible. Otherwise, return False and print a |
| 530 | warning if skip is False, or raise a SkipTest exception if skip is |
| 531 | True. |
| 532 | """ |
| 533 | if self.has_pthread_kill: |
| 534 | return True |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 535 | |
Victor Stinner | f44ce87 | 2011-05-03 17:20:31 +0200 | [diff] [blame] | 536 | # The fault handler timeout thread masks all signals. If the main |
| 537 | # thread masks also SIGUSR1, all threads mask this signal. In this |
| 538 | # case, if we send SIGUSR1 to the process, the signal is pending in the |
| 539 | # main or the faulthandler timeout thread. Unblock SIGUSR1 in the main |
| 540 | # thread calls the signal handler only if the signal is pending for the |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 541 | # main thread. Stop the faulthandler timeout thread to workaround this |
| 542 | # problem. |
| 543 | import faulthandler |
Victor Stinner | f44ce87 | 2011-05-03 17:20:31 +0200 | [diff] [blame] | 544 | faulthandler.cancel_dump_tracebacks_later() |
Victor Stinner | 2d4a91e | 2011-05-03 14:11:22 +0200 | [diff] [blame] | 545 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 546 | # Issue #11998: The _tkinter module loads the Tcl library which |
| 547 | # creates a thread waiting events in select(). This thread receives |
| 548 | # signals blocked by all other threads. We cannot test blocked |
| 549 | # signals |
| 550 | if '_tkinter' in sys.modules: |
| 551 | message = ("_tkinter is loaded and pthread_kill() is missing, " |
| 552 | "cannot test blocked signals (issue #11998)") |
| 553 | if skip: |
| 554 | self.skipTest(message) |
| 555 | else: |
| 556 | print("WARNING: %s" % message) |
| 557 | return False |
| 558 | return True |
| 559 | |
| 560 | def kill(self, signum): |
| 561 | if self.has_pthread_kill: |
Victor Stinner | 2a12974 | 2011-05-30 23:02:52 +0200 | [diff] [blame] | 562 | tid = threading.get_ident() |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 563 | signal.pthread_kill(tid, signum) |
| 564 | else: |
| 565 | pid = os.getpid() |
| 566 | os.kill(pid, signum) |
| 567 | |
| 568 | @unittest.skipUnless(hasattr(signal, 'sigpending'), |
| 569 | 'need signal.sigpending()') |
| 570 | def test_sigpending_empty(self): |
| 571 | self.assertEqual(signal.sigpending(), set()) |
| 572 | |
| 573 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 574 | 'need signal.pthread_sigmask()') |
| 575 | @unittest.skipUnless(hasattr(signal, 'sigpending'), |
| 576 | 'need signal.sigpending()') |
| 577 | def test_sigpending(self): |
| 578 | self.can_test_blocked_signals(True) |
| 579 | |
| 580 | signum = signal.SIGUSR1 |
| 581 | old_handler = signal.signal(signum, self.handler) |
| 582 | self.addCleanup(signal.signal, signum, old_handler) |
| 583 | |
| 584 | signal.pthread_sigmask(signal.SIG_BLOCK, [signum]) |
| 585 | self.kill(signum) |
| 586 | self.assertEqual(signal.sigpending(), {signum}) |
| 587 | with self.assertRaises(ZeroDivisionError): |
| 588 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 589 | |
| 590 | @unittest.skipUnless(hasattr(signal, 'pthread_kill'), |
| 591 | 'need signal.pthread_kill()') |
| 592 | def test_pthread_kill(self): |
| 593 | signum = signal.SIGUSR1 |
Victor Stinner | 2a12974 | 2011-05-30 23:02:52 +0200 | [diff] [blame] | 594 | current = threading.get_ident() |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 595 | |
| 596 | old_handler = signal.signal(signum, self.handler) |
| 597 | self.addCleanup(signal.signal, signum, old_handler) |
| 598 | |
| 599 | with self.assertRaises(ZeroDivisionError): |
| 600 | signal.pthread_kill(current, signum) |
| 601 | |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 602 | @unittest.skipUnless(hasattr(signal, 'sigwait'), |
| 603 | 'need signal.sigwait()') |
Victor Stinner | 7f294d1 | 2011-06-10 14:02:10 +0200 | [diff] [blame] | 604 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 605 | 'need signal.pthread_sigmask()') |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 606 | @unittest.skipUnless(hasattr(os, 'fork'), 'need os.fork()') |
| 607 | def test_sigwait(self): |
| 608 | def test(signum): |
| 609 | signal.alarm(1) |
| 610 | received = signal.sigwait([signum]) |
| 611 | if received != signum: |
| 612 | print("sigwait() received %s, not %s" |
| 613 | % (received, signum), |
| 614 | file=sys.stderr) |
| 615 | os._exit(1) |
| 616 | |
| 617 | signum = signal.SIGALRM |
| 618 | |
Victor Stinner | af49460 | 2011-06-10 12:48:13 +0200 | [diff] [blame] | 619 | # sigwait must be called with the signal blocked: since the current |
| 620 | # process might have several threads running, we fork() a child process |
| 621 | # to have a single thread. |
| 622 | pid = os.fork() |
| 623 | if pid == 0: |
| 624 | # child: block and wait the signal |
| 625 | try: |
| 626 | signal.signal(signum, self.handler) |
| 627 | signal.pthread_sigmask(signal.SIG_BLOCK, [signum]) |
| 628 | |
| 629 | # Do the tests |
| 630 | test(signum) |
| 631 | |
| 632 | # The handler must not be called on unblock |
| 633 | try: |
| 634 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 635 | except ZeroDivisionError: |
| 636 | print("the signal handler has been called", |
| 637 | file=sys.stderr) |
| 638 | os._exit(1) |
Victor Stinner | 4659166 | 2011-06-10 13:53:32 +0200 | [diff] [blame] | 639 | except BaseException as err: |
| 640 | print("error: {}".format(err), file=sys.stderr) |
Victor Stinner | af49460 | 2011-06-10 12:48:13 +0200 | [diff] [blame] | 641 | os._exit(1) |
Victor Stinner | 4659166 | 2011-06-10 13:53:32 +0200 | [diff] [blame] | 642 | else: |
| 643 | os._exit(0) |
Victor Stinner | af49460 | 2011-06-10 12:48:13 +0200 | [diff] [blame] | 644 | else: |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 645 | # parent: check that the child correcty received the signal |
Victor Stinner | af49460 | 2011-06-10 12:48:13 +0200 | [diff] [blame] | 646 | self.assertEqual(os.waitpid(pid, 0), (pid, 0)) |
| 647 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 648 | @unittest.skipUnless(hasattr(signal, 'sigwait'), |
| 649 | 'need signal.sigwait()') |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 650 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 651 | 'need signal.pthread_sigmask()') |
Victor Stinner | 10c30d6 | 2011-06-10 01:39:53 +0200 | [diff] [blame] | 652 | @unittest.skipIf(threading is None, "test needs threading module") |
| 653 | def test_sigwait_thread(self): |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 654 | # Check that calling sigwait() from a thread doesn't suspend the whole |
| 655 | # process. A new interpreter is spawned to avoid problems when mixing |
| 656 | # threads and fork(): only async-safe functions are allowed between |
| 657 | # fork() and exec(). |
| 658 | assert_python_ok("-c", """if True: |
| 659 | import os, threading, sys, time, signal |
Victor Stinner | 10c30d6 | 2011-06-10 01:39:53 +0200 | [diff] [blame] | 660 | |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 661 | # the default handler terminates the process |
| 662 | signum = signal.SIGUSR1 |
| 663 | |
| 664 | def kill_later(): |
| 665 | # wait until the main thread is waiting in sigwait() |
| 666 | time.sleep(1) |
| 667 | os.kill(os.getpid(), signum) |
| 668 | |
| 669 | # the signal must be blocked by all the threads |
| 670 | signal.pthread_sigmask(signal.SIG_BLOCK, [signum]) |
| 671 | killer = threading.Thread(target=kill_later) |
Victor Stinner | af49460 | 2011-06-10 12:48:13 +0200 | [diff] [blame] | 672 | killer.start() |
| 673 | received = signal.sigwait([signum]) |
| 674 | if received != signum: |
| 675 | print("sigwait() received %s, not %s" % (received, signum), |
| 676 | file=sys.stderr) |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 677 | sys.exit(1) |
Victor Stinner | 10c30d6 | 2011-06-10 01:39:53 +0200 | [diff] [blame] | 678 | killer.join() |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 679 | # unblock the signal, which should have been cleared by sigwait() |
| 680 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 681 | """) |
Victor Stinner | af49460 | 2011-06-10 12:48:13 +0200 | [diff] [blame] | 682 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 683 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 684 | 'need signal.pthread_sigmask()') |
| 685 | def test_pthread_sigmask_arguments(self): |
| 686 | self.assertRaises(TypeError, signal.pthread_sigmask) |
| 687 | self.assertRaises(TypeError, signal.pthread_sigmask, 1) |
| 688 | self.assertRaises(TypeError, signal.pthread_sigmask, 1, 2, 3) |
| 689 | self.assertRaises(OSError, signal.pthread_sigmask, 1700, []) |
| 690 | |
| 691 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 692 | 'need signal.pthread_sigmask()') |
| 693 | def test_pthread_sigmask(self): |
| 694 | test_blocked_signals = self.can_test_blocked_signals(False) |
| 695 | signum = signal.SIGUSR1 |
Victor Stinner | 6fd49e1 | 2011-05-04 12:38:03 +0200 | [diff] [blame] | 696 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 697 | # Install our signal handler |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 698 | old_handler = signal.signal(signum, self.handler) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 699 | self.addCleanup(signal.signal, signum, old_handler) |
| 700 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 701 | # Unblock SIGUSR1 (and copy the old mask) to test our signal handler |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 702 | old_mask = signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 703 | self.addCleanup(signal.pthread_sigmask, signal.SIG_SETMASK, old_mask) |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 704 | with self.assertRaises(ZeroDivisionError): |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 705 | self.kill(signum) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 706 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 707 | # Block and then raise SIGUSR1. The signal is blocked: the signal |
| 708 | # handler is not called, and the signal is now pending |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 709 | signal.pthread_sigmask(signal.SIG_BLOCK, [signum]) |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 710 | if test_blocked_signals: |
| 711 | self.kill(signum) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 712 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 713 | # Check the new mask |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 714 | blocked = self.read_sigmask() |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 715 | self.assertIn(signum, blocked) |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 716 | self.assertEqual(old_mask ^ blocked, {signum}) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 717 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 718 | # Unblock SIGUSR1 |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 719 | if test_blocked_signals: |
Victor Stinner | 6fd49e1 | 2011-05-04 12:38:03 +0200 | [diff] [blame] | 720 | with self.assertRaises(ZeroDivisionError): |
| 721 | # unblock the pending signal calls immediatly the signal handler |
| 722 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 723 | else: |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 724 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 725 | with self.assertRaises(ZeroDivisionError): |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 726 | self.kill(signum) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 727 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 728 | # Check the new mask |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 729 | unblocked = self.read_sigmask() |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 730 | self.assertNotIn(signum, unblocked) |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 731 | self.assertEqual(blocked ^ unblocked, {signum}) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 732 | self.assertSequenceEqual(old_mask, unblocked) |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 733 | # Finally, restore the previous signal handler and the signal mask |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 734 | |
| 735 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 736 | def test_main(): |
Antoine Pitrou | 8189ab8 | 2011-03-20 17:35:32 +0100 | [diff] [blame] | 737 | try: |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 738 | support.run_unittest(PosixTests, InterProcessSignalTests, |
Antoine Pitrou | 8189ab8 | 2011-03-20 17:35:32 +0100 | [diff] [blame] | 739 | WakeupSignalTests, SiginterruptTest, |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 740 | ItimerTest, WindowsSignalTests, |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 741 | PendingSignalsTests) |
Antoine Pitrou | 8189ab8 | 2011-03-20 17:35:32 +0100 | [diff] [blame] | 742 | finally: |
| 743 | support.reap_children() |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 744 | |
| 745 | |
| 746 | if __name__ == "__main__": |
| 747 | test_main() |