Skip to content

Commit 0961dbd

Browse files
authored
bpo-40140: test_builtin.PtyTests registers SIGHUP handler (GH-19314) (GH-19316) (GH-19318)
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) (cherry picked from commit 745bd91)
1 parent 00c779f commit 0961dbd

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
@@ -1639,7 +1639,21 @@ class PtyTests(unittest.TestCase):
16391639
"""Tests that use a pseudo terminal to guarantee stdin and stdout are
16401640
terminals in the test environment"""
16411641

1642+
@staticmethod
1643+
def handle_sighup(signum, frame):
1644+
# bpo-40140: if the process is the session leader, os.close(fd)
1645+
# of "pid, fd = pty.fork()" can raise SIGHUP signal:
1646+
# just ignore the signal.
1647+
pass
1648+
16421649
def run_child(self, child, terminal_input):
1650+
old_sighup = signal.signal(signal.SIGHUP, self.handle_sighup)
1651+
try:
1652+
return self._run_child(child, terminal_input)
1653+
finally:
1654+
signal.signal(signal.SIGHUP, old_sighup)
1655+
1656+
def _run_child(self, child, terminal_input):
16431657
r, w = os.pipe() # Pipe test results from child back to parent
16441658
try:
16451659
pid, fd = pty.fork()
@@ -1690,6 +1704,9 @@ def run_child(self, child, terminal_input):
16901704
child_output = child_output.decode("ascii", "ignore")
16911705
self.fail("got %d lines in pipe but expected 2, child output was:\n%s"
16921706
% (len(lines), child_output))
1707+
1708+
# bpo-40155: Close the PTY before waiting for the child process
1709+
# completion, otherwise the child process hangs on AIX.
16931710
os.close(fd)
16941711

16951712
# 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)