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