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 | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 12 | from test.script_helper import assert_python_ok, spawn_python |
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 | |
Victor Stinner | e71db44 | 2011-06-24 20:52:27 +0200 | [diff] [blame] | 298 | @unittest.skipUnless(hasattr(signal, 'pthread_kill'), |
| 299 | 'need signal.pthread_kill()') |
| 300 | def test_pthread_kill_main_thread(self): |
| 301 | # Test that a signal can be sent to the main thread with pthread_kill() |
| 302 | # before any other thread has been created (see issue #12392). |
| 303 | code = """if True: |
| 304 | import threading |
| 305 | import signal |
| 306 | import sys |
| 307 | |
| 308 | def handler(signum, frame): |
| 309 | sys.exit(3) |
| 310 | |
| 311 | signal.signal(signal.SIGUSR1, handler) |
| 312 | signal.pthread_kill(threading.get_ident(), signal.SIGUSR1) |
| 313 | sys.exit(1) |
| 314 | """ |
| 315 | |
| 316 | with spawn_python('-c', code) as process: |
| 317 | stdout, stderr = process.communicate() |
| 318 | exitcode = process.wait() |
| 319 | if exitcode != 3: |
| 320 | raise Exception("Child error (exit code %s): %s" % |
| 321 | (exitcode, stdout)) |
| 322 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 323 | def setUp(self): |
| 324 | import fcntl |
| 325 | |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 326 | self.alrm = signal.signal(signal.SIGALRM, self.handler) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 327 | self.read, self.write = os.pipe() |
| 328 | flags = fcntl.fcntl(self.write, fcntl.F_GETFL, 0) |
| 329 | flags = flags | os.O_NONBLOCK |
| 330 | fcntl.fcntl(self.write, fcntl.F_SETFL, flags) |
| 331 | self.old_wakeup = signal.set_wakeup_fd(self.write) |
| 332 | |
| 333 | def tearDown(self): |
| 334 | signal.set_wakeup_fd(self.old_wakeup) |
| 335 | os.close(self.read) |
| 336 | os.close(self.write) |
| 337 | signal.signal(signal.SIGALRM, self.alrm) |
| 338 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 339 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 340 | class SiginterruptTest(unittest.TestCase): |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 341 | |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 342 | def readpipe_interrupted(self, interrupt): |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 343 | """Perform a read during which a signal will arrive. Return True if the |
| 344 | read is interrupted by the signal and raises an exception. Return False |
| 345 | if it returns normally. |
| 346 | """ |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 347 | # use a subprocess to have only one thread, to have a timeout on the |
| 348 | # blocking read and to not touch signal handling in this process |
| 349 | code = """if 1: |
| 350 | import errno |
| 351 | import os |
| 352 | import signal |
| 353 | import sys |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 354 | |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 355 | interrupt = %r |
| 356 | r, w = os.pipe() |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 357 | |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 358 | def handler(signum, frame): |
| 359 | pass |
| 360 | |
Victor Stinner | 4527365 | 2011-06-22 22:15:51 +0200 | [diff] [blame] | 361 | print("ready") |
| 362 | sys.stdout.flush() |
| 363 | |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 364 | signal.signal(signal.SIGALRM, handler) |
| 365 | if interrupt is not None: |
| 366 | signal.siginterrupt(signal.SIGALRM, interrupt) |
| 367 | |
| 368 | # run the test twice |
| 369 | for loop in range(2): |
| 370 | # send a SIGALRM in a second (during the read) |
| 371 | signal.alarm(1) |
| 372 | try: |
| 373 | # blocking call: read from a pipe without data |
| 374 | os.read(r, 1) |
| 375 | except OSError as err: |
| 376 | if err.errno != errno.EINTR: |
| 377 | raise |
| 378 | else: |
| 379 | sys.exit(2) |
| 380 | sys.exit(3) |
| 381 | """ % (interrupt,) |
| 382 | with spawn_python('-c', code) as process: |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 383 | try: |
Victor Stinner | 4527365 | 2011-06-22 22:15:51 +0200 | [diff] [blame] | 384 | # wait until the child process is loaded and has started |
| 385 | first_line = process.stdout.readline() |
| 386 | |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 387 | stdout, stderr = process.communicate(timeout=3.0) |
| 388 | except subprocess.TimeoutExpired: |
| 389 | process.kill() |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 390 | return False |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 391 | else: |
Victor Stinner | 4527365 | 2011-06-22 22:15:51 +0200 | [diff] [blame] | 392 | stdout = first_line + stdout |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 393 | exitcode = process.wait() |
| 394 | if exitcode not in (2, 3): |
| 395 | raise Exception("Child error (exit code %s): %s" |
| 396 | % (exitcode, stdout)) |
| 397 | return (exitcode == 3) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 398 | |
| 399 | def test_without_siginterrupt(self): |
Victor Stinner | 3a7f0f0 | 2011-05-08 02:10:36 +0200 | [diff] [blame] | 400 | # If a signal handler is installed and siginterrupt is not called |
| 401 | # at all, when that signal arrives, it interrupts a syscall that's in |
| 402 | # progress. |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 403 | interrupted = self.readpipe_interrupted(None) |
| 404 | self.assertTrue(interrupted) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 405 | |
| 406 | def test_siginterrupt_on(self): |
Victor Stinner | 3a7f0f0 | 2011-05-08 02:10:36 +0200 | [diff] [blame] | 407 | # If a signal handler is installed and siginterrupt is called with |
| 408 | # a true value for the second argument, when that signal arrives, it |
| 409 | # interrupts a syscall that's in progress. |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 410 | interrupted = self.readpipe_interrupted(True) |
| 411 | self.assertTrue(interrupted) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 412 | |
| 413 | def test_siginterrupt_off(self): |
Victor Stinner | 3a7f0f0 | 2011-05-08 02:10:36 +0200 | [diff] [blame] | 414 | # If a signal handler is installed and siginterrupt is called with |
| 415 | # a false value for the second argument, when that signal arrives, it |
| 416 | # does not interrupt a syscall that's in progress. |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 417 | interrupted = self.readpipe_interrupted(False) |
| 418 | self.assertFalse(interrupted) |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 419 | |
| 420 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 421 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 422 | class ItimerTest(unittest.TestCase): |
| 423 | def setUp(self): |
| 424 | self.hndl_called = False |
| 425 | self.hndl_count = 0 |
| 426 | self.itimer = None |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 427 | self.old_alarm = signal.signal(signal.SIGALRM, self.sig_alrm) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 428 | |
| 429 | def tearDown(self): |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 430 | signal.signal(signal.SIGALRM, self.old_alarm) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 431 | if self.itimer is not None: # test_itimer_exc doesn't change this attr |
| 432 | # just ensure that itimer is stopped |
| 433 | signal.setitimer(self.itimer, 0) |
| 434 | |
| 435 | def sig_alrm(self, *args): |
| 436 | self.hndl_called = True |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 437 | |
| 438 | def sig_vtalrm(self, *args): |
| 439 | self.hndl_called = True |
| 440 | |
| 441 | if self.hndl_count > 3: |
| 442 | # it shouldn't be here, because it should have been disabled. |
| 443 | raise signal.ItimerError("setitimer didn't disable ITIMER_VIRTUAL " |
| 444 | "timer.") |
| 445 | elif self.hndl_count == 3: |
| 446 | # disable ITIMER_VIRTUAL, this function shouldn't be called anymore |
| 447 | signal.setitimer(signal.ITIMER_VIRTUAL, 0) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 448 | |
| 449 | self.hndl_count += 1 |
| 450 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 451 | def sig_prof(self, *args): |
| 452 | self.hndl_called = True |
| 453 | signal.setitimer(signal.ITIMER_PROF, 0) |
| 454 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 455 | def test_itimer_exc(self): |
| 456 | # XXX I'm assuming -1 is an invalid itimer, but maybe some platform |
| 457 | # defines it ? |
| 458 | self.assertRaises(signal.ItimerError, signal.setitimer, -1, 0) |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 459 | # Negative times are treated as zero on some platforms. |
| 460 | if 0: |
| 461 | self.assertRaises(signal.ItimerError, |
| 462 | signal.setitimer, signal.ITIMER_REAL, -1) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 463 | |
| 464 | def test_itimer_real(self): |
| 465 | self.itimer = signal.ITIMER_REAL |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 466 | signal.setitimer(self.itimer, 1.0) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 467 | signal.pause() |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 468 | self.assertEqual(self.hndl_called, True) |
| 469 | |
R. David Murray | 44546f8 | 2010-04-21 01:59:28 +0000 | [diff] [blame] | 470 | # Issue 3864, unknown if this affects earlier versions of freebsd also |
Gregory P. Smith | 397cd8a | 2010-10-17 04:23:21 +0000 | [diff] [blame] | 471 | @unittest.skipIf(sys.platform in ('freebsd6', 'netbsd5'), |
| 472 | '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] | 473 | def test_itimer_virtual(self): |
| 474 | self.itimer = signal.ITIMER_VIRTUAL |
| 475 | signal.signal(signal.SIGVTALRM, self.sig_vtalrm) |
| 476 | signal.setitimer(self.itimer, 0.3, 0.2) |
| 477 | |
Mark Dickinson | 7837347 | 2009-10-31 10:39:21 +0000 | [diff] [blame] | 478 | start_time = time.time() |
Stefan Krah | f1da97304 | 2010-04-20 08:15:14 +0000 | [diff] [blame] | 479 | while time.time() - start_time < 60.0: |
Mark Dickinson | 9507887 | 2009-10-04 18:47:48 +0000 | [diff] [blame] | 480 | # use up some virtual time by doing real work |
| 481 | _ = pow(12345, 67890, 10000019) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 482 | if signal.getitimer(self.itimer) == (0.0, 0.0): |
| 483 | break # sig_vtalrm handler stopped this itimer |
Stefan Krah | f1da97304 | 2010-04-20 08:15:14 +0000 | [diff] [blame] | 484 | else: # Issue 8424 |
Benjamin Peterson | 9b140f6 | 2010-05-15 18:00:56 +0000 | [diff] [blame] | 485 | self.skipTest("timeout: likely cause: machine too slow or load too " |
| 486 | "high") |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 487 | |
| 488 | # virtual itimer should be (0.0, 0.0) now |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 489 | self.assertEqual(signal.getitimer(self.itimer), (0.0, 0.0)) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 490 | # and the handler should have been called |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 491 | self.assertEqual(self.hndl_called, True) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 492 | |
R. David Murray | 44546f8 | 2010-04-21 01:59:28 +0000 | [diff] [blame] | 493 | # Issue 3864, unknown if this affects earlier versions of freebsd also |
| 494 | @unittest.skipIf(sys.platform=='freebsd6', |
| 495 | '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] | 496 | def test_itimer_prof(self): |
| 497 | self.itimer = signal.ITIMER_PROF |
| 498 | signal.signal(signal.SIGPROF, self.sig_prof) |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 499 | signal.setitimer(self.itimer, 0.2, 0.2) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 500 | |
Mark Dickinson | 7837347 | 2009-10-31 10:39:21 +0000 | [diff] [blame] | 501 | start_time = time.time() |
Stefan Krah | f1da97304 | 2010-04-20 08:15:14 +0000 | [diff] [blame] | 502 | while time.time() - start_time < 60.0: |
Mark Dickinson | 7837347 | 2009-10-31 10:39:21 +0000 | [diff] [blame] | 503 | # do some work |
| 504 | _ = pow(12345, 67890, 10000019) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 505 | if signal.getitimer(self.itimer) == (0.0, 0.0): |
| 506 | break # sig_prof handler stopped this itimer |
Stefan Krah | f1da97304 | 2010-04-20 08:15:14 +0000 | [diff] [blame] | 507 | else: # Issue 8424 |
Benjamin Peterson | 9b140f6 | 2010-05-15 18:00:56 +0000 | [diff] [blame] | 508 | self.skipTest("timeout: likely cause: machine too slow or load too " |
| 509 | "high") |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 510 | |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 511 | # profiling itimer should be (0.0, 0.0) now |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 512 | self.assertEqual(signal.getitimer(self.itimer), (0.0, 0.0)) |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 513 | # and the handler should have been called |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 514 | self.assertEqual(self.hndl_called, True) |
| 515 | |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 516 | |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 517 | class PendingSignalsTests(unittest.TestCase): |
| 518 | """ |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 519 | Test pthread_sigmask(), pthread_kill(), sigpending() and sigwait() |
| 520 | functions. |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 521 | """ |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 522 | def setUp(self): |
| 523 | self.has_pthread_kill = hasattr(signal, 'pthread_kill') |
| 524 | |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 525 | def handler(self, signum, frame): |
| 526 | 1/0 |
| 527 | |
| 528 | def read_sigmask(self): |
| 529 | return signal.pthread_sigmask(signal.SIG_BLOCK, []) |
| 530 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 531 | def can_test_blocked_signals(self, skip): |
| 532 | """ |
| 533 | Check if a blocked signal can be raised to the main thread without |
| 534 | calling its signal handler. We need pthread_kill() or exactly one |
| 535 | thread (the main thread). |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 536 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 537 | Return True if it's possible. Otherwise, return False and print a |
| 538 | warning if skip is False, or raise a SkipTest exception if skip is |
| 539 | True. |
| 540 | """ |
| 541 | if self.has_pthread_kill: |
| 542 | return True |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 543 | |
Victor Stinner | f44ce87 | 2011-05-03 17:20:31 +0200 | [diff] [blame] | 544 | # The fault handler timeout thread masks all signals. If the main |
| 545 | # thread masks also SIGUSR1, all threads mask this signal. In this |
| 546 | # case, if we send SIGUSR1 to the process, the signal is pending in the |
| 547 | # main or the faulthandler timeout thread. Unblock SIGUSR1 in the main |
| 548 | # 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] | 549 | # main thread. Stop the faulthandler timeout thread to workaround this |
| 550 | # problem. |
| 551 | import faulthandler |
Victor Stinner | f44ce87 | 2011-05-03 17:20:31 +0200 | [diff] [blame] | 552 | faulthandler.cancel_dump_tracebacks_later() |
Victor Stinner | 2d4a91e | 2011-05-03 14:11:22 +0200 | [diff] [blame] | 553 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 554 | # Issue #11998: The _tkinter module loads the Tcl library which |
| 555 | # creates a thread waiting events in select(). This thread receives |
| 556 | # signals blocked by all other threads. We cannot test blocked |
| 557 | # signals |
| 558 | if '_tkinter' in sys.modules: |
| 559 | message = ("_tkinter is loaded and pthread_kill() is missing, " |
| 560 | "cannot test blocked signals (issue #11998)") |
| 561 | if skip: |
| 562 | self.skipTest(message) |
| 563 | else: |
| 564 | print("WARNING: %s" % message) |
| 565 | return False |
| 566 | return True |
| 567 | |
| 568 | def kill(self, signum): |
| 569 | if self.has_pthread_kill: |
Victor Stinner | 2a12974 | 2011-05-30 23:02:52 +0200 | [diff] [blame] | 570 | tid = threading.get_ident() |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 571 | signal.pthread_kill(tid, signum) |
| 572 | else: |
| 573 | pid = os.getpid() |
| 574 | os.kill(pid, signum) |
| 575 | |
| 576 | @unittest.skipUnless(hasattr(signal, 'sigpending'), |
| 577 | 'need signal.sigpending()') |
| 578 | def test_sigpending_empty(self): |
| 579 | self.assertEqual(signal.sigpending(), set()) |
| 580 | |
| 581 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 582 | 'need signal.pthread_sigmask()') |
| 583 | @unittest.skipUnless(hasattr(signal, 'sigpending'), |
| 584 | 'need signal.sigpending()') |
| 585 | def test_sigpending(self): |
| 586 | self.can_test_blocked_signals(True) |
| 587 | |
| 588 | signum = signal.SIGUSR1 |
| 589 | old_handler = signal.signal(signum, self.handler) |
| 590 | self.addCleanup(signal.signal, signum, old_handler) |
| 591 | |
| 592 | signal.pthread_sigmask(signal.SIG_BLOCK, [signum]) |
| 593 | self.kill(signum) |
| 594 | self.assertEqual(signal.sigpending(), {signum}) |
| 595 | with self.assertRaises(ZeroDivisionError): |
| 596 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 597 | |
| 598 | @unittest.skipUnless(hasattr(signal, 'pthread_kill'), |
| 599 | 'need signal.pthread_kill()') |
| 600 | def test_pthread_kill(self): |
| 601 | signum = signal.SIGUSR1 |
Victor Stinner | 2a12974 | 2011-05-30 23:02:52 +0200 | [diff] [blame] | 602 | current = threading.get_ident() |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 603 | |
| 604 | old_handler = signal.signal(signum, self.handler) |
| 605 | self.addCleanup(signal.signal, signum, old_handler) |
| 606 | |
| 607 | with self.assertRaises(ZeroDivisionError): |
| 608 | signal.pthread_kill(current, signum) |
| 609 | |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 610 | @unittest.skipUnless(hasattr(signal, 'sigwait'), |
| 611 | 'need signal.sigwait()') |
Victor Stinner | 7f294d1 | 2011-06-10 14:02:10 +0200 | [diff] [blame] | 612 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 613 | 'need signal.pthread_sigmask()') |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 614 | @unittest.skipUnless(hasattr(os, 'fork'), 'need os.fork()') |
| 615 | def test_sigwait(self): |
| 616 | def test(signum): |
| 617 | signal.alarm(1) |
| 618 | received = signal.sigwait([signum]) |
| 619 | if received != signum: |
| 620 | print("sigwait() received %s, not %s" |
| 621 | % (received, signum), |
| 622 | file=sys.stderr) |
| 623 | os._exit(1) |
| 624 | |
| 625 | signum = signal.SIGALRM |
| 626 | |
Victor Stinner | af49460 | 2011-06-10 12:48:13 +0200 | [diff] [blame] | 627 | # sigwait must be called with the signal blocked: since the current |
| 628 | # process might have several threads running, we fork() a child process |
| 629 | # to have a single thread. |
| 630 | pid = os.fork() |
| 631 | if pid == 0: |
| 632 | # child: block and wait the signal |
| 633 | try: |
| 634 | signal.signal(signum, self.handler) |
| 635 | signal.pthread_sigmask(signal.SIG_BLOCK, [signum]) |
| 636 | |
| 637 | # Do the tests |
| 638 | test(signum) |
| 639 | |
| 640 | # The handler must not be called on unblock |
| 641 | try: |
| 642 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 643 | except ZeroDivisionError: |
| 644 | print("the signal handler has been called", |
| 645 | file=sys.stderr) |
| 646 | os._exit(1) |
Victor Stinner | 4659166 | 2011-06-10 13:53:32 +0200 | [diff] [blame] | 647 | except BaseException as err: |
| 648 | print("error: {}".format(err), file=sys.stderr) |
Victor Stinner | af49460 | 2011-06-10 12:48:13 +0200 | [diff] [blame] | 649 | os._exit(1) |
Victor Stinner | 4659166 | 2011-06-10 13:53:32 +0200 | [diff] [blame] | 650 | else: |
| 651 | os._exit(0) |
Victor Stinner | af49460 | 2011-06-10 12:48:13 +0200 | [diff] [blame] | 652 | else: |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 653 | # parent: check that the child correcty received the signal |
Victor Stinner | af49460 | 2011-06-10 12:48:13 +0200 | [diff] [blame] | 654 | self.assertEqual(os.waitpid(pid, 0), (pid, 0)) |
| 655 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 656 | @unittest.skipUnless(hasattr(signal, 'sigwait'), |
| 657 | 'need signal.sigwait()') |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 658 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 659 | 'need signal.pthread_sigmask()') |
Victor Stinner | 10c30d6 | 2011-06-10 01:39:53 +0200 | [diff] [blame] | 660 | @unittest.skipIf(threading is None, "test needs threading module") |
| 661 | def test_sigwait_thread(self): |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 662 | # Check that calling sigwait() from a thread doesn't suspend the whole |
| 663 | # process. A new interpreter is spawned to avoid problems when mixing |
| 664 | # threads and fork(): only async-safe functions are allowed between |
| 665 | # fork() and exec(). |
| 666 | assert_python_ok("-c", """if True: |
| 667 | import os, threading, sys, time, signal |
Victor Stinner | 10c30d6 | 2011-06-10 01:39:53 +0200 | [diff] [blame] | 668 | |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 669 | # the default handler terminates the process |
| 670 | signum = signal.SIGUSR1 |
| 671 | |
| 672 | def kill_later(): |
| 673 | # wait until the main thread is waiting in sigwait() |
| 674 | time.sleep(1) |
| 675 | os.kill(os.getpid(), signum) |
| 676 | |
| 677 | # the signal must be blocked by all the threads |
| 678 | signal.pthread_sigmask(signal.SIG_BLOCK, [signum]) |
| 679 | killer = threading.Thread(target=kill_later) |
Victor Stinner | af49460 | 2011-06-10 12:48:13 +0200 | [diff] [blame] | 680 | killer.start() |
| 681 | received = signal.sigwait([signum]) |
| 682 | if received != signum: |
| 683 | print("sigwait() received %s, not %s" % (received, signum), |
| 684 | file=sys.stderr) |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 685 | sys.exit(1) |
Victor Stinner | 10c30d6 | 2011-06-10 01:39:53 +0200 | [diff] [blame] | 686 | killer.join() |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 687 | # unblock the signal, which should have been cleared by sigwait() |
| 688 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 689 | """) |
Victor Stinner | af49460 | 2011-06-10 12:48:13 +0200 | [diff] [blame] | 690 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 691 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 692 | 'need signal.pthread_sigmask()') |
| 693 | def test_pthread_sigmask_arguments(self): |
| 694 | self.assertRaises(TypeError, signal.pthread_sigmask) |
| 695 | self.assertRaises(TypeError, signal.pthread_sigmask, 1) |
| 696 | self.assertRaises(TypeError, signal.pthread_sigmask, 1, 2, 3) |
| 697 | self.assertRaises(OSError, signal.pthread_sigmask, 1700, []) |
| 698 | |
| 699 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 700 | 'need signal.pthread_sigmask()') |
| 701 | def test_pthread_sigmask(self): |
| 702 | test_blocked_signals = self.can_test_blocked_signals(False) |
| 703 | signum = signal.SIGUSR1 |
Victor Stinner | 6fd49e1 | 2011-05-04 12:38:03 +0200 | [diff] [blame] | 704 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 705 | # Install our signal handler |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 706 | old_handler = signal.signal(signum, self.handler) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 707 | self.addCleanup(signal.signal, signum, old_handler) |
| 708 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 709 | # Unblock SIGUSR1 (and copy the old mask) to test our signal handler |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 710 | old_mask = signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 711 | self.addCleanup(signal.pthread_sigmask, signal.SIG_SETMASK, old_mask) |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 712 | with self.assertRaises(ZeroDivisionError): |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 713 | self.kill(signum) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 714 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 715 | # Block and then raise SIGUSR1. The signal is blocked: the signal |
| 716 | # handler is not called, and the signal is now pending |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 717 | signal.pthread_sigmask(signal.SIG_BLOCK, [signum]) |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 718 | if test_blocked_signals: |
| 719 | self.kill(signum) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 720 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 721 | # Check the new mask |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 722 | blocked = self.read_sigmask() |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 723 | self.assertIn(signum, blocked) |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 724 | self.assertEqual(old_mask ^ blocked, {signum}) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 725 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 726 | # Unblock SIGUSR1 |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 727 | if test_blocked_signals: |
Victor Stinner | 6fd49e1 | 2011-05-04 12:38:03 +0200 | [diff] [blame] | 728 | with self.assertRaises(ZeroDivisionError): |
| 729 | # unblock the pending signal calls immediatly the signal handler |
| 730 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 731 | else: |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 732 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 733 | with self.assertRaises(ZeroDivisionError): |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 734 | self.kill(signum) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 735 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 736 | # Check the new mask |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 737 | unblocked = self.read_sigmask() |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 738 | self.assertNotIn(signum, unblocked) |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 739 | self.assertEqual(blocked ^ unblocked, {signum}) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 740 | self.assertSequenceEqual(old_mask, unblocked) |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 741 | # Finally, restore the previous signal handler and the signal mask |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 742 | |
| 743 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 744 | def test_main(): |
Antoine Pitrou | 8189ab8 | 2011-03-20 17:35:32 +0100 | [diff] [blame] | 745 | try: |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 746 | support.run_unittest(PosixTests, InterProcessSignalTests, |
Antoine Pitrou | 8189ab8 | 2011-03-20 17:35:32 +0100 | [diff] [blame] | 747 | WakeupSignalTests, SiginterruptTest, |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 748 | ItimerTest, WindowsSignalTests, |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 749 | PendingSignalsTests) |
Antoine Pitrou | 8189ab8 | 2011-03-20 17:35:32 +0100 | [diff] [blame] | 750 | finally: |
| 751 | support.reap_children() |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 752 | |
| 753 | |
| 754 | if __name__ == "__main__": |
| 755 | test_main() |