bpo-31160: Fix race condition in test_os.PtyTests (GH-19263) · python/cpython@16d7567
@@ -1846,6 +1846,7 @@ def run_child(self, child, terminal_input):
18461846os.close(w)
18471847self.skipTest("pty.fork() raised {}".format(e))
18481848raise
1849+18491850if pid == 0:
18501851# Child
18511852try:
@@ -1859,9 +1860,11 @@ def run_child(self, child, terminal_input):
18591860finally:
18601861# We don't want to return to unittest...
18611862os._exit(0)
1863+18621864# Parent
18631865os.close(w)
18641866os.write(fd, terminal_input)
1867+18651868# Get results from the pipe
18661869with open(r, "r") as rpipe:
18671870lines = []
@@ -1871,6 +1874,7 @@ def run_child(self, child, terminal_input):
18711874# The other end was closed => the child exited
18721875break
18731876lines.append(line)
1877+18741878# Check the result was got and corresponds to the user's terminal input
18751879if len(lines) != 2:
18761880# Something went wrong, try to get at stderr
@@ -1888,11 +1892,14 @@ def run_child(self, child, terminal_input):
18881892child_output = child_output.decode("ascii", "ignore")
18891893self.fail("got %d lines in pipe but expected 2, child output was:\n%s"
18901894% (len(lines), child_output))
1891-os.close(fd)
189218951893-# Wait until the child process completes
1896+# Wait until the child process completes before closing the PTY to
1897+# prevent sending SIGHUP to the child process.
18941898support.wait_process(pid, exitcode=0)
189518991900+# Close the PTY
1901+os.close(fd)
1902+18961903return lines
1897190418981905def check_input_tty(self, prompt, terminal_input, stdio_encoding=None):