Debian Codegraph @

Lib/test/_test_eintr.py

raw
1 """
2 This test suite exercises some system calls subject to interruption with EINTR,
3 to check that it is actually handled transparently.
4 It is intended to be run by the main test suite within a child process, to
5 ensure there is no background thread running (so that signals are delivered to
6 the correct thread).
7 Signals are generated in-process using setitimer(ITIMER_REAL), which allows
8 sub-second periodicity (contrarily to signal()).
9 """
10
11 import contextlib
12 import faulthandler
13 import fcntl
14 import os
15 import platform
16 import select
17 import signal
18 import socket
19 import subprocess
20 import sys
21 import textwrap
22 import time
23 import unittest
24
25 from test import support
26 from test.support import os_helper
27 from test.support import socket_helper
28
29
30 # gh-109592: Tolerate a difference of 20 ms when comparing timings
31 # (clock resolution)
32 CLOCK_RES = 0.020
33
34
35 @contextlib.contextmanager
36 def kill_on_error(proc):
37 """Context manager killing the subprocess if a Python exception is raised."""
38 with proc:
39 try:
40 yield proc
41 except:
42 proc.kill()
43 raise
44
45
46 @unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()")
47 class EINTRBaseTest(unittest.TestCase):
48 """ Base class for EINTR tests. """
49
50 # delay for initial signal delivery
51 signal_delay = 0.1
52 # signal delivery periodicity
53 signal_period = 0.1
54 # default sleep time for tests - should obviously have:
55 # sleep_time > signal_period
56 sleep_time = 0.2
57
58 def sighandler(self, signum, frame):
59 self.signals += 1
60
61 def setUp(self):
62 self.signals = 0
63 self.orig_handler = signal.signal(signal.SIGALRM, self.sighandler)
64 signal.setitimer(signal.ITIMER_REAL, self.signal_delay,
65 self.signal_period)
66
67 # Use faulthandler as watchdog to debug when a test hangs
68 # (timeout of 10 minutes)
69 faulthandler.dump_traceback_later(10 * 60, exit=True,
70 file=sys.__stderr__)
71
72 @staticmethod
73 def stop_alarm():
74 signal.setitimer(signal.ITIMER_REAL, 0, 0)
75
76 def tearDown(self):
77 self.stop_alarm()
78 signal.signal(signal.SIGALRM, self.orig_handler)
79 faulthandler.cancel_dump_traceback_later()
80
81 def subprocess(self, *args, **kw):
82 cmd_args = (sys.executable, '-c') + args
83 return subprocess.Popen(cmd_args, **kw)
84
85 def check_elapsed_time(self, elapsed):
86 self.assertGreaterEqual(elapsed, self.sleep_time - CLOCK_RES)
87
88
89 @unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()")
90 class OSEINTRTest(EINTRBaseTest):
91 """ EINTR tests for the os module. """
92
93 def new_sleep_process(self):
94 code = f'import time; time.sleep({self.sleep_time!r})'
95 return self.subprocess(code)
96
97 def _test_wait_multiple(self, wait_func):
98 num = 3
99 processes = [self.new_sleep_process() for _ in range(num)]
100 for _ in range(num):
101 wait_func()
102 # Call the Popen method to avoid a ResourceWarning
103 for proc in processes:
104 proc.wait()
105
106 def test_wait(self):
107 self._test_wait_multiple(os.wait)
108
109 @unittest.skipUnless(hasattr(os, 'wait3'), 'requires wait3()')
110 def test_wait3(self):
111 self._test_wait_multiple(lambda: os.wait3(0))
112
113 def _test_wait_single(self, wait_func):
114 proc = self.new_sleep_process()
115 wait_func(proc.pid)
116 # Call the Popen method to avoid a ResourceWarning
117 proc.wait()
118
119 def test_waitpid(self):
120 self._test_wait_single(lambda pid: os.waitpid(pid, 0))
121
122 @unittest.skipUnless(hasattr(os, 'wait4'), 'requires wait4()')
123 def test_wait4(self):
124 self._test_wait_single(lambda pid: os.wait4(pid, 0))
125
126 def _interrupted_reads(self):
127 """Make a fd which will force block on read of expected bytes."""
128 rd, wr = os.pipe()
129 self.addCleanup(os.close, rd)
130 # wr closed explicitly by parent
131
132 # the payload below are smaller than PIPE_BUF, hence the writes will be
133 # atomic
134 data = [b"hello", b"world", b"spam"]
135
136 code = '\n'.join((
137 'import os, sys, time',
138 '',
139 'wr = int(sys.argv[1])',
140 f'data = {data!r}',
141 f'sleep_time = {self.sleep_time!r}',
142 '',
143 'for item in data:',
144 ' # let the parent block on read()',
145 ' time.sleep(sleep_time)',
146 ' os.write(wr, item)',
147 ))
148
149 proc = self.subprocess(code, str(wr), pass_fds=[wr])
150 with kill_on_error(proc):
151 os.close(wr)
152 for datum in data:
153 yield rd, datum
154 self.assertEqual(proc.wait(), 0)
155
156 def test_read(self):
157 for fd, expected in self._interrupted_reads():
158 self.assertEqual(expected, os.read(fd, len(expected)))
159
160 def test_readinto(self):
161 for fd, expected in self._interrupted_reads():
162 buffer = bytearray(len(expected))
163 self.assertEqual(os.readinto(fd, buffer), len(expected))
164 self.assertEqual(buffer, expected)
165
166 def test_write(self):
167 rd, wr = os.pipe()
168 self.addCleanup(os.close, wr)
169 # rd closed explicitly by parent
170
171 # we must write enough data for the write() to block
172 data = b"x" * support.PIPE_MAX_SIZE
173
174 code = '\n'.join((
175 'import io, os, sys, time',
176 '',
177 'rd = int(sys.argv[1])',
178 f'sleep_time = {self.sleep_time!r}',
179 f'data = b"x" * {support.PIPE_MAX_SIZE}',
180 'data_len = len(data)',
181 '',
182 '# let the parent block on write()',
183 'time.sleep(sleep_time)',
184 '',
185 'read_data = io.BytesIO()',
186 'while len(read_data.getvalue()) < data_len:',
187 ' chunk = os.read(rd, 2 * data_len)',
188 ' read_data.write(chunk)',
189 '',
190 'value = read_data.getvalue()',
191 'if value != data:',
192 ' raise Exception(f"read error: {len(value)}'
193 ' vs {data_len} bytes")',
194 ))
195
196 proc = self.subprocess(code, str(rd), pass_fds=[rd])
197 with kill_on_error(proc):
198 os.close(rd)
199 written = 0
200 while written < len(data):
201 written += os.write(wr, memoryview(data)[written:])
202 self.assertEqual(proc.wait(), 0)
203
204
205 @unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()")
206 class SocketEINTRTest(EINTRBaseTest):
207 """ EINTR tests for the socket module. """
208
209 @unittest.skipUnless(hasattr(socket, 'socketpair'), 'needs socketpair()')
210 def _test_recv(self, recv_func):
211 rd, wr = socket.socketpair()
212 self.addCleanup(rd.close)
213 # wr closed explicitly by parent
214
215 # single-byte payload guard us against partial recv
216 data = [b"x", b"y", b"z"]
217
218 code = '\n'.join((
219 'import os, socket, sys, time',
220 '',
221 'fd = int(sys.argv[1])',
222 f'family = {int(wr.family)}',
223 f'sock_type = {int(wr.type)}',
224 f'data = {data!r}',
225 f'sleep_time = {self.sleep_time!r}',
226 '',
227 'wr = socket.fromfd(fd, family, sock_type)',
228 'os.close(fd)',
229 '',
230 'with wr:',
231 ' for item in data:',
232 ' # let the parent block on recv()',
233 ' time.sleep(sleep_time)',
234 ' wr.sendall(item)',
235 ))
236
237 fd = wr.fileno()
238 proc = self.subprocess(code, str(fd), pass_fds=[fd])
239 with kill_on_error(proc):
240 wr.close()
241 for item in data:
242 self.assertEqual(item, recv_func(rd, len(item)))
243 self.assertEqual(proc.wait(), 0)
244
245 def test_recv(self):
246 self._test_recv(socket.socket.recv)
247
248 @unittest.skipUnless(hasattr(socket.socket, 'recvmsg'), 'needs recvmsg()')
249 def test_recvmsg(self):
250 self._test_recv(lambda sock, data: sock.recvmsg(data)[0])
251
252 def _test_send(self, send_func):
253 rd, wr = socket.socketpair()
254 self.addCleanup(wr.close)
255 # rd closed explicitly by parent
256
257 # we must send enough data for the send() to block
258 data = b"xyz" * (support.SOCK_MAX_SIZE // 3)
259
260 code = '\n'.join((
261 'import os, socket, sys, time',
262 '',
263 'fd = int(sys.argv[1])',
264 f'family = {int(rd.family)}',
265 f'sock_type = {int(rd.type)}',
266 f'sleep_time = {self.sleep_time!r}',
267 f'data = b"xyz" * {support.SOCK_MAX_SIZE // 3}',
268 'data_len = len(data)',
269 '',
270 'rd = socket.fromfd(fd, family, sock_type)',
271 'os.close(fd)',
272 '',
273 'with rd:',
274 ' # let the parent block on send()',
275 ' time.sleep(sleep_time)',
276 '',
277 ' received_data = bytearray(data_len)',
278 ' n = 0',
279 ' while n < data_len:',
280 ' n += rd.recv_into(memoryview(received_data)[n:])',
281 '',
282 'if received_data != data:',
283 ' raise Exception(f"recv error: {len(received_data)}'
284 ' vs {data_len} bytes")',
285 ))
286
287 fd = rd.fileno()
288 proc = self.subprocess(code, str(fd), pass_fds=[fd])
289 with kill_on_error(proc):
290 rd.close()
291 written = 0
292 while written < len(data):
293 sent = send_func(wr, memoryview(data)[written:])
294 # sendall() returns None
295 written += len(data) if sent is None else sent
296 self.assertEqual(proc.wait(), 0)
297
298 def test_send(self):
299 self._test_send(socket.socket.send)
300
301 def test_sendall(self):
302 self._test_send(socket.socket.sendall)
303
304 @unittest.skipUnless(hasattr(socket.socket, 'sendmsg'), 'needs sendmsg()')
305 def test_sendmsg(self):
306 self._test_send(lambda sock, data: sock.sendmsg([data]))
307
308 def test_accept(self):
309 sock = socket.create_server((socket_helper.HOST, 0))
310 self.addCleanup(sock.close)
311 port = sock.getsockname()[1]
312
313 code = '\n'.join((
314 'import socket, time',
315 '',
316 f'host = {socket_helper.HOST!r}',
317 f'port = {port}',
318 f'sleep_time = {self.sleep_time!r}',
319 '',
320 '# let parent block on accept()',
321 'time.sleep(sleep_time)',
322 'with socket.create_connection((host, port)):',
323 ' time.sleep(sleep_time)',
324 ))
325
326 proc = self.subprocess(code)
327 with kill_on_error(proc):
328 client_sock, _ = sock.accept()
329 client_sock.close()
330 self.assertEqual(proc.wait(), 0)
331
332 # Issue #25122: There is a race condition in the FreeBSD kernel on
333 # handling signals in the FIFO device. Skip the test until the bug is
334 # fixed in the kernel.
335 # https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=203162
336 @support.requires_freebsd_version(10, 3)
337 @unittest.skipUnless(hasattr(os, 'mkfifo'), 'needs mkfifo()')
338 def _test_open(self, do_open_close_reader, do_open_close_writer):
339 filename = os_helper.TESTFN
340
341 # Use a fifo: until the child opens it for reading, the parent will
342 # block when trying to open it for writing.
343 os_helper.unlink(filename)
344 try:
345 os.mkfifo(filename)
346 except PermissionError as exc:
347 self.skipTest(f'os.mkfifo(): {exc!r}')
348 self.addCleanup(os_helper.unlink, filename)
349
350 code = '\n'.join((
351 'import os, time',
352 '',
353 f'path = {filename!a}',
354 f'sleep_time = {self.sleep_time!r}',
355 '',
356 '# let the parent block',
357 'time.sleep(sleep_time)',
358 '',
359 do_open_close_reader,
360 ))
361
362 proc = self.subprocess(code)
363 with kill_on_error(proc):
364 do_open_close_writer(filename)
365 self.assertEqual(proc.wait(), 0)
366
367 def python_open(self, path):
368 fp = open(path, 'w')
369 fp.close()
370
371 @unittest.skipIf(sys.platform == "darwin",
372 "hangs under macOS; see bpo-25234, bpo-35363")
373 def test_open(self):
374 self._test_open("fp = open(path, 'r')\nfp.close()",
375 self.python_open)
376
377 def os_open(self, path):
378 fd = os.open(path, os.O_WRONLY)
379 os.close(fd)
380
381 @unittest.skipIf(sys.platform == "darwin",
382 "hangs under macOS; see bpo-25234, bpo-35363")
383 @unittest.skipIf(sys.platform.startswith('netbsd'),
384 "hangs on NetBSD; see gh-137397")
385 def test_os_open(self):
386 self._test_open("fd = os.open(path, os.O_RDONLY)\nos.close(fd)",
387 self.os_open)
388
389
390 @unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()")
391 class TimeEINTRTest(EINTRBaseTest):
392 """ EINTR tests for the time module. """
393
394 def test_sleep(self):
395 t0 = time.monotonic()
396 time.sleep(self.sleep_time)
397 self.stop_alarm()
398 dt = time.monotonic() - t0
399 self.check_elapsed_time(dt)
400
401
402 @unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()")
403 # bpo-30320: Need pthread_sigmask() to block the signal, otherwise the test
404 # is vulnerable to a race condition between the child and the parent processes.
405 @unittest.skipUnless(hasattr(signal, 'pthread_sigmask'),
406 'need signal.pthread_sigmask()')
407 class SignalEINTRTest(EINTRBaseTest):
408 """ EINTR tests for the signal module. """
409
410 def check_sigwait(self, wait_func):
411 signum = signal.SIGUSR1
412
413 old_handler = signal.signal(signum, lambda *args: None)
414 self.addCleanup(signal.signal, signum, old_handler)
415
416 code = '\n'.join((
417 'import os, time',
418 f'pid = {os.getpid()}',
419 f'signum = {int(signum)}',
420 f'sleep_time = {self.sleep_time!r}',
421 'time.sleep(sleep_time)',
422 'os.kill(pid, signum)',
423 ))
424
425 signal.pthread_sigmask(signal.SIG_BLOCK, [signum])
426 self.addCleanup(signal.pthread_sigmask, signal.SIG_UNBLOCK, [signum])
427
428 proc = self.subprocess(code)
429 with kill_on_error(proc):
430 wait_func(signum)
431
432 self.assertEqual(proc.wait(), 0)
433
434 @unittest.skipUnless(hasattr(signal, 'sigwaitinfo'),
435 'need signal.sigwaitinfo()')
436 def test_sigwaitinfo(self):
437 def wait_func(signum):
438 signal.sigwaitinfo([signum])
439
440 self.check_sigwait(wait_func)
441
442 @unittest.skipUnless(hasattr(signal, 'sigtimedwait'),
443 'need signal.sigwaitinfo()')
444 def test_sigtimedwait(self):
445 def wait_func(signum):
446 signal.sigtimedwait([signum], 120.0)
447
448 self.check_sigwait(wait_func)
449
450
451 @unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()")
452 class SelectEINTRTest(EINTRBaseTest):
453 """ EINTR tests for the select module. """
454
455 def test_select(self):
456 t0 = time.monotonic()
457 select.select([], [], [], self.sleep_time)
458 dt = time.monotonic() - t0
459 self.stop_alarm()
460 self.check_elapsed_time(dt)
461
462 @unittest.skipIf(sys.platform == "darwin",
463 "poll may fail on macOS; see issue #28087")
464 @unittest.skipUnless(hasattr(select, 'poll'), 'need select.poll')
465 def test_poll(self):
466 poller = select.poll()
467
468 t0 = time.monotonic()
469 poller.poll(self.sleep_time * 1e3)
470 dt = time.monotonic() - t0
471 self.stop_alarm()
472 self.check_elapsed_time(dt)
473
474 @unittest.skipUnless(hasattr(select, 'epoll'), 'need select.epoll')
475 def test_epoll(self):
476 poller = select.epoll()
477 self.addCleanup(poller.close)
478
479 t0 = time.monotonic()
480 poller.poll(self.sleep_time)
481 dt = time.monotonic() - t0
482 self.stop_alarm()
483 self.check_elapsed_time(dt)
484
485 @unittest.skipUnless(hasattr(select, 'kqueue'), 'need select.kqueue')
486 def test_kqueue(self):
487 kqueue = select.kqueue()
488 self.addCleanup(kqueue.close)
489
490 t0 = time.monotonic()
491 kqueue.control(None, 1, self.sleep_time)
492 dt = time.monotonic() - t0
493 self.stop_alarm()
494 self.check_elapsed_time(dt)
495
496 @unittest.skipUnless(hasattr(select, 'devpoll'), 'need select.devpoll')
497 def test_devpoll(self):
498 poller = select.devpoll()
499 self.addCleanup(poller.close)
500
501 t0 = time.monotonic()
502 poller.poll(self.sleep_time * 1e3)
503 dt = time.monotonic() - t0
504 self.stop_alarm()
505 self.check_elapsed_time(dt)
506
507
508 class FCNTLEINTRTest(EINTRBaseTest):
509 def _lock(self, lock_func, lock_name):
510 self.addCleanup(os_helper.unlink, os_helper.TESTFN)
511 rd1, wr1 = os.pipe()
512 rd2, wr2 = os.pipe()
513 for fd in (rd1, wr1, rd2, wr2):
514 self.addCleanup(os.close, fd)
515 code = textwrap.dedent(f"""
516 import fcntl, os, time
517 with open('{os_helper.TESTFN}', 'wb') as f:
518 fcntl.{lock_name}(f, fcntl.LOCK_EX)
519 os.write({wr1}, b"ok")
520 _ = os.read({rd2}, 2) # wait for parent process
521 time.sleep({self.sleep_time})
522 """)
523 proc = self.subprocess(code, pass_fds=[wr1, rd2])
524 with kill_on_error(proc):
525 with open(os_helper.TESTFN, 'wb') as f:
526 # synchronize the subprocess
527 ok = os.read(rd1, 2)
528 self.assertEqual(ok, b"ok")
529
530 # notify the child that the parent is ready
531 start_time = time.monotonic()
532 os.write(wr2, b"go")
533
534 # the child locked the file just a moment ago for 'sleep_time' seconds
535 # that means that the lock below will block for 'sleep_time' minus some
536 # potential context switch delay
537 lock_func(f, fcntl.LOCK_EX)
538 dt = time.monotonic() - start_time
539 self.stop_alarm()
540 self.check_elapsed_time(dt)
541 proc.wait()
542
543 # Issue 35633: See https://bugs.python.org/issue35633#msg333662
544 # skip test rather than accept PermissionError from all platforms
545 @unittest.skipIf(platform.system() == "AIX", "AIX returns PermissionError")
546 def test_lockf(self):
547 self._lock(fcntl.lockf, "lockf")
548
549 def test_flock(self):
550 self._lock(fcntl.flock, "flock")
551
552
553 if __name__ == "__main__":
554 unittest.main()