Victor Stinner | d6debb2 | 2017-03-27 16:05:26 +0200 | [diff] [blame] | 1 | import os |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 2 | import random |
Guido van Rossum | 4f17e3e | 1995-03-16 15:07:38 +0000 | [diff] [blame] | 3 | import signal |
Victor Stinner | 1151710 | 2014-07-29 23:31:34 +0200 | [diff] [blame] | 4 | import socket |
Antoine Pitrou | f7d090c | 2017-06-29 16:40:14 +0200 | [diff] [blame^] | 5 | import statistics |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 6 | import subprocess |
Victor Stinner | d6debb2 | 2017-03-27 16:05:26 +0200 | [diff] [blame] | 7 | import sys |
| 8 | import time |
| 9 | import unittest |
| 10 | from test import support |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 11 | from test.support.script_helper import assert_python_ok, spawn_python |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 12 | try: |
| 13 | import threading |
| 14 | except ImportError: |
| 15 | threading = None |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 16 | try: |
| 17 | import _testcapi |
| 18 | except ImportError: |
| 19 | _testcapi = None |
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 | |
Giampaolo Rodola' | e09fb71 | 2014-04-04 15:34:17 +0200 | [diff] [blame] | 22 | class GenericTests(unittest.TestCase): |
| 23 | |
Stefan Krah | 63c4b24 | 2014-04-15 22:40:06 +0200 | [diff] [blame] | 24 | @unittest.skipIf(threading is None, "test needs threading module") |
Giampaolo Rodola' | e09fb71 | 2014-04-04 15:34:17 +0200 | [diff] [blame] | 25 | def test_enums(self): |
| 26 | for name in dir(signal): |
| 27 | sig = getattr(signal, name) |
| 28 | if name in {'SIG_DFL', 'SIG_IGN'}: |
| 29 | self.assertIsInstance(sig, signal.Handlers) |
| 30 | elif name in {'SIG_BLOCK', 'SIG_UNBLOCK', 'SIG_SETMASK'}: |
| 31 | self.assertIsInstance(sig, signal.Sigmasks) |
| 32 | elif name.startswith('SIG') and not name.startswith('SIG_'): |
| 33 | self.assertIsInstance(sig, signal.Signals) |
| 34 | elif name.startswith('CTRL_'): |
| 35 | self.assertIsInstance(sig, signal.Signals) |
| 36 | self.assertEqual(sys.platform, "win32") |
| 37 | |
| 38 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 39 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 40 | class PosixTests(unittest.TestCase): |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 41 | def trivial_signal_handler(self, *args): |
| 42 | pass |
| 43 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 44 | def test_out_of_range_signal_number_raises_error(self): |
| 45 | self.assertRaises(ValueError, signal.getsignal, 4242) |
| 46 | |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 47 | self.assertRaises(ValueError, signal.signal, 4242, |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 48 | self.trivial_signal_handler) |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 49 | |
| 50 | def test_setting_signal_handler_to_none_raises_error(self): |
| 51 | self.assertRaises(TypeError, signal.signal, |
| 52 | signal.SIGUSR1, None) |
| 53 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 54 | def test_getsignal(self): |
| 55 | hup = signal.signal(signal.SIGHUP, self.trivial_signal_handler) |
Giampaolo Rodola' | e09fb71 | 2014-04-04 15:34:17 +0200 | [diff] [blame] | 56 | self.assertIsInstance(hup, signal.Handlers) |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 57 | self.assertEqual(signal.getsignal(signal.SIGHUP), |
| 58 | self.trivial_signal_handler) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 59 | signal.signal(signal.SIGHUP, hup) |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 60 | self.assertEqual(signal.getsignal(signal.SIGHUP), hup) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 61 | |
Victor Stinner | 32eb840 | 2016-03-15 11:12:35 +0100 | [diff] [blame] | 62 | # Issue 3864, unknown if this affects earlier versions of freebsd also |
| 63 | @unittest.skipIf(sys.platform=='freebsd6', |
| 64 | 'inter process signals not reliable (do not mix well with threading) ' |
| 65 | 'on freebsd6') |
| 66 | def test_interprocess_signal(self): |
| 67 | dirname = os.path.dirname(__file__) |
| 68 | script = os.path.join(dirname, 'signalinterproctester.py') |
| 69 | assert_python_ok(script) |
| 70 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 71 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 72 | @unittest.skipUnless(sys.platform == "win32", "Windows specific") |
| 73 | class WindowsSignalTests(unittest.TestCase): |
| 74 | def test_issue9324(self): |
Brian Curtin | eccd4d9 | 2010-10-01 15:09:53 +0000 | [diff] [blame] | 75 | # Updated for issue #10003, adding SIGBREAK |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 76 | handler = lambda x, y: None |
Nick Coghlan | 60b3ac7 | 2013-08-03 22:56:30 +1000 | [diff] [blame] | 77 | checked = set() |
Brian Curtin | eccd4d9 | 2010-10-01 15:09:53 +0000 | [diff] [blame] | 78 | for sig in (signal.SIGABRT, signal.SIGBREAK, signal.SIGFPE, |
| 79 | signal.SIGILL, signal.SIGINT, signal.SIGSEGV, |
| 80 | signal.SIGTERM): |
Nick Coghlan | 60b3ac7 | 2013-08-03 22:56:30 +1000 | [diff] [blame] | 81 | # Set and then reset a handler for signals that work on windows. |
| 82 | # Issue #18396, only for signals without a C-level handler. |
| 83 | if signal.getsignal(sig) is not None: |
| 84 | signal.signal(sig, signal.signal(sig, handler)) |
| 85 | checked.add(sig) |
| 86 | # Issue #18396: Ensure the above loop at least tested *something* |
| 87 | self.assertTrue(checked) |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 88 | |
| 89 | with self.assertRaises(ValueError): |
| 90 | signal.signal(-1, handler) |
Brian Curtin | 80cd4bf | 2010-08-07 03:52:38 +0000 | [diff] [blame] | 91 | |
| 92 | with self.assertRaises(ValueError): |
| 93 | signal.signal(7, handler) |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 94 | |
| 95 | |
Benjamin Peterson | c68a4a0 | 2013-01-18 00:10:24 -0500 | [diff] [blame] | 96 | class WakeupFDTests(unittest.TestCase): |
| 97 | |
| 98 | def test_invalid_fd(self): |
Victor Stinner | 1d8948e | 2014-07-24 22:51:05 +0200 | [diff] [blame] | 99 | fd = support.make_bad_fd() |
Victor Stinner | a7d03d9 | 2014-07-21 17:17:28 +0200 | [diff] [blame] | 100 | self.assertRaises((ValueError, OSError), |
| 101 | signal.set_wakeup_fd, fd) |
Benjamin Peterson | c68a4a0 | 2013-01-18 00:10:24 -0500 | [diff] [blame] | 102 | |
Victor Stinner | 1151710 | 2014-07-29 23:31:34 +0200 | [diff] [blame] | 103 | def test_invalid_socket(self): |
| 104 | sock = socket.socket() |
| 105 | fd = sock.fileno() |
| 106 | sock.close() |
| 107 | self.assertRaises((ValueError, OSError), |
| 108 | signal.set_wakeup_fd, fd) |
| 109 | |
Victor Stinner | d18ccd1 | 2014-07-24 21:58:53 +0200 | [diff] [blame] | 110 | def test_set_wakeup_fd_result(self): |
Victor Stinner | 1d8948e | 2014-07-24 22:51:05 +0200 | [diff] [blame] | 111 | r1, w1 = os.pipe() |
| 112 | self.addCleanup(os.close, r1) |
| 113 | self.addCleanup(os.close, w1) |
| 114 | r2, w2 = os.pipe() |
| 115 | self.addCleanup(os.close, r2) |
| 116 | self.addCleanup(os.close, w2) |
Victor Stinner | d18ccd1 | 2014-07-24 21:58:53 +0200 | [diff] [blame] | 117 | |
Victor Stinner | 7cea44d | 2014-08-27 14:02:36 +0200 | [diff] [blame] | 118 | if hasattr(os, 'set_blocking'): |
| 119 | os.set_blocking(w1, False) |
| 120 | os.set_blocking(w2, False) |
Victor Stinner | 3822760 | 2014-08-27 12:59:44 +0200 | [diff] [blame] | 121 | |
Victor Stinner | 1d8948e | 2014-07-24 22:51:05 +0200 | [diff] [blame] | 122 | signal.set_wakeup_fd(w1) |
Victor Stinner | c82a179 | 2014-07-24 22:55:12 +0200 | [diff] [blame] | 123 | self.assertEqual(signal.set_wakeup_fd(w2), w1) |
| 124 | self.assertEqual(signal.set_wakeup_fd(-1), w2) |
| 125 | self.assertEqual(signal.set_wakeup_fd(-1), -1) |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 126 | |
Victor Stinner | 1151710 | 2014-07-29 23:31:34 +0200 | [diff] [blame] | 127 | def test_set_wakeup_fd_socket_result(self): |
| 128 | sock1 = socket.socket() |
| 129 | self.addCleanup(sock1.close) |
Victor Stinner | 3822760 | 2014-08-27 12:59:44 +0200 | [diff] [blame] | 130 | sock1.setblocking(False) |
Victor Stinner | 1151710 | 2014-07-29 23:31:34 +0200 | [diff] [blame] | 131 | fd1 = sock1.fileno() |
| 132 | |
| 133 | sock2 = socket.socket() |
| 134 | self.addCleanup(sock2.close) |
Victor Stinner | 3822760 | 2014-08-27 12:59:44 +0200 | [diff] [blame] | 135 | sock2.setblocking(False) |
Victor Stinner | 1151710 | 2014-07-29 23:31:34 +0200 | [diff] [blame] | 136 | fd2 = sock2.fileno() |
| 137 | |
| 138 | signal.set_wakeup_fd(fd1) |
Victor Stinner | da565a7 | 2014-07-30 10:03:03 +0200 | [diff] [blame] | 139 | self.assertEqual(signal.set_wakeup_fd(fd2), fd1) |
| 140 | self.assertEqual(signal.set_wakeup_fd(-1), fd2) |
| 141 | self.assertEqual(signal.set_wakeup_fd(-1), -1) |
Victor Stinner | 1151710 | 2014-07-29 23:31:34 +0200 | [diff] [blame] | 142 | |
Victor Stinner | 3822760 | 2014-08-27 12:59:44 +0200 | [diff] [blame] | 143 | # On Windows, files are always blocking and Windows does not provide a |
| 144 | # function to test if a socket is in non-blocking mode. |
| 145 | @unittest.skipIf(sys.platform == "win32", "tests specific to POSIX") |
| 146 | def test_set_wakeup_fd_blocking(self): |
| 147 | rfd, wfd = os.pipe() |
| 148 | self.addCleanup(os.close, rfd) |
| 149 | self.addCleanup(os.close, wfd) |
| 150 | |
| 151 | # fd must be non-blocking |
| 152 | os.set_blocking(wfd, True) |
| 153 | with self.assertRaises(ValueError) as cm: |
| 154 | signal.set_wakeup_fd(wfd) |
| 155 | self.assertEqual(str(cm.exception), |
| 156 | "the fd %s must be in non-blocking mode" % wfd) |
| 157 | |
| 158 | # non-blocking is ok |
| 159 | os.set_blocking(wfd, False) |
| 160 | signal.set_wakeup_fd(wfd) |
| 161 | signal.set_wakeup_fd(-1) |
| 162 | |
Benjamin Peterson | c68a4a0 | 2013-01-18 00:10:24 -0500 | [diff] [blame] | 163 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 164 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 165 | class WakeupSignalTests(unittest.TestCase): |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 166 | @unittest.skipIf(_testcapi is None, 'need _testcapi') |
Charles-François Natali | 027f9a3 | 2011-10-02 18:36:05 +0200 | [diff] [blame] | 167 | def check_wakeup(self, test_body, *signals, ordered=True): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 168 | # use a subprocess to have only one thread |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 169 | code = """if 1: |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 170 | import _testcapi |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 171 | import os |
| 172 | import signal |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 173 | import struct |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 174 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 175 | signals = {!r} |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 176 | |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 177 | def handler(signum, frame): |
| 178 | pass |
| 179 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 180 | def check_signum(signals): |
| 181 | data = os.read(read, len(signals)+1) |
| 182 | raised = struct.unpack('%uB' % len(data), data) |
Charles-François Natali | 027f9a3 | 2011-10-02 18:36:05 +0200 | [diff] [blame] | 183 | if not {!r}: |
| 184 | raised = set(raised) |
| 185 | signals = set(signals) |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 186 | if raised != signals: |
| 187 | raise Exception("%r != %r" % (raised, signals)) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 188 | |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 189 | {} |
| 190 | |
| 191 | signal.signal(signal.SIGALRM, handler) |
| 192 | read, write = os.pipe() |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 193 | os.set_blocking(write, False) |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 194 | signal.set_wakeup_fd(write) |
| 195 | |
| 196 | test() |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 197 | check_signum(signals) |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 198 | |
| 199 | os.close(read) |
| 200 | os.close(write) |
Giampaolo Rodola' | e09fb71 | 2014-04-04 15:34:17 +0200 | [diff] [blame] | 201 | """.format(tuple(map(int, signals)), ordered, test_body) |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 202 | |
| 203 | assert_python_ok('-c', code) |
Victor Stinner | d49b1f1 | 2011-05-08 02:03:15 +0200 | [diff] [blame] | 204 | |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 205 | @unittest.skipIf(_testcapi is None, 'need _testcapi') |
Antoine Pitrou | 6f6ec37 | 2013-08-17 20:27:56 +0200 | [diff] [blame] | 206 | def test_wakeup_write_error(self): |
| 207 | # Issue #16105: write() errors in the C signal handler should not |
| 208 | # pass silently. |
| 209 | # Use a subprocess to have only one thread. |
| 210 | code = """if 1: |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 211 | import _testcapi |
Antoine Pitrou | 6f6ec37 | 2013-08-17 20:27:56 +0200 | [diff] [blame] | 212 | import errno |
Antoine Pitrou | 6f6ec37 | 2013-08-17 20:27:56 +0200 | [diff] [blame] | 213 | import os |
| 214 | import signal |
| 215 | import sys |
Antoine Pitrou | 6f6ec37 | 2013-08-17 20:27:56 +0200 | [diff] [blame] | 216 | from test.support import captured_stderr |
| 217 | |
| 218 | def handler(signum, frame): |
| 219 | 1/0 |
| 220 | |
| 221 | signal.signal(signal.SIGALRM, handler) |
| 222 | r, w = os.pipe() |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 223 | os.set_blocking(r, False) |
Antoine Pitrou | 6f6ec37 | 2013-08-17 20:27:56 +0200 | [diff] [blame] | 224 | |
| 225 | # Set wakeup_fd a read-only file descriptor to trigger the error |
| 226 | signal.set_wakeup_fd(r) |
| 227 | try: |
| 228 | with captured_stderr() as err: |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 229 | _testcapi.raise_signal(signal.SIGALRM) |
Antoine Pitrou | 6f6ec37 | 2013-08-17 20:27:56 +0200 | [diff] [blame] | 230 | except ZeroDivisionError: |
| 231 | # An ignored exception should have been printed out on stderr |
| 232 | err = err.getvalue() |
| 233 | if ('Exception ignored when trying to write to the signal wakeup fd' |
| 234 | not in err): |
| 235 | raise AssertionError(err) |
| 236 | if ('OSError: [Errno %d]' % errno.EBADF) not in err: |
| 237 | raise AssertionError(err) |
| 238 | else: |
| 239 | raise AssertionError("ZeroDivisionError not raised") |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 240 | |
| 241 | os.close(r) |
| 242 | os.close(w) |
Antoine Pitrou | 6f6ec37 | 2013-08-17 20:27:56 +0200 | [diff] [blame] | 243 | """ |
Antoine Pitrou | 8f0bdda | 2013-08-17 21:43:47 +0200 | [diff] [blame] | 244 | r, w = os.pipe() |
| 245 | try: |
| 246 | os.write(r, b'x') |
| 247 | except OSError: |
| 248 | pass |
| 249 | else: |
| 250 | self.skipTest("OS doesn't report write() error on the read end of a pipe") |
| 251 | finally: |
| 252 | os.close(r) |
| 253 | os.close(w) |
Antoine Pitrou | 6f6ec37 | 2013-08-17 20:27:56 +0200 | [diff] [blame] | 254 | |
| 255 | assert_python_ok('-c', code) |
| 256 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 257 | def test_wakeup_fd_early(self): |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 258 | self.check_wakeup("""def test(): |
| 259 | import select |
| 260 | import time |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 261 | |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 262 | TIMEOUT_FULL = 10 |
| 263 | TIMEOUT_HALF = 5 |
| 264 | |
Victor Stinner | 749a6a8 | 2015-03-30 22:09:14 +0200 | [diff] [blame] | 265 | class InterruptSelect(Exception): |
| 266 | pass |
| 267 | |
| 268 | def handler(signum, frame): |
| 269 | raise InterruptSelect |
| 270 | signal.signal(signal.SIGALRM, handler) |
| 271 | |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 272 | signal.alarm(1) |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 273 | |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 274 | # We attempt to get a signal during the sleep, |
| 275 | # before select is called |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 276 | try: |
| 277 | select.select([], [], [], TIMEOUT_FULL) |
Victor Stinner | 749a6a8 | 2015-03-30 22:09:14 +0200 | [diff] [blame] | 278 | except InterruptSelect: |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 279 | pass |
| 280 | else: |
| 281 | raise Exception("select() was not interrupted") |
| 282 | |
| 283 | before_time = time.monotonic() |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 284 | select.select([read], [], [], TIMEOUT_FULL) |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 285 | after_time = time.monotonic() |
| 286 | dt = after_time - before_time |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 287 | if dt >= TIMEOUT_HALF: |
| 288 | raise Exception("%s >= %s" % (dt, TIMEOUT_HALF)) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 289 | """, signal.SIGALRM) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 290 | |
| 291 | def test_wakeup_fd_during(self): |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 292 | self.check_wakeup("""def test(): |
| 293 | import select |
| 294 | import time |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 295 | |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 296 | TIMEOUT_FULL = 10 |
| 297 | TIMEOUT_HALF = 5 |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 298 | |
Victor Stinner | 749a6a8 | 2015-03-30 22:09:14 +0200 | [diff] [blame] | 299 | class InterruptSelect(Exception): |
| 300 | pass |
| 301 | |
| 302 | def handler(signum, frame): |
| 303 | raise InterruptSelect |
| 304 | signal.signal(signal.SIGALRM, handler) |
| 305 | |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 306 | signal.alarm(1) |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 307 | before_time = time.monotonic() |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 308 | # We attempt to get a signal during the select call |
| 309 | try: |
| 310 | select.select([read], [], [], TIMEOUT_FULL) |
Victor Stinner | 749a6a8 | 2015-03-30 22:09:14 +0200 | [diff] [blame] | 311 | except InterruptSelect: |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 312 | pass |
| 313 | else: |
Victor Stinner | 749a6a8 | 2015-03-30 22:09:14 +0200 | [diff] [blame] | 314 | raise Exception("select() was not interrupted") |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 315 | after_time = time.monotonic() |
Victor Stinner | e40b3aa | 2011-07-04 17:35:10 +0200 | [diff] [blame] | 316 | dt = after_time - before_time |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 317 | if dt >= TIMEOUT_HALF: |
| 318 | raise Exception("%s >= %s" % (dt, TIMEOUT_HALF)) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 319 | """, signal.SIGALRM) |
Victor Stinner | d49b1f1 | 2011-05-08 02:03:15 +0200 | [diff] [blame] | 320 | |
| 321 | def test_signum(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 322 | self.check_wakeup("""def test(): |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 323 | import _testcapi |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 324 | signal.signal(signal.SIGUSR1, handler) |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 325 | _testcapi.raise_signal(signal.SIGUSR1) |
| 326 | _testcapi.raise_signal(signal.SIGALRM) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 327 | """, signal.SIGUSR1, signal.SIGALRM) |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 328 | |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 329 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 330 | 'need signal.pthread_sigmask()') |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 331 | def test_pending(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 332 | self.check_wakeup("""def test(): |
| 333 | signum1 = signal.SIGUSR1 |
| 334 | signum2 = signal.SIGUSR2 |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 335 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 336 | signal.signal(signum1, handler) |
| 337 | signal.signal(signum2, handler) |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 338 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 339 | signal.pthread_sigmask(signal.SIG_BLOCK, (signum1, signum2)) |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 340 | _testcapi.raise_signal(signum1) |
| 341 | _testcapi.raise_signal(signum2) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 342 | # Unblocking the 2 signals calls the C signal handler twice |
| 343 | signal.pthread_sigmask(signal.SIG_UNBLOCK, (signum1, signum2)) |
Charles-François Natali | 027f9a3 | 2011-10-02 18:36:05 +0200 | [diff] [blame] | 344 | """, signal.SIGUSR1, signal.SIGUSR2, ordered=False) |
Victor Stinner | c13ef66 | 2011-05-25 02:35:58 +0200 | [diff] [blame] | 345 | |
Christian Heimes | 5fb7c2a | 2007-12-24 08:52:31 +0000 | [diff] [blame] | 346 | |
Victor Stinner | 1151710 | 2014-07-29 23:31:34 +0200 | [diff] [blame] | 347 | @unittest.skipUnless(hasattr(socket, 'socketpair'), 'need socket.socketpair') |
| 348 | class WakeupSocketSignalTests(unittest.TestCase): |
| 349 | |
| 350 | @unittest.skipIf(_testcapi is None, 'need _testcapi') |
| 351 | def test_socket(self): |
| 352 | # use a subprocess to have only one thread |
| 353 | code = """if 1: |
| 354 | import signal |
| 355 | import socket |
| 356 | import struct |
| 357 | import _testcapi |
| 358 | |
| 359 | signum = signal.SIGINT |
| 360 | signals = (signum,) |
| 361 | |
| 362 | def handler(signum, frame): |
| 363 | pass |
| 364 | |
| 365 | signal.signal(signum, handler) |
| 366 | |
| 367 | read, write = socket.socketpair() |
| 368 | read.setblocking(False) |
| 369 | write.setblocking(False) |
| 370 | signal.set_wakeup_fd(write.fileno()) |
| 371 | |
| 372 | _testcapi.raise_signal(signum) |
| 373 | |
| 374 | data = read.recv(1) |
| 375 | if not data: |
| 376 | raise Exception("no signum written") |
| 377 | raised = struct.unpack('B', data) |
| 378 | if raised != signals: |
| 379 | raise Exception("%r != %r" % (raised, signals)) |
| 380 | |
| 381 | read.close() |
| 382 | write.close() |
| 383 | """ |
| 384 | |
| 385 | assert_python_ok('-c', code) |
| 386 | |
| 387 | @unittest.skipIf(_testcapi is None, 'need _testcapi') |
| 388 | def test_send_error(self): |
| 389 | # Use a subprocess to have only one thread. |
| 390 | if os.name == 'nt': |
| 391 | action = 'send' |
| 392 | else: |
| 393 | action = 'write' |
| 394 | code = """if 1: |
| 395 | import errno |
| 396 | import signal |
| 397 | import socket |
| 398 | import sys |
| 399 | import time |
| 400 | import _testcapi |
| 401 | from test.support import captured_stderr |
| 402 | |
| 403 | signum = signal.SIGINT |
| 404 | |
| 405 | def handler(signum, frame): |
| 406 | pass |
| 407 | |
| 408 | signal.signal(signum, handler) |
| 409 | |
| 410 | read, write = socket.socketpair() |
| 411 | read.setblocking(False) |
| 412 | write.setblocking(False) |
| 413 | |
| 414 | signal.set_wakeup_fd(write.fileno()) |
| 415 | |
| 416 | # Close sockets: send() will fail |
| 417 | read.close() |
| 418 | write.close() |
| 419 | |
| 420 | with captured_stderr() as err: |
| 421 | _testcapi.raise_signal(signum) |
| 422 | |
| 423 | err = err.getvalue() |
| 424 | if ('Exception ignored when trying to {action} to the signal wakeup fd' |
| 425 | not in err): |
| 426 | raise AssertionError(err) |
| 427 | """.format(action=action) |
| 428 | assert_python_ok('-c', code) |
| 429 | |
| 430 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 431 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 432 | class SiginterruptTest(unittest.TestCase): |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 433 | |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 434 | def readpipe_interrupted(self, interrupt): |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 435 | """Perform a read during which a signal will arrive. Return True if the |
| 436 | read is interrupted by the signal and raises an exception. Return False |
| 437 | if it returns normally. |
| 438 | """ |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 439 | # use a subprocess to have only one thread, to have a timeout on the |
| 440 | # blocking read and to not touch signal handling in this process |
| 441 | code = """if 1: |
| 442 | import errno |
| 443 | import os |
| 444 | import signal |
| 445 | import sys |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 446 | |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 447 | interrupt = %r |
| 448 | r, w = os.pipe() |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 449 | |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 450 | def handler(signum, frame): |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 451 | 1 / 0 |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 452 | |
| 453 | signal.signal(signal.SIGALRM, handler) |
| 454 | if interrupt is not None: |
| 455 | signal.siginterrupt(signal.SIGALRM, interrupt) |
| 456 | |
Victor Stinner | dfde0d4 | 2011-07-01 15:58:39 +0200 | [diff] [blame] | 457 | print("ready") |
| 458 | sys.stdout.flush() |
| 459 | |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 460 | # run the test twice |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 461 | try: |
| 462 | for loop in range(2): |
| 463 | # send a SIGALRM in a second (during the read) |
| 464 | signal.alarm(1) |
| 465 | try: |
| 466 | # blocking call: read from a pipe without data |
| 467 | os.read(r, 1) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 468 | except ZeroDivisionError: |
| 469 | pass |
Victor Stinner | 56e8c29 | 2014-07-21 12:30:22 +0200 | [diff] [blame] | 470 | else: |
| 471 | sys.exit(2) |
| 472 | sys.exit(3) |
| 473 | finally: |
| 474 | os.close(r) |
| 475 | os.close(w) |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 476 | """ % (interrupt,) |
| 477 | with spawn_python('-c', code) as process: |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 478 | try: |
Victor Stinner | 4527365 | 2011-06-22 22:15:51 +0200 | [diff] [blame] | 479 | # wait until the child process is loaded and has started |
| 480 | first_line = process.stdout.readline() |
| 481 | |
Victor Stinner | 19e5bcd | 2011-07-01 15:59:54 +0200 | [diff] [blame] | 482 | stdout, stderr = process.communicate(timeout=5.0) |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 483 | except subprocess.TimeoutExpired: |
| 484 | process.kill() |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 485 | return False |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 486 | else: |
Victor Stinner | 4527365 | 2011-06-22 22:15:51 +0200 | [diff] [blame] | 487 | stdout = first_line + stdout |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 488 | exitcode = process.wait() |
| 489 | if exitcode not in (2, 3): |
Antoine Pitrou | dab4e8a | 2014-05-11 19:05:23 +0200 | [diff] [blame] | 490 | raise Exception("Child error (exit code %s): %r" |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 491 | % (exitcode, stdout)) |
| 492 | return (exitcode == 3) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 493 | |
| 494 | def test_without_siginterrupt(self): |
Victor Stinner | 3a7f0f0 | 2011-05-08 02:10:36 +0200 | [diff] [blame] | 495 | # If a signal handler is installed and siginterrupt is not called |
| 496 | # at all, when that signal arrives, it interrupts a syscall that's in |
| 497 | # progress. |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 498 | interrupted = self.readpipe_interrupted(None) |
| 499 | self.assertTrue(interrupted) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 500 | |
| 501 | def test_siginterrupt_on(self): |
Victor Stinner | 3a7f0f0 | 2011-05-08 02:10:36 +0200 | [diff] [blame] | 502 | # If a signal handler is installed and siginterrupt is called with |
| 503 | # a true value for the second argument, when that signal arrives, it |
| 504 | # interrupts a syscall that's in progress. |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 505 | interrupted = self.readpipe_interrupted(True) |
| 506 | self.assertTrue(interrupted) |
Christian Heimes | 8640e74 | 2008-02-23 16:23:06 +0000 | [diff] [blame] | 507 | |
| 508 | def test_siginterrupt_off(self): |
Victor Stinner | 3a7f0f0 | 2011-05-08 02:10:36 +0200 | [diff] [blame] | 509 | # If a signal handler is installed and siginterrupt is called with |
| 510 | # a false value for the second argument, when that signal arrives, it |
| 511 | # does not interrupt a syscall that's in progress. |
Victor Stinner | d628496 | 2011-06-20 23:28:09 +0200 | [diff] [blame] | 512 | interrupted = self.readpipe_interrupted(False) |
| 513 | self.assertFalse(interrupted) |
Jean-Paul Calderone | 9c39bc7 | 2010-05-09 03:25:16 +0000 | [diff] [blame] | 514 | |
| 515 | |
Brian Curtin | 3f004b1 | 2010-08-06 19:34:52 +0000 | [diff] [blame] | 516 | @unittest.skipIf(sys.platform == "win32", "Not valid on Windows") |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 517 | class ItimerTest(unittest.TestCase): |
| 518 | def setUp(self): |
| 519 | self.hndl_called = False |
| 520 | self.hndl_count = 0 |
| 521 | self.itimer = None |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 522 | self.old_alarm = signal.signal(signal.SIGALRM, self.sig_alrm) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 523 | |
| 524 | def tearDown(self): |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 525 | signal.signal(signal.SIGALRM, self.old_alarm) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 526 | if self.itimer is not None: # test_itimer_exc doesn't change this attr |
| 527 | # just ensure that itimer is stopped |
| 528 | signal.setitimer(self.itimer, 0) |
| 529 | |
| 530 | def sig_alrm(self, *args): |
| 531 | self.hndl_called = True |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 532 | |
| 533 | def sig_vtalrm(self, *args): |
| 534 | self.hndl_called = True |
| 535 | |
| 536 | if self.hndl_count > 3: |
| 537 | # it shouldn't be here, because it should have been disabled. |
| 538 | raise signal.ItimerError("setitimer didn't disable ITIMER_VIRTUAL " |
| 539 | "timer.") |
| 540 | elif self.hndl_count == 3: |
| 541 | # disable ITIMER_VIRTUAL, this function shouldn't be called anymore |
| 542 | signal.setitimer(signal.ITIMER_VIRTUAL, 0) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 543 | |
| 544 | self.hndl_count += 1 |
| 545 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 546 | def sig_prof(self, *args): |
| 547 | self.hndl_called = True |
| 548 | signal.setitimer(signal.ITIMER_PROF, 0) |
| 549 | |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 550 | def test_itimer_exc(self): |
| 551 | # XXX I'm assuming -1 is an invalid itimer, but maybe some platform |
| 552 | # defines it ? |
| 553 | self.assertRaises(signal.ItimerError, signal.setitimer, -1, 0) |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 554 | # Negative times are treated as zero on some platforms. |
| 555 | if 0: |
| 556 | self.assertRaises(signal.ItimerError, |
| 557 | signal.setitimer, signal.ITIMER_REAL, -1) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 558 | |
| 559 | def test_itimer_real(self): |
| 560 | self.itimer = signal.ITIMER_REAL |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 561 | signal.setitimer(self.itimer, 1.0) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 562 | signal.pause() |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 563 | self.assertEqual(self.hndl_called, True) |
| 564 | |
R. David Murray | 44546f8 | 2010-04-21 01:59:28 +0000 | [diff] [blame] | 565 | # Issue 3864, unknown if this affects earlier versions of freebsd also |
Gregory P. Smith | 397cd8a | 2010-10-17 04:23:21 +0000 | [diff] [blame] | 566 | @unittest.skipIf(sys.platform in ('freebsd6', 'netbsd5'), |
| 567 | '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] | 568 | def test_itimer_virtual(self): |
| 569 | self.itimer = signal.ITIMER_VIRTUAL |
| 570 | signal.signal(signal.SIGVTALRM, self.sig_vtalrm) |
| 571 | signal.setitimer(self.itimer, 0.3, 0.2) |
| 572 | |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 573 | start_time = time.monotonic() |
| 574 | while time.monotonic() - start_time < 60.0: |
Mark Dickinson | 9507887 | 2009-10-04 18:47:48 +0000 | [diff] [blame] | 575 | # use up some virtual time by doing real work |
| 576 | _ = pow(12345, 67890, 10000019) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 577 | if signal.getitimer(self.itimer) == (0.0, 0.0): |
| 578 | break # sig_vtalrm handler stopped this itimer |
Stefan Krah | f1da97304 | 2010-04-20 08:15:14 +0000 | [diff] [blame] | 579 | else: # Issue 8424 |
Benjamin Peterson | 9b140f6 | 2010-05-15 18:00:56 +0000 | [diff] [blame] | 580 | self.skipTest("timeout: likely cause: machine too slow or load too " |
| 581 | "high") |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 582 | |
| 583 | # virtual itimer should be (0.0, 0.0) now |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 584 | self.assertEqual(signal.getitimer(self.itimer), (0.0, 0.0)) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 585 | # and the handler should have been called |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 586 | self.assertEqual(self.hndl_called, True) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 587 | |
R. David Murray | 44546f8 | 2010-04-21 01:59:28 +0000 | [diff] [blame] | 588 | # Issue 3864, unknown if this affects earlier versions of freebsd also |
| 589 | @unittest.skipIf(sys.platform=='freebsd6', |
| 590 | '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] | 591 | def test_itimer_prof(self): |
| 592 | self.itimer = signal.ITIMER_PROF |
| 593 | signal.signal(signal.SIGPROF, self.sig_prof) |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 594 | signal.setitimer(self.itimer, 0.2, 0.2) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 595 | |
Victor Stinner | 79d68f9 | 2015-03-19 21:54:09 +0100 | [diff] [blame] | 596 | start_time = time.monotonic() |
| 597 | while time.monotonic() - start_time < 60.0: |
Mark Dickinson | 7837347 | 2009-10-31 10:39:21 +0000 | [diff] [blame] | 598 | # do some work |
| 599 | _ = pow(12345, 67890, 10000019) |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 600 | if signal.getitimer(self.itimer) == (0.0, 0.0): |
| 601 | break # sig_prof handler stopped this itimer |
Stefan Krah | f1da97304 | 2010-04-20 08:15:14 +0000 | [diff] [blame] | 602 | else: # Issue 8424 |
Benjamin Peterson | 9b140f6 | 2010-05-15 18:00:56 +0000 | [diff] [blame] | 603 | self.skipTest("timeout: likely cause: machine too slow or load too " |
| 604 | "high") |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 605 | |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 606 | # profiling itimer should be (0.0, 0.0) now |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 607 | self.assertEqual(signal.getitimer(self.itimer), (0.0, 0.0)) |
Neal Norwitz | f5c7c2e | 2008-04-05 04:47:45 +0000 | [diff] [blame] | 608 | # and the handler should have been called |
Martin v. Löwis | 823725e | 2008-03-24 13:39:54 +0000 | [diff] [blame] | 609 | self.assertEqual(self.hndl_called, True) |
| 610 | |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 611 | |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 612 | class PendingSignalsTests(unittest.TestCase): |
| 613 | """ |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 614 | Test pthread_sigmask(), pthread_kill(), sigpending() and sigwait() |
| 615 | functions. |
Victor Stinner | 35b300c | 2011-05-04 13:20:35 +0200 | [diff] [blame] | 616 | """ |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 617 | @unittest.skipUnless(hasattr(signal, 'sigpending'), |
| 618 | 'need signal.sigpending()') |
| 619 | def test_sigpending_empty(self): |
| 620 | self.assertEqual(signal.sigpending(), set()) |
| 621 | |
| 622 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 623 | 'need signal.pthread_sigmask()') |
| 624 | @unittest.skipUnless(hasattr(signal, 'sigpending'), |
| 625 | 'need signal.sigpending()') |
| 626 | def test_sigpending(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 627 | code = """if 1: |
| 628 | import os |
| 629 | import signal |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 630 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 631 | def handler(signum, frame): |
| 632 | 1/0 |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 633 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 634 | signum = signal.SIGUSR1 |
| 635 | signal.signal(signum, handler) |
| 636 | |
| 637 | signal.pthread_sigmask(signal.SIG_BLOCK, [signum]) |
| 638 | os.kill(os.getpid(), signum) |
| 639 | pending = signal.sigpending() |
Giampaolo Rodola' | e09fb71 | 2014-04-04 15:34:17 +0200 | [diff] [blame] | 640 | for sig in pending: |
| 641 | assert isinstance(sig, signal.Signals), repr(pending) |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 642 | if pending != {signum}: |
| 643 | raise Exception('%s != {%s}' % (pending, signum)) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 644 | try: |
| 645 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 646 | except ZeroDivisionError: |
| 647 | pass |
| 648 | else: |
| 649 | raise Exception("ZeroDivisionError not raised") |
| 650 | """ |
| 651 | assert_python_ok('-c', code) |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 652 | |
| 653 | @unittest.skipUnless(hasattr(signal, 'pthread_kill'), |
| 654 | 'need signal.pthread_kill()') |
| 655 | def test_pthread_kill(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 656 | code = """if 1: |
| 657 | import signal |
| 658 | import threading |
| 659 | import sys |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 660 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 661 | signum = signal.SIGUSR1 |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 662 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 663 | def handler(signum, frame): |
| 664 | 1/0 |
| 665 | |
| 666 | signal.signal(signum, handler) |
| 667 | |
| 668 | if sys.platform == 'freebsd6': |
| 669 | # Issue #12392 and #12469: send a signal to the main thread |
| 670 | # doesn't work before the creation of the first thread on |
| 671 | # FreeBSD 6 |
| 672 | def noop(): |
| 673 | pass |
| 674 | thread = threading.Thread(target=noop) |
| 675 | thread.start() |
| 676 | thread.join() |
| 677 | |
| 678 | tid = threading.get_ident() |
| 679 | try: |
| 680 | signal.pthread_kill(tid, signum) |
| 681 | except ZeroDivisionError: |
| 682 | pass |
| 683 | else: |
| 684 | raise Exception("ZeroDivisionError not raised") |
| 685 | """ |
| 686 | assert_python_ok('-c', code) |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 687 | |
Victor Stinner | 7f294d1 | 2011-06-10 14:02:10 +0200 | [diff] [blame] | 688 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 689 | 'need signal.pthread_sigmask()') |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 690 | def wait_helper(self, blocked, test): |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 691 | """ |
| 692 | test: body of the "def test(signum):" function. |
| 693 | blocked: number of the blocked signal |
| 694 | """ |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 695 | code = '''if 1: |
| 696 | import signal |
| 697 | import sys |
Giampaolo Rodola' | e09fb71 | 2014-04-04 15:34:17 +0200 | [diff] [blame] | 698 | from signal import Signals |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 699 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 700 | def handler(signum, frame): |
| 701 | 1/0 |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 702 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 703 | %s |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 704 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 705 | blocked = %s |
| 706 | signum = signal.SIGALRM |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 707 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 708 | # child: block and wait the signal |
| 709 | try: |
| 710 | signal.signal(signum, handler) |
| 711 | signal.pthread_sigmask(signal.SIG_BLOCK, [blocked]) |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 712 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 713 | # Do the tests |
| 714 | test(signum) |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 715 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 716 | # The handler must not be called on unblock |
| 717 | try: |
| 718 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [blocked]) |
| 719 | except ZeroDivisionError: |
| 720 | print("the signal handler has been called", |
| 721 | file=sys.stderr) |
| 722 | sys.exit(1) |
| 723 | except BaseException as err: |
| 724 | print("error: {}".format(err), file=sys.stderr) |
| 725 | sys.stderr.flush() |
| 726 | sys.exit(1) |
| 727 | ''' % (test.strip(), blocked) |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 728 | |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 729 | # sig*wait* must be called with the signal blocked: since the current |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 730 | # process might have several threads running, use a subprocess to have |
| 731 | # a single thread. |
| 732 | assert_python_ok('-c', code) |
Victor Stinner | af49460 | 2011-06-10 12:48:13 +0200 | [diff] [blame] | 733 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 734 | @unittest.skipUnless(hasattr(signal, 'sigwait'), |
| 735 | 'need signal.sigwait()') |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 736 | def test_sigwait(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 737 | self.wait_helper(signal.SIGALRM, ''' |
| 738 | def test(signum): |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 739 | signal.alarm(1) |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 740 | received = signal.sigwait([signum]) |
Giampaolo Rodola' | e09fb71 | 2014-04-04 15:34:17 +0200 | [diff] [blame] | 741 | assert isinstance(received, signal.Signals), received |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 742 | if received != signum: |
| 743 | raise Exception('received %s, not %s' % (received, signum)) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 744 | ''') |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 745 | |
| 746 | @unittest.skipUnless(hasattr(signal, 'sigwaitinfo'), |
| 747 | 'need signal.sigwaitinfo()') |
| 748 | def test_sigwaitinfo(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 749 | self.wait_helper(signal.SIGALRM, ''' |
| 750 | def test(signum): |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 751 | signal.alarm(1) |
| 752 | info = signal.sigwaitinfo([signum]) |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 753 | if info.si_signo != signum: |
| 754 | raise Exception("info.si_signo != %s" % signum) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 755 | ''') |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 756 | |
| 757 | @unittest.skipUnless(hasattr(signal, 'sigtimedwait'), |
| 758 | 'need signal.sigtimedwait()') |
| 759 | def test_sigtimedwait(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 760 | self.wait_helper(signal.SIGALRM, ''' |
| 761 | def test(signum): |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 762 | signal.alarm(1) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 763 | info = signal.sigtimedwait([signum], 10.1000) |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 764 | if info.si_signo != signum: |
| 765 | raise Exception('info.si_signo != %s' % signum) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 766 | ''') |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 767 | |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 768 | @unittest.skipUnless(hasattr(signal, 'sigtimedwait'), |
| 769 | 'need signal.sigtimedwait()') |
| 770 | def test_sigtimedwait_poll(self): |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 771 | # check that polling with sigtimedwait works |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 772 | self.wait_helper(signal.SIGALRM, ''' |
| 773 | def test(signum): |
Victor Stinner | 9e8b82f | 2011-06-29 10:43:02 +0200 | [diff] [blame] | 774 | import os |
| 775 | os.kill(os.getpid(), signum) |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 776 | info = signal.sigtimedwait([signum], 0) |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 777 | if info.si_signo != signum: |
| 778 | raise Exception('info.si_signo != %s' % signum) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 779 | ''') |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 780 | |
| 781 | @unittest.skipUnless(hasattr(signal, 'sigtimedwait'), |
| 782 | 'need signal.sigtimedwait()') |
| 783 | def test_sigtimedwait_timeout(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 784 | self.wait_helper(signal.SIGALRM, ''' |
| 785 | def test(signum): |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 786 | received = signal.sigtimedwait([signum], 1.0) |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 787 | if received is not None: |
| 788 | raise Exception("received=%r" % (received,)) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 789 | ''') |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 790 | |
| 791 | @unittest.skipUnless(hasattr(signal, 'sigtimedwait'), |
| 792 | 'need signal.sigtimedwait()') |
| 793 | def test_sigtimedwait_negative_timeout(self): |
| 794 | signum = signal.SIGALRM |
Victor Stinner | 643cd68 | 2012-03-02 22:54:03 +0100 | [diff] [blame] | 795 | self.assertRaises(ValueError, signal.sigtimedwait, [signum], -1.0) |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 796 | |
Ross Lagerwall | bc80822 | 2011-06-25 12:13:40 +0200 | [diff] [blame] | 797 | @unittest.skipUnless(hasattr(signal, 'sigwait'), |
| 798 | 'need signal.sigwait()') |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 799 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 800 | 'need signal.pthread_sigmask()') |
Victor Stinner | 10c30d6 | 2011-06-10 01:39:53 +0200 | [diff] [blame] | 801 | @unittest.skipIf(threading is None, "test needs threading module") |
| 802 | def test_sigwait_thread(self): |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 803 | # Check that calling sigwait() from a thread doesn't suspend the whole |
| 804 | # process. A new interpreter is spawned to avoid problems when mixing |
| 805 | # threads and fork(): only async-safe functions are allowed between |
| 806 | # fork() and exec(). |
| 807 | assert_python_ok("-c", """if True: |
| 808 | import os, threading, sys, time, signal |
Victor Stinner | 10c30d6 | 2011-06-10 01:39:53 +0200 | [diff] [blame] | 809 | |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 810 | # the default handler terminates the process |
| 811 | signum = signal.SIGUSR1 |
| 812 | |
| 813 | def kill_later(): |
| 814 | # wait until the main thread is waiting in sigwait() |
| 815 | time.sleep(1) |
| 816 | os.kill(os.getpid(), signum) |
| 817 | |
| 818 | # the signal must be blocked by all the threads |
| 819 | signal.pthread_sigmask(signal.SIG_BLOCK, [signum]) |
| 820 | killer = threading.Thread(target=kill_later) |
Victor Stinner | af49460 | 2011-06-10 12:48:13 +0200 | [diff] [blame] | 821 | killer.start() |
| 822 | received = signal.sigwait([signum]) |
| 823 | if received != signum: |
| 824 | print("sigwait() received %s, not %s" % (received, signum), |
| 825 | file=sys.stderr) |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 826 | sys.exit(1) |
Victor Stinner | 10c30d6 | 2011-06-10 01:39:53 +0200 | [diff] [blame] | 827 | killer.join() |
Victor Stinner | 415007e | 2011-06-13 16:19:06 +0200 | [diff] [blame] | 828 | # unblock the signal, which should have been cleared by sigwait() |
| 829 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
| 830 | """) |
Victor Stinner | af49460 | 2011-06-10 12:48:13 +0200 | [diff] [blame] | 831 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 832 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 833 | 'need signal.pthread_sigmask()') |
| 834 | def test_pthread_sigmask_arguments(self): |
| 835 | self.assertRaises(TypeError, signal.pthread_sigmask) |
| 836 | self.assertRaises(TypeError, signal.pthread_sigmask, 1) |
| 837 | self.assertRaises(TypeError, signal.pthread_sigmask, 1, 2, 3) |
| 838 | self.assertRaises(OSError, signal.pthread_sigmask, 1700, []) |
| 839 | |
| 840 | @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'), |
| 841 | 'need signal.pthread_sigmask()') |
| 842 | def test_pthread_sigmask(self): |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 843 | code = """if 1: |
| 844 | import signal |
| 845 | import os; import threading |
| 846 | |
| 847 | def handler(signum, frame): |
| 848 | 1/0 |
| 849 | |
| 850 | def kill(signum): |
| 851 | os.kill(os.getpid(), signum) |
| 852 | |
Giampaolo Rodola' | e09fb71 | 2014-04-04 15:34:17 +0200 | [diff] [blame] | 853 | def check_mask(mask): |
| 854 | for sig in mask: |
| 855 | assert isinstance(sig, signal.Signals), repr(sig) |
| 856 | |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 857 | def read_sigmask(): |
Giampaolo Rodola' | e09fb71 | 2014-04-04 15:34:17 +0200 | [diff] [blame] | 858 | sigmask = signal.pthread_sigmask(signal.SIG_BLOCK, []) |
| 859 | check_mask(sigmask) |
| 860 | return sigmask |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 861 | |
Victor Stinner | b3e7219 | 2011-05-08 01:46:11 +0200 | [diff] [blame] | 862 | signum = signal.SIGUSR1 |
Victor Stinner | 6fd49e1 | 2011-05-04 12:38:03 +0200 | [diff] [blame] | 863 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 864 | # Install our signal handler |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 865 | old_handler = signal.signal(signum, handler) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 866 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 867 | # Unblock SIGUSR1 (and copy the old mask) to test our signal handler |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 868 | old_mask = signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
Giampaolo Rodola' | e09fb71 | 2014-04-04 15:34:17 +0200 | [diff] [blame] | 869 | check_mask(old_mask) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 870 | try: |
| 871 | kill(signum) |
| 872 | except ZeroDivisionError: |
| 873 | pass |
| 874 | else: |
| 875 | raise Exception("ZeroDivisionError not raised") |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 876 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 877 | # Block and then raise SIGUSR1. The signal is blocked: the signal |
| 878 | # handler is not called, and the signal is now pending |
Giampaolo Rodola' | e09fb71 | 2014-04-04 15:34:17 +0200 | [diff] [blame] | 879 | mask = signal.pthread_sigmask(signal.SIG_BLOCK, [signum]) |
| 880 | check_mask(mask) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 881 | kill(signum) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 882 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 883 | # Check the new mask |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 884 | blocked = read_sigmask() |
Giampaolo Rodola' | e09fb71 | 2014-04-04 15:34:17 +0200 | [diff] [blame] | 885 | check_mask(blocked) |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 886 | if signum not in blocked: |
| 887 | raise Exception("%s not in %s" % (signum, blocked)) |
| 888 | if old_mask ^ blocked != {signum}: |
| 889 | raise Exception("%s ^ %s != {%s}" % (old_mask, blocked, signum)) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 890 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 891 | # Unblock SIGUSR1 |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 892 | try: |
R David Murray | fc06999 | 2013-12-13 20:52:19 -0500 | [diff] [blame] | 893 | # unblock the pending signal calls immediately the signal handler |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 894 | signal.pthread_sigmask(signal.SIG_UNBLOCK, [signum]) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 895 | except ZeroDivisionError: |
| 896 | pass |
| 897 | else: |
| 898 | raise Exception("ZeroDivisionError not raised") |
| 899 | try: |
| 900 | kill(signum) |
| 901 | except ZeroDivisionError: |
| 902 | pass |
| 903 | else: |
| 904 | raise Exception("ZeroDivisionError not raised") |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 905 | |
Victor Stinner | d0e516d | 2011-05-03 14:57:12 +0200 | [diff] [blame] | 906 | # Check the new mask |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 907 | unblocked = read_sigmask() |
Victor Stinner | 0a01f13 | 2011-07-04 18:06:35 +0200 | [diff] [blame] | 908 | if signum in unblocked: |
| 909 | raise Exception("%s in %s" % (signum, unblocked)) |
| 910 | if blocked ^ unblocked != {signum}: |
| 911 | raise Exception("%s ^ %s != {%s}" % (blocked, unblocked, signum)) |
| 912 | if old_mask != unblocked: |
| 913 | raise Exception("%s != %s" % (old_mask, unblocked)) |
Victor Stinner | d554cdf | 2011-07-04 17:49:40 +0200 | [diff] [blame] | 914 | """ |
| 915 | assert_python_ok('-c', code) |
| 916 | |
| 917 | @unittest.skipIf(sys.platform == 'freebsd6', |
| 918 | "issue #12392: send a signal to the main thread doesn't work " |
| 919 | "before the creation of the first thread on FreeBSD 6") |
| 920 | @unittest.skipUnless(hasattr(signal, 'pthread_kill'), |
| 921 | 'need signal.pthread_kill()') |
| 922 | def test_pthread_kill_main_thread(self): |
| 923 | # Test that a signal can be sent to the main thread with pthread_kill() |
| 924 | # before any other thread has been created (see issue #12392). |
| 925 | code = """if True: |
| 926 | import threading |
| 927 | import signal |
| 928 | import sys |
| 929 | |
| 930 | def handler(signum, frame): |
| 931 | sys.exit(3) |
| 932 | |
| 933 | signal.signal(signal.SIGUSR1, handler) |
| 934 | signal.pthread_kill(threading.get_ident(), signal.SIGUSR1) |
| 935 | sys.exit(2) |
| 936 | """ |
| 937 | |
| 938 | with spawn_python('-c', code) as process: |
| 939 | stdout, stderr = process.communicate() |
| 940 | exitcode = process.wait() |
| 941 | if exitcode != 3: |
| 942 | raise Exception("Child error (exit code %s): %s" % |
| 943 | (exitcode, stdout)) |
Victor Stinner | a929335 | 2011-04-30 15:21:58 +0200 | [diff] [blame] | 944 | |
| 945 | |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 946 | class StressTest(unittest.TestCase): |
| 947 | """ |
| 948 | Stress signal delivery, especially when a signal arrives in |
| 949 | the middle of recomputing the signal state or executing |
| 950 | previously tripped signal handlers. |
| 951 | """ |
| 952 | |
Antoine Pitrou | f7d090c | 2017-06-29 16:40:14 +0200 | [diff] [blame^] | 953 | def setsig(self, signum, handler): |
| 954 | old_handler = signal.signal(signum, handler) |
| 955 | self.addCleanup(signal.signal, signum, old_handler) |
| 956 | |
| 957 | def measure_itimer_resolution(self): |
| 958 | N = 20 |
| 959 | times = [] |
| 960 | |
| 961 | def handler(signum=None, frame=None): |
| 962 | if len(times) < N: |
| 963 | times.append(time.perf_counter()) |
| 964 | # 1 µs is the smallest possible timer interval, |
| 965 | # we want to measure what the concrete duration |
| 966 | # will be on this platform |
| 967 | signal.setitimer(signal.ITIMER_REAL, 1e-6) |
| 968 | |
| 969 | self.addCleanup(signal.setitimer, signal.ITIMER_REAL, 0) |
| 970 | self.setsig(signal.SIGALRM, handler) |
| 971 | handler() |
| 972 | while len(times) < N: |
| 973 | time.sleep(1e-3) |
| 974 | |
| 975 | durations = [times[i+1] - times[i] for i in range(len(times) - 1)] |
| 976 | med = statistics.median(durations) |
| 977 | if support.verbose: |
| 978 | print("detected median itimer() resolution: %.6f s." % (med,)) |
| 979 | return med |
| 980 | |
| 981 | def decide_itimer_count(self): |
| 982 | # Some systems have poor setitimer() resolution (for example |
| 983 | # measured around 20 ms. on FreeBSD 9), so decide on a reasonable |
| 984 | # number of sequential timers based on that. |
| 985 | reso = self.measure_itimer_resolution() |
| 986 | if reso <= 1e-4: |
| 987 | return 10000 |
| 988 | elif reso <= 1e-2: |
| 989 | return 100 |
| 990 | else: |
| 991 | self.skipTest("detected itimer resolution (%.3f s.) too high " |
| 992 | "(> 10 ms.) on this platform (or system too busy)" |
| 993 | % (reso,)) |
| 994 | |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 995 | @unittest.skipUnless(hasattr(signal, "setitimer"), |
| 996 | "test needs setitimer()") |
| 997 | def test_stress_delivery_dependent(self): |
| 998 | """ |
| 999 | This test uses dependent signal handlers. |
| 1000 | """ |
Antoine Pitrou | f7d090c | 2017-06-29 16:40:14 +0200 | [diff] [blame^] | 1001 | N = self.decide_itimer_count() |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 1002 | sigs = [] |
| 1003 | |
| 1004 | def first_handler(signum, frame): |
| 1005 | # 1e-6 is the minimum non-zero value for `setitimer()`. |
| 1006 | # Choose a random delay so as to improve chances of |
| 1007 | # triggering a race condition. Ideally the signal is received |
| 1008 | # when inside critical signal-handling routines such as |
| 1009 | # Py_MakePendingCalls(). |
| 1010 | signal.setitimer(signal.ITIMER_REAL, 1e-6 + random.random() * 1e-5) |
| 1011 | |
| 1012 | def second_handler(signum=None, frame=None): |
| 1013 | sigs.append(signum) |
| 1014 | |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 1015 | # Here on Linux, SIGPROF > SIGALRM > SIGUSR1. By using both |
| 1016 | # ascending and descending sequences (SIGUSR1 then SIGALRM, |
| 1017 | # SIGPROF then SIGALRM), we maximize chances of hitting a bug. |
Antoine Pitrou | f7d090c | 2017-06-29 16:40:14 +0200 | [diff] [blame^] | 1018 | self.setsig(signal.SIGPROF, first_handler) |
| 1019 | self.setsig(signal.SIGUSR1, first_handler) |
| 1020 | self.setsig(signal.SIGALRM, second_handler) # for ITIMER_REAL |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 1021 | |
| 1022 | expected_sigs = 0 |
| 1023 | deadline = time.time() + 15.0 |
| 1024 | |
| 1025 | while expected_sigs < N: |
| 1026 | os.kill(os.getpid(), signal.SIGPROF) |
| 1027 | expected_sigs += 1 |
| 1028 | # Wait for handlers to run to avoid signal coalescing |
| 1029 | while len(sigs) < expected_sigs and time.time() < deadline: |
| 1030 | time.sleep(1e-5) |
| 1031 | |
| 1032 | os.kill(os.getpid(), signal.SIGUSR1) |
| 1033 | expected_sigs += 1 |
| 1034 | while len(sigs) < expected_sigs and time.time() < deadline: |
| 1035 | time.sleep(1e-5) |
| 1036 | |
| 1037 | # All ITIMER_REAL signals should have been delivered to the |
| 1038 | # Python handler |
| 1039 | self.assertEqual(len(sigs), N, "Some signals were lost") |
| 1040 | |
| 1041 | @unittest.skipUnless(hasattr(signal, "setitimer"), |
| 1042 | "test needs setitimer()") |
| 1043 | def test_stress_delivery_simultaneous(self): |
| 1044 | """ |
| 1045 | This test uses simultaneous signal handlers. |
| 1046 | """ |
Antoine Pitrou | f7d090c | 2017-06-29 16:40:14 +0200 | [diff] [blame^] | 1047 | N = self.decide_itimer_count() |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 1048 | sigs = [] |
| 1049 | |
| 1050 | def handler(signum, frame): |
| 1051 | sigs.append(signum) |
| 1052 | |
Antoine Pitrou | f7d090c | 2017-06-29 16:40:14 +0200 | [diff] [blame^] | 1053 | self.setsig(signal.SIGUSR1, handler) |
| 1054 | self.setsig(signal.SIGALRM, handler) # for ITIMER_REAL |
Antoine Pitrou | c08177a | 2017-06-28 23:29:29 +0200 | [diff] [blame] | 1055 | |
| 1056 | expected_sigs = 0 |
| 1057 | deadline = time.time() + 15.0 |
| 1058 | |
| 1059 | while expected_sigs < N: |
| 1060 | # Hopefully the SIGALRM will be received somewhere during |
| 1061 | # initial processing of SIGUSR1. |
| 1062 | signal.setitimer(signal.ITIMER_REAL, 1e-6 + random.random() * 1e-5) |
| 1063 | os.kill(os.getpid(), signal.SIGUSR1) |
| 1064 | |
| 1065 | expected_sigs += 2 |
| 1066 | # Wait for handlers to run to avoid signal coalescing |
| 1067 | while len(sigs) < expected_sigs and time.time() < deadline: |
| 1068 | time.sleep(1e-5) |
| 1069 | |
| 1070 | # All ITIMER_REAL signals should have been delivered to the |
| 1071 | # Python handler |
| 1072 | self.assertEqual(len(sigs), N, "Some signals were lost") |
| 1073 | |
| 1074 | |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 1075 | def tearDownModule(): |
| 1076 | support.reap_children() |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 1077 | |
| 1078 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 1079 | unittest.main() |