Skip to content

Commit 745bd91

Browse files
authored
bpo-40140: test_builtin.PtyTests registers SIGHUP handler (GH-19314) (GH-19316)
test_builtin.PtyTests now registers an handler for SIGHUP signal. Closing the PTY file descriptor can emit a SIGHUP signal: just ignore it. run_child() now also closes the PTY file descriptor before waiting for the process completition, otherwise the test hangs on AIX. (cherry picked from commit 7a51a7e)
1 parent 1098671 commit 745bd91

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

Lib/test/test_builtin.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1790,7 +1790,21 @@ class PtyTests(unittest.TestCase):
17901790
"""Tests that use a pseudo terminal to guarantee stdin and stdout are
17911791
terminals in the test environment"""
17921792

1793+
@staticmethod
1794+
def handle_sighup(signum, frame):
1795+
# bpo-40140: if the process is the session leader, os.close(fd)
1796+
# of "pid, fd = pty.fork()" can raise SIGHUP signal:
1797+
# just ignore the signal.
1798+
pass
1799+
17931800
def run_child(self, child, terminal_input):
1801+
old_sighup = signal.signal(signal.SIGHUP, self.handle_sighup)
1802+
try:
1803+
return self._run_child(child, terminal_input)
1804+
finally:
1805+
signal.signal(signal.SIGHUP, old_sighup)
1806+
1807+
def _run_child(self, child, terminal_input):
17941808
r, w = os.pipe() # Pipe test results from child back to parent
17951809
try:
17961810
pid, fd = pty.fork()
@@ -1841,6 +1855,9 @@ def run_child(self, child, terminal_input):
18411855
child_output = child_output.decode("ascii", "ignore")
18421856
self.fail("got %d lines in pipe but expected 2, child output was:\n%s"
18431857
% (len(lines), child_output))
1858+
1859+
# bpo-40155: Close the PTY before waiting for the child process
1860+
# completion, otherwise the child process hangs on AIX.
18441861
os.close(fd)
18451862

18461863
# Wait until the child process completes

Lib/test/test_pty.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def setUp(self):
7070
self.addCleanup(signal.signal, signal.SIGALRM, old_alarm)
7171

7272
old_sighup = signal.signal(signal.SIGHUP, self.handle_sighup)
73-
self.addCleanup(signal.signal, signal.SIGHUP, old_alarm)
73+
self.addCleanup(signal.signal, signal.SIGHUP, old_sighup)
7474

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

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

0 commit comments

Comments
 (0)