Skip to content

[3.5] bpo-30320, bpo-25277: backport test_eintr enhancements from master to 3.5 #1532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 38 additions & 31 deletions Lib/test/eintrdata/eintr_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

import contextlib
import io
import faulthandler
import os
import select
import signal
Expand Down Expand Up @@ -50,6 +50,10 @@ def setUpClass(cls):
signal.setitimer(signal.ITIMER_REAL, cls.signal_delay,
cls.signal_period)

# Issue #25277: Use faulthandler to try to debug a hang on FreeBSD
if hasattr(faulthandler, 'dump_traceback_later'):
faulthandler.dump_traceback_later(10 * 60, exit=True)

@classmethod
def stop_alarm(cls):
signal.setitimer(signal.ITIMER_REAL, 0, 0)
Expand All @@ -58,6 +62,8 @@ def stop_alarm(cls):
def tearDownClass(cls):
cls.stop_alarm()
signal.signal(signal.SIGALRM, cls.orig_handler)
if hasattr(faulthandler, 'cancel_dump_traceback_later'):
faulthandler.cancel_dump_traceback_later()

def subprocess(self, *args, **kw):
cmd_args = (sys.executable, '-c') + args
Expand All @@ -77,6 +83,9 @@ def _test_wait_multiple(self, wait_func):
processes = [self.new_sleep_process() for _ in range(num)]
for _ in range(num):
wait_func()
# Call the Popen method to avoid a ResourceWarning
for proc in processes:
proc.wait()

def test_wait(self):
self._test_wait_multiple(os.wait)
Expand All @@ -88,6 +97,8 @@ def test_wait3(self):
def _test_wait_single(self, wait_func):
proc = self.new_sleep_process()
wait_func(proc.pid)
# Call the Popen method to avoid a ResourceWarning
proc.wait()

def test_waitpid(self):
self._test_wait_single(lambda pid: os.waitpid(pid, 0))
Expand Down Expand Up @@ -358,59 +369,55 @@ def test_sleep(self):


@unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()")
# bpo-30320: Need pthread_sigmask() to block the signal, otherwise the test
# is vulnerable to a race condition between the child and the parent processes.
@unittest.skipUnless(hasattr(signal, 'pthread_sigmask'),
'need signal.pthread_sigmask()')
class SignalEINTRTest(EINTRBaseTest):
""" EINTR tests for the signal module. """

@unittest.skipUnless(hasattr(signal, 'sigtimedwait'),
'need signal.sigtimedwait()')
def test_sigtimedwait(self):
t0 = time.monotonic()
signal.sigtimedwait([signal.SIGUSR1], self.sleep_time)
dt = time.monotonic() - t0
self.assertGreaterEqual(dt, self.sleep_time)

@unittest.skipUnless(hasattr(signal, 'sigwaitinfo'),
'need signal.sigwaitinfo()')
def test_sigwaitinfo(self):
# Issue #25277, #25868: give a few milliseconds to the parent process
# between os.write() and signal.sigwaitinfo() to works around a race
# condition
self.sleep_time = 0.100

def check_sigwait(self, wait_func):
signum = signal.SIGUSR1
pid = os.getpid()

old_handler = signal.signal(signum, lambda *args: None)
self.addCleanup(signal.signal, signum, old_handler)

rpipe, wpipe = os.pipe()

code = '\n'.join((
'import os, time',
'pid = %s' % os.getpid(),
'signum = %s' % int(signum),
'sleep_time = %r' % self.sleep_time,
'rpipe = %r' % rpipe,
'os.read(rpipe, 1)',
'os.close(rpipe)',
'time.sleep(sleep_time)',
'os.kill(pid, signum)',
))

old_mask = signal.pthread_sigmask(signal.SIG_BLOCK, [signum])
self.addCleanup(signal.pthread_sigmask, signal.SIG_UNBLOCK, [signum])

t0 = time.monotonic()
proc = self.subprocess(code, pass_fds=(rpipe,))
os.close(rpipe)
proc = self.subprocess(code)
with kill_on_error(proc):
# sync child-parent
os.write(wpipe, b'x')
os.close(wpipe)
wait_func(signum)
dt = time.monotonic() - t0

self.assertEqual(proc.wait(), 0)

# parent
@unittest.skipUnless(hasattr(signal, 'sigwaitinfo'),
'need signal.sigwaitinfo()')
def test_sigwaitinfo(self):
def wait_func(signum):
signal.sigwaitinfo([signum])
dt = time.monotonic() - t0
self.assertEqual(proc.wait(), 0)

self.assertGreaterEqual(dt, self.sleep_time)
self.check_sigwait(wait_func)

@unittest.skipUnless(hasattr(signal, 'sigtimedwait'),
'need signal.sigwaitinfo()')
def test_sigtimedwait(self):
def wait_func(signum):
signal.sigtimedwait([signum], 120.0)

self.check_sigwait(wait_func)


@unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()")
Expand Down