From e7b0cdece6bb362ec25c31aee62cfc1ca1f3264e Mon Sep 17 00:00:00 2001 From: dawn <90008@klbr.net> Date: Fri, 13 Nov 2026 14:11:17 +0300 Subject: [PATCH] python: support helper waitid and zombie stop on darwin Signed-off-by: dawn <90008@klbr.net> --- priv/python/albedo_helper_lifetime.py | 72 ++++++++++++++++++++++++--- test/harness/helper_lifetime_test.py | 42 ++++++++++++++++ 2 files changed, 107 insertions(+), 7 deletions(-) diff --git a/priv/python/albedo_helper_lifetime.py b/priv/python/albedo_helper_lifetime.py index 66daf5c35..07fdcfc6a 100644 --- a/priv/python/albedo_helper_lifetime.py +++ b/priv/python/albedo_helper_lifetime.py @@ -7,8 +7,11 @@ cleanup so their process group IDs cannot be reused while we signal them. from __future__ import annotations +import ctypes +import errno import os import signal +import struct import subprocess import sys import threading @@ -17,6 +20,63 @@ from typing import IO import albedo_proc +_DARWIN_P_PID = 1 +_DARWIN_WNOHANG = 0x1 +_DARWIN_WEXITED = 0x4 +_DARWIN_WNOWAIT = 0x20 +_DARWIN_SIGINFO_BYTES = 104 +_DARWIN_SI_PID_OFFSET = 12 + + +def _darwin_libc() -> ctypes.CDLL | None: + if sys.platform != "darwin": + return None + libc = ctypes.CDLL(None, use_errno=True) + libc.waitid.argtypes = [ctypes.c_int, ctypes.c_uint, ctypes.c_void_p, ctypes.c_int] + libc.waitid.restype = ctypes.c_int + return libc + + +_DARWIN_LIBC = _darwin_libc() + + +def _exited_unreaped(pid: int) -> bool: + waitid = getattr(os, "waitid", None) + if waitid is not None: + try: + return ( + waitid( + getattr(os, "P_PID"), + pid, + getattr(os, "WEXITED") + | getattr(os, "WNOWAIT") + | getattr(os, "WNOHANG"), + ) + is not None + ) + except ChildProcessError: + return True + if _DARWIN_LIBC is None: + raise RuntimeError("os.waitid is unavailable on this platform") + info = ctypes.create_string_buffer(_DARWIN_SIGINFO_BYTES) + ctypes.set_errno(0) + if ( + _DARWIN_LIBC.waitid( + _DARWIN_P_PID, + pid, + info, + _DARWIN_WEXITED | _DARWIN_WNOWAIT | _DARWIN_WNOHANG, + ) + == -1 + ): + err = ctypes.get_errno() + if err == errno.ECHILD: + return True + if err == errno.EINTR: + return False + raise OSError(err, os.strerror(err)) + return struct.unpack_from("=i", info.raw, _DARWIN_SI_PID_OFFSET)[0] == pid + def read_request() -> bytes: """Read exactly one line without buffering the subsequent control byte.""" @@ -73,13 +133,7 @@ class Commands: while True: # WNOWAIT pins the leader even after it exits, including while # descendants remain in its group. Popen.wait would reap it. - try: - status = os.waitid( - os.P_PID, process.pid, os.WEXITED | os.WNOWAIT | os.WNOHANG - ) - except ChildProcessError: - return - if status is not None: + if _exited_unreaped(process.pid): return if time.monotonic() >= deadline: raise subprocess.TimeoutExpired(process.args, timeout) @@ -113,6 +167,10 @@ def stop(group: int) -> None: os.killpg(group, signal.SIGKILL) except ProcessLookupError: pass + except PermissionError: + # Darwin returns EPERM when only the un-reaped zombie leader remains. + if sys.platform != "darwin" or albedo_proc.live_members(group) != []: + raise def await_stopped(group: int) -> None: diff --git a/test/harness/helper_lifetime_test.py b/test/harness/helper_lifetime_test.py index e4c69179d..594dac4ef 100644 --- a/test/harness/helper_lifetime_test.py +++ b/test/harness/helper_lifetime_test.py @@ -31,6 +31,48 @@ class AwaitStoppedTests(unittest.TestCase): with mock.patch.object(os, "killpg", side_effect=ProcessLookupError): albedo_helper_lifetime.stop(1) + def test_stopping_a_darwin_zombie_only_group_ignores_eperm(self) -> None: + with ( + mock.patch.object(albedo_helper_lifetime.sys, "platform", "darwin"), + mock.patch.object(os, "killpg", side_effect=PermissionError), + mock.patch.object(albedo_proc, "live_members", return_value=[]), + ): + albedo_helper_lifetime.stop(1) + + def test_stopping_a_live_group_that_returns_eperm_raises(self) -> None: + with ( + mock.patch.object(albedo_helper_lifetime.sys, "platform", "darwin"), + mock.patch.object(os, "killpg", side_effect=PermissionError), + mock.patch.object(albedo_proc, "live_members", return_value=[1]), + ): + with self.assertRaises(PermissionError): + albedo_helper_lifetime.stop(1) + + +class WaitTests(unittest.TestCase): + def test_wait_observes_exit_without_reaping_leader(self) -> None: + import subprocess + + with mock.patch.object(albedo_helper_lifetime.threading, "Thread"): + commands = albedo_helper_lifetime.Commands() + with ( + open(os.devnull, "rb") as devnull_in, + open(os.devnull, "wb") as devnull_out, + ): + proc = commands.start( + "exit 7", + cwd=None, + stdin=devnull_in, + stdout=devnull_out, + stderr=subprocess.STDOUT, + ) + try: + commands.wait(proc, 5.0) + self.assertIsNone(proc.returncode) + finally: + commands.finish(proc) + self.assertEqual(proc.returncode, 7) + if __name__ == "__main__": unittest.main() -- knotmirror 0.0.1