This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch master
in repository terminology.

View the commit online.

commit 08eeb74e3c0b899ceb9cc73930cc7d6523092604
Author: Cedric BAIL <[email protected]>
AuthorDate: Fri Nov 20 13:07:28 2026 -0600

    tests: kill the tybenchd stand-in daemons through a pidfd
    
    The finally block checked that the stand-in daemon was alive before it
    killed it. The daemon can exit between the check and the kill, and the
    ProcessLookupError turned a passing test into a failure now and then.
    
    Open a pidfd right after reading the pidfile and signal that, tolerating
    the ProcessLookupError of a stand-in already gone. The pidfile is read
    once tybenchd returned, when the stand-in may have ended and its pid
    been given to another process: the pidfd pins whichever process has the
    pid once open, so its command line is checked to hold the stand-in's
    path, and any other process is left alone. tybenchd_kill.py also waits
    for the pidfd to become readable instead of polling /proc. A missing
    pidfile is reported as a failure.
---
 tests/tybenchd_kill.py  | 53 +++++++++++++++++++++++++++++++++++--------------
 tests/tybenchd_reset.py | 45 +++++++++++++++++++++++++++++++----------
 2 files changed, 72 insertions(+), 26 deletions(-)

diff --git a/tests/tybenchd_kill.py b/tests/tybenchd_kill.py
index 1d480bb6..c92a87d5 100644
--- a/tests/tybenchd_kill.py
+++ b/tests/tybenchd_kill.py
@@ -10,11 +10,12 @@ itself after 30 s, so a test that did not get to clean up leaves nothing
 behind.
 """
 import os
+import select
 import shutil
+import signal
 import subprocess
 import sys
 import tempfile
-import time
 
 TYBENCHD = os.path.abspath(sys.argv[1])
 FAKE_DAEMON = """#!%s
@@ -35,17 +36,33 @@ while True:
 """
 
 
-def alive(pid):
+def stand_in_open(pidfile, program):
+    """A pidfd of the stand-in, which program is the file of, so that it is
+    the one process this test signals. The pid may have been given to
+    another process since the stand-in ended: the pidfd pins whichever has
+    it once open, whose command line tells them apart. None if there is no
+    pidfile, or no stand-in any more."""
     try:
-        with open("/proc/%d/stat" % pid) as f:
-            return f.read().rsplit(")", 1)[1].split()[0] != "Z"
+        with open(pidfile) as f:
+            pid = int(f.read())
+        pidfd = os.pidfd_open(pid)
+    except (OSError, ValueError):
+        return None
+    try:
+        # empty for a process that ended and was not reaped yet
+        with open("/proc/%d/cmdline" % pid, "rb") as f:
+            cmdline = f.read()
     except OSError:
-        return False
+        cmdline = b""
+    if cmdline and program.encode() not in cmdline:
+        os.close(pidfd)
+        return None
+    return pidfd
 
 
 def main():
     tmp = tempfile.mkdtemp(prefix="tybenchd-kill-")
-    pid = None
+    pidfd = None
     try:
         shutil.copy(TYBENCHD, os.path.join(tmp, "tybenchd"))
         fake = os.path.join(tmp, "tymuxd")
@@ -56,21 +73,27 @@ def main():
         env = dict(os.environ, XDG_RUNTIME_DIR=tmp, PIDFILE=pidfile)
         res = subprocess.run([os.path.join(tmp, "tybenchd"), "-i", "1", "10"],
                              env=env, timeout=60, capture_output=True)
-        with open(pidfile) as f:
-            pid = int(f.read())
+        if not os.path.exists(pidfile):
+            print("FAIL: the stand-in daemon never started")
+            return 1
+        pidfd = stand_in_open(pidfile, fake)
         if res.returncode == 0:
             print("FAIL: tybenchd succeeded against a daemon that hangs up")
             return 1
-        for _ in range(500):
-            if not alive(pid):
-                print("PASS: the daemon is gone after the failed run")
-                return 0
-            time.sleep(0.01)
+        # a pidfd is readable once its process has ended; None means it
+        # was gone before it could be opened
+        if pidfd is None or select.select([pidfd], [], [], 5)[0]:
+            print("PASS: the daemon is gone after the failed run")
+            return 0
         print("FAIL: the daemon is still running after the failed run")
         return 1
     finally:
-        if pid and alive(pid):
-            os.kill(pid, 9)
+        if pidfd is not None:
+            try:
+                signal.pidfd_send_signal(pidfd, signal.SIGKILL)
+            except ProcessLookupError:
+                pass
+            os.close(pidfd)
         shutil.rmtree(tmp, ignore_errors=True)
 
 
diff --git a/tests/tybenchd_reset.py b/tests/tybenchd_reset.py
index 3421c4c7..cd139f24 100644
--- a/tests/tybenchd_reset.py
+++ b/tests/tybenchd_reset.py
@@ -15,6 +15,7 @@ without ever reaching the fd handler. The stand-in ends by itself after
 """
 import os
 import shutil
+import signal
 import subprocess
 import sys
 import tempfile
@@ -54,17 +55,33 @@ while True:
 """
 
 
-def alive(pid):
+def stand_in_open(pidfile, program):
+    """A pidfd of the stand-in, which program is the file of, so that it is
+    the one process this test signals. The pid may have been given to
+    another process since the stand-in ended: the pidfd pins whichever has
+    it once open, whose command line tells them apart. None if there is no
+    pidfile, or no stand-in any more."""
     try:
-        with open("/proc/%d/stat" % pid) as f:
-            return f.read().rsplit(")", 1)[1].split()[0] != "Z"
+        with open(pidfile) as f:
+            pid = int(f.read())
+        pidfd = os.pidfd_open(pid)
+    except (OSError, ValueError):
+        return None
+    try:
+        # empty for a process that ended and was not reaped yet
+        with open("/proc/%d/cmdline" % pid, "rb") as f:
+            cmdline = f.read()
     except OSError:
-        return False
+        cmdline = b""
+    if cmdline and program.encode() not in cmdline:
+        os.close(pidfd)
+        return None
+    return pidfd
 
 
 def main():
     tmp = tempfile.mkdtemp(prefix="tybenchd-reset-")
-    pid = None
+    pidfd = None
     try:
         shutil.copy(TYBENCHD, os.path.join(tmp, "tybenchd"))
         fake = os.path.join(tmp, "tymuxd")
@@ -78,13 +95,15 @@ def main():
         res = subprocess.run([os.path.join(tmp, "tybenchd"), "-i", "1", "10"],
                              env=env, timeout=60, capture_output=True,
                              text=True)
-        with open(pidfile) as f:
-            pid = int(f.read())
+        pidfd = stand_in_open(pidfile, fake)
         if res.returncode == 0:
             print("FAIL: tybenchd succeeded against a daemon that resets")
             return 1
-        with open(commandfile) as f:
-            command = f.read()
+        try:
+            with open(commandfile) as f:
+                command = f.read()
+        except OSError:
+            command = "none"
         if command != str(TSRV_MSG_INPUT):
             print("FAIL: tybenchd did not send its input after the attach "
                   "(message %s):\n%s" % (command, res.stderr))
@@ -99,8 +118,12 @@ def main():
         print("PASS: tybenchd reported the reset and nothing else")
         return 0
     finally:
-        if pid and alive(pid):
-            os.kill(pid, 9)
+        if pidfd is not None:
+            try:
+                signal.pidfd_send_signal(pidfd, signal.SIGKILL)
+            except ProcessLookupError:
+                pass
+            os.close(pidfd)
         shutil.rmtree(tmp, ignore_errors=True)
 
 

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to