From 49689e47229ea8cc72ad0982405fc47d92daafac Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:35:58 -0500 Subject: [PATCH] test(handoff): time held exits from the run, and only once the program is at its exit --- tools/tests/test_native_hook_handoff.py | 43 ++++++++++++++++++------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/tools/tests/test_native_hook_handoff.py b/tools/tests/test_native_hook_handoff.py index 8dd215bd06..99ec5ff635 100644 --- a/tools/tests/test_native_hook_handoff.py +++ b/tools/tests/test_native_hook_handoff.py @@ -2,6 +2,7 @@ import errno import fcntl import json import os +import select import shutil import signal import socket @@ -2048,23 +2049,39 @@ class NativeHookHandoffTest(unittest.TestCase): worker = "-E DDH_WORKER_PID=$$" if self.prefix else "DDH_WORKER_PID=$$" runner = " ".join(self.prefix) + " " + settings + " " + worker if self.prefix else "env " + settings + " " + worker run = subprocess.Popen(["sh", "-c", "exec %s %s" % (runner, probe)], pass_fds=(table,), - stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, text=True) + stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) + run.started = time.monotonic() # after the build: only the run is timed + run.seen, run.rest = {}, b"" self.addCleanup(run.wait) self.addCleanup(lambda: run.poll() is None and run.kill()) return run, table @staticmethod - def printed(run): - return dict(line.split("\t", 1) for line in run.stdout.read().splitlines() if "\t" in line) + def printed(run, until=(), deadline=60): + # What the program printed, as it comes: it flushes once, at the end of main(), so lines in mean + # it is at its exit. Reads until the keys in until are in (or end of output, or the deadline). + fd = run.stdout.fileno() + end = time.monotonic() + deadline + while not all(key in run.seen for key in until) and time.monotonic() < end: + if not select.select([fd], [], [], 0.1)[0]: + continue + data = os.read(fd, 4096) + if not data: + break + *lines, run.rest = (run.rest + data).split(b"\n") + for line in lines: + if b"\t" in line: + key, value = line.decode().split("\t", 1) + run.seen[key] = value + return run.seen def test_with_no_free_fd_a_pidfd_ends_a_held_exit_once_its_holder_has_exited(self): forker = subprocess.Popen(["sleep", "1"]) self.addCleanup(forker.wait) # a zombie once it exits: kill() alone would call it alive - start = time.monotonic() run, table = self.start_worker_exit("takenpidfd", forker.pid, 4, nofds=True) self.assertEqual(9, run.wait(timeout=60)) # its own exit, not a signal - took = time.monotonic() - start - result = self.printed(run) + took = time.monotonic() - run.started + result = self.printed(run, ("pidfd", "nofds")) self.assertEqual("1", result.get("pidfd"), result) self.assertEqual("1", result.get("nofds"), result) # well inside the bounded wait (5000 looks) that kill() alone would have taken @@ -2076,12 +2093,12 @@ class NativeHookHandoffTest(unittest.TestCase): self.addCleanup(forker.wait) self.addCleanup(forker.kill) run, table = self.start_worker_exit("takenbounded", forker.pid, 4, nofds=True, child_start=0) + result = self.printed(run, ("pidfd", "nofds")) # at its exit before the wait is timed + self.assertEqual("0", result.get("pidfd"), result) + self.assertEqual("1", result.get("nofds"), result) with self.assertRaises(subprocess.TimeoutExpired): run.wait(timeout=2) # the holder lives, as kill() tells self.assertEqual(9, run.wait(timeout=90)) # then the bound: it gives up waiting - result = self.printed(run) - self.assertEqual("0", result.get("pidfd"), result) - self.assertEqual("1", result.get("nofds"), result) state, status = struct.unpack("