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 19ed8313db9d280d7df587b8c6cb2f47e9baa9e3
Author: Cedric BAIL <[email protected]>
AuthorDate: Fri Nov 20 05:51:13 2026 -0600

    tybenchd: delete its fd handler once
    
    When reading from tymuxd failed, the fd handler returned
    ECORE_CALLBACK_CANCEL, so ecore deleted and freed the handler, and the
    benchmark then deleted it again once the loop was over: a use after
    free on the error path, which ecore reports as a magic check failure.
    
    Keep the handler on error, as on every other path: the loop is quitting
    anyway, and the handler is deleted after it.
    
    tests/tybenchd_reset.py runs tybenchd against a stand-in daemon that
    answers the attach, then closes the socket with the command still
    unread, which resets the connection. It saw the magic check failure 3
    times out of 3 before this change and passes now.
---
 src/bin/tybenchd.c      |   3 +-
 tests/meson.build       |   5 +++
 tests/tybenchd_reset.py | 107 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 114 insertions(+), 1 deletion(-)

diff --git a/src/bin/tybenchd.c b/src/bin/tybenchd.c
index 21e2e19e..4a77989d 100644
--- a/src/bin/tybenchd.c
+++ b/src/bin/tybenchd.c
@@ -71,10 +71,11 @@ _cb_fd(void *data, Ecore_Fd_Handler *fdh __attribute__((unused)))
    int type = 0;
 
    ecore_timer_reset(watch->timeout);
+   /* _run() deletes the handler once the loop is done */
    if (termsrv_reader_fill(&watch->reader, watch->sock) < 0)
      {
         ecore_main_loop_quit();
-        return ECORE_CALLBACK_CANCEL;
+        return ECORE_CALLBACK_RENEW;
      }
    while ((type = termsrv_reader_next(&watch->reader, &payload, &size)) > 0)
      {
diff --git a/tests/meson.build b/tests/meson.build
index e96dc755..275e628f 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -155,6 +155,11 @@ if benchmarks
          args: [files('tybenchd_kill.py'), tybenchd.full_path()],
          depends: tybenchd,
          timeout: 120)
+    test('tybenchd-reset',
+         python3,
+         args: [files('tybenchd_reset.py'), tybenchd.full_path()],
+         depends: tybenchd,
+         timeout: 120)
     benchmark('tymuxd-throughput',
               tybenchd,
               args: ['-i', '5', '1000000'],
diff --git a/tests/tybenchd_reset.py b/tests/tybenchd_reset.py
new file mode 100644
index 00000000..3421c4c7
--- /dev/null
+++ b/tests/tybenchd_reset.py
@@ -0,0 +1,107 @@
+#!/usr/bin/env python3
+"""Check that tybenchd survives a daemon that resets the connection.
+
+Usage: tybenchd_reset.py PATH_TO_TYBENCHD
+
+tybenchd starts the tymuxd found next to it. Here that is a stand-in which
+answers the attach, then closes the socket without reading the command:
+closing with unread data resets the connection, so tybenchd's read fails
+instead of seeing an end of file. tybenchd must report the failure without
+ecore complaining about its fd handler. The stand-in notes the type of the
+message that follows the attach: the test checks it was the input, so that
+a stand-in the client no longer understands cannot make the test pass
+without ever reaching the fd handler. The stand-in ends by itself after
+30 s.
+"""
+import os
+import shutil
+import subprocess
+import sys
+import tempfile
+
+TYBENCHD = os.path.abspath(sys.argv[1])
+TSRV_MSG_INPUT = 4
+FAKE_DAEMON = """#!%s
+import os, select, signal, socket, struct, sys, time
+# 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()))
+TSRV_MSG_ATTACHED = 2
+attached = struct.pack("=IIi", 80, 24, -1)
+while True:
+    conn, _ = srv.accept()
+    # tybenchd checks whether a daemon runs before it attaches
+    if not conn.recv(64):
+        conn.close()
+        continue
+    shm = os.memfd_create("screen")
+    socket.send_fds(conn, [struct.pack("=II", TSRV_MSG_ATTACHED,
+                                       len(attached)) + attached], [shm])
+    os.close(shm)
+    # wait for the command, note what it is, and leave it unread
+    select.select([conn], [], [], 5)
+    header = conn.recv(8, socket.MSG_PEEK)
+    with open(os.environ["COMMANDFILE"], "w") as f:
+        f.write(str(struct.unpack("=II", header)[0]) if len(header) == 8
+                else "none")
+    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-reset-")
+    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")
+        commandfile = os.path.join(tmp, "command")
+        env = dict(os.environ, XDG_RUNTIME_DIR=tmp, PIDFILE=pidfile,
+                   COMMANDFILE=commandfile)
+        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())
+        if res.returncode == 0:
+            print("FAIL: tybenchd succeeded against a daemon that resets")
+            return 1
+        with open(commandfile) as f:
+            command = f.read()
+        if command != str(TSRV_MSG_INPUT):
+            print("FAIL: tybenchd did not send its input after the attach "
+                  "(message %s):\n%s" % (command, res.stderr))
+            return 1
+        if res.returncode < 0:
+            print("FAIL: tybenchd died of signal %d:\n%s"
+                  % (-res.returncode, res.stderr))
+            return 1
+        if "Magic" in res.stderr or "ERROR: AddressSanitizer" in res.stderr:
+            print("FAIL: tybenchd misused its fd handler:\n" + res.stderr)
+            return 1
+        print("PASS: tybenchd reported the reset and nothing else")
+        return 0
+    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