Skip to content

bpo-40140: test_builtin.PtyTests registers SIGHUP handler #19314

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 1 commit into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
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
23 changes: 18 additions & 5 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1837,7 +1837,21 @@ class PtyTests(unittest.TestCase):
"""Tests that use a pseudo terminal to guarantee stdin and stdout are
terminals in the test environment"""

@staticmethod
def handle_sighup(signum, frame):
# bpo-40140: if the process is the session leader, os.close(fd)
# of "pid, fd = pty.fork()" can raise SIGHUP signal:
# just ignore the signal.
pass

def run_child(self, child, terminal_input):
old_sighup = signal.signal(signal.SIGHUP, self.handle_sighup)
try:
return self._run_child(child, terminal_input)
finally:
signal.signal(signal.SIGHUP, old_sighup)

def _run_child(self, child, terminal_input):
r, w = os.pipe() # Pipe test results from child back to parent
try:
pid, fd = pty.fork()
Expand Down Expand Up @@ -1893,13 +1907,12 @@ def run_child(self, child, terminal_input):
self.fail("got %d lines in pipe but expected 2, child output was:\n%s"
% (len(lines), child_output))

# Wait until the child process completes before closing the PTY to
# prevent sending SIGHUP to the child process.
support.wait_process(pid, exitcode=0)

# Close the PTY
# bpo-40155: Close the PTY before waiting for the child process
# completion, otherwise the child process hangs on AIX.
os.close(fd)

support.wait_process(pid, exitcode=0)

return lines

def check_input_tty(self, prompt, terminal_input, stdio_encoding=None):
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_pty.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def setUp(self):
self.addCleanup(signal.signal, signal.SIGALRM, old_alarm)

old_sighup = signal.signal(signal.SIGHUP, self.handle_sighup)
self.addCleanup(signal.signal, signal.SIGHUP, old_alarm)
self.addCleanup(signal.signal, signal.SIGHUP, old_sighup)

# isatty() and close() can hang on some platforms. Set an alarm
# before running the test to make sure we don't hang forever.
Expand All @@ -81,8 +81,8 @@ def handle_sig(self, sig, frame):
self.fail("isatty hung")

@staticmethod
def handle_sighup(sig, frame):
# if the process is the session leader, os.close(master_fd)
def handle_sighup(signum, frame):
# bpo-38547: if the process is the session leader, os.close(master_fd)
# of "master_fd, slave_name = pty.master_open()" raises SIGHUP
# signal: just ignore the signal.
pass
Expand Down