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 9ed6fd5d09ee6606525049138775b6c36449badd
Author: Cedric BAIL <[email protected]>
AuthorDate: Sun Nov 8 22:45:53 2026 -0600

    tybenchd: kill the daemon when a run fails
    
    When a run failed once connected to the daemon it had started (no
    ATTACHED answer, a failed write, the connection dropping, or no message
    for 60 s), tybenchd exited and left that tymuxd and its shell running,
    one more for every failed invocation. Such a run now ends the session
    the way tymux kill does.
    
    A failure to connect after the daemon started is not covered: there is
    no connection to find the daemon's pid from.
    
    tests/tybenchd_kill.py runs tybenchd against a stand-in daemon that
    hangs up on the attach, and checks the stand-in is gone afterwards. It
    is a test of the benchmarks build, as tybenchd is only built with it.
---
 src/bin/tybenchd.c     |  2 ++
 tests/meson.build      |  6 ++++
 tests/tybenchd_kill.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 85 insertions(+)

diff --git a/src/bin/tybenchd.c b/src/bin/tybenchd.c
index b084ea27..21e2e19e 100644
--- a/src/bin/tybenchd.c
+++ b/src/bin/tybenchd.c
@@ -136,6 +136,8 @@ _run(const char *name, const char *cmd, Run *run)
    ret = 0;
 
 end:
+   if (ret < 0)
+     termsrv_daemon_kill(sock);
    termsrv_reader_clear(&watch.reader);
    close(sock);
    return ret;
diff --git a/tests/meson.build b/tests/meson.build
index 2ae17348..e96dc755 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -149,6 +149,12 @@ if benchmarks
             depends: bench_corpus,
             timeout: 600)
   if host_os == 'linux'
+    # tybenchd is only built with the benchmarks, so this check lives here.
+    test('tybenchd-kill',
+         python3,
+         args: [files('tybenchd_kill.py'), tybenchd.full_path()],
+         depends: tybenchd,
+         timeout: 120)
     benchmark('tymuxd-throughput',
               tybenchd,
               args: ['-i', '5', '1000000'],
diff --git a/tests/tybenchd_kill.py b/tests/tybenchd_kill.py
new file mode 100644
index 00000000..1d480bb6
--- /dev/null
+++ b/tests/tybenchd_kill.py
@@ -0,0 +1,77 @@
+#!/usr/bin/env python3
+"""Check that tybenchd ends the daemon it started when a run fails.
+
+Usage: tybenchd_kill.py PATH_TO_TYBENCHD
+
+tybenchd starts the tymuxd found next to it. Here that is a stand-in which
+listens on the socket and hangs up on whoever connects, so the attach fails.
+The stand-in must be gone once tybenchd has reported the failure. It ends by
+itself after 30 s, so a test that did not get to clean up leaves nothing
+behind.
+"""
+import os
+import shutil
+import subprocess
+import sys
+import tempfile
+import time
+
+TYBENCHD = os.path.abspath(sys.argv[1])
+FAKE_DAEMON = """#!%s
+import os, signal, socket, sys
+# never outlives the test, whatever happens to it
+signal.alarm(30)
+path = os.path.join(os.environ["XDG_RUNTIME_DIR"], "terminology", "tymux",
+                    sys.argv[1] + ".sock")
+srv = socket.socket(socket.AF_UNIX)
+srv.bind(path)
+srv.listen(8)
+with open(os.environ["PIDFILE"], "w") as f:
+    f.write(str(os.getpid()))
+while True:
+    conn, _ = srv.accept()
+    conn.recv(64)
+    conn.close()
+"""
+
+
+def alive(pid):
+    try:
+        with open("/proc/%d/stat" % pid) as f:
+            return f.read().rsplit(")", 1)[1].split()[0] != "Z"
+    except OSError:
+        return False
+
+
+def main():
+    tmp = tempfile.mkdtemp(prefix="tybenchd-kill-")
+    pid = None
+    try:
+        shutil.copy(TYBENCHD, os.path.join(tmp, "tybenchd"))
+        fake = os.path.join(tmp, "tymuxd")
+        with open(fake, "w") as f:
+            f.write(FAKE_DAEMON % sys.executable)
+        os.chmod(fake, 0o755)
+        pidfile = os.path.join(tmp, "pid")
+        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 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)
+        print("FAIL: the daemon is still running after the failed run")
+        return 1
+    finally:
+        if pid and alive(pid):
+            os.kill(pid, 9)
+        shutil.rmtree(tmp, ignore_errors=True)
+
+
+sys.exit(main())

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

Reply via email to