_test_eintr.py 18 KB

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