diff --git a/CHANGELOG.md b/CHANGELOG.md index 954b8ca91..e72efe6ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ Releases of TandemEngine, TandemLLM's inference engine (TandemLLM up to 0.15.4; - A page of the KV pool is never unmapped while the disk tier's writer copies from it (SRV-885). The writer copies a finished prompt's rows after its request ends, under the capture guard; the next request could release the draft window's pages under that copy, an illegal memory access. Every unmap now holds the same guard. - The 27B's disk caches are rebuilt once: their fingerprint moves with the draft window's fix (SRV-885). An entry written before could hold a new chat's draft rows from the window's shared page, marked as real. - The 27B with several lanes no longer stops answering when a prompt that continues another lane's conversation goes to a lane that has not served a request yet (#174). A lane's warm-up left a draft sync waiting and then gave its pages back to the KV pool. The first request there read the draft cache before the drafter's reset, the waiting sync wrote its rows onto pages that were no longer mapped, the GPU faulted (an illegal memory access) and every request after it was a 503 `engine_failed` until a restart. A draft sync whose pages were given back is now dropped, with one log line. The 27B's fingerprint moves with it; the stored rows are the same. +- ops/engine-exec.sh joins the tree: the reference ExecStart for a unit that holds the engine in its cgroup, and it holds on death rather than on silence. A supervisor that leaves on the first `curl -m 10 /health` timeout treats a stalled engine as a dead one: a long disk-tier save or a GPU sync holds every Python thread past the timeout while the kernel still accepts connections, so a keep-alive client finishes its request while the fresh probe hears nothing. Leaving took a serving engine down with its unit four times in a morning. The hold needs `QSE_HOLD_FAILS` (6) missed probes in a row AND the pid dead AND nothing listening on the port before it lets the unit go; a dead engine fails all three at once, a busy one fails none of them for long. install.sh does not copy it: a box's unit is the box's own file. tests/test_engine_exec_hold.py drives a stub engine through the cases. ## 0.16.0, 2026-11-18 diff --git a/install.sh b/install.sh index 70871ad4e..128493d3b 100755 --- a/install.sh +++ b/install.sh @@ -1181,7 +1181,9 @@ else # and what the setup guide's apply job runs beside them (server/setup/apply.py); roles.sh is # what start.sh runs after /health, watchdog.sh the cron line of docs/operations.md, hold.sh # a box-alone command (OPS-161). No chatd launcher: chatd is TandemApp's, run by its - # TandemNode (ENG-413 WP7). Not copied: gate.sh is the release gate of a development checkout. + # TandemNode (ENG-413 WP7). Not copied: gate.sh is the release gate of a development + # checkout, and engine-exec.sh is a unit supervisor's reference -- a box's unit is its + # own file, like ops/qwen38-engine.service. # tests/test_install_ops_copy.py keeps this list and their "$HERE/" calls in step. for s in start.sh stop.sh engines.sh use-profile.sh make-secrets.sh engine_status.py \ fetch_weights.py roles.sh watchdog.sh hold.sh; do install -m 755 "$SRC/ops/$s" "$RUN/ops/$s"; done diff --git a/ops/engine-exec.sh b/ops/engine-exec.sh new file mode 100644 index 000000000..00d83ab48 --- /dev/null +++ b/ops/engine-exec.sh @@ -0,0 +1,99 @@ +#!/bin/bash +# A systemd unit's ExecStart for one engine: ops/start.sh backgrounds the engine and returns, so +# this script stays the unit's main process for as long as the engine runs and the unit's cgroup +# owns it. `systemctl stop` then drains the engine cleanly, and nothing can kill it by accident: +# on 2026-11-07 a restart asked mid-boot died with the agent session that asked for it, the +# SIGTERM going down the whole process tree. +# +# A reference, not an installed file: install.sh does not copy it, like ops/gate.sh and +# ops/qwen38-engine.service. A box's unit is the box's own file; copy this beside the rendered +# serve.env as DIR/run/ops/engine-exec.sh, point the unit's ExecStart at it through bash, and +# keep Type=simple with KillMode=control-group. +# +# Hold on death, not on silence (2026-11-21). A `curl -m N /health` loop that leaves on the first +# timeout treats a stalled engine as a dead one: a long disk-tier save or a GPU sync holds every +# Python thread past the timeout while the kernel still accepts connections, so a client on a +# keep-alive finishes its request while the fresh probe hears nothing. Leaving on one miss took +# a serving engine down with the unit four times in a morning. The engine is gone only when +# several probes in a row fail AND its pid is dead AND nothing listens on the port: a dead +# engine fails all three at once, a busy one fails none of them for long. +set -u +HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # DIR/run/ops, beside serve.env +RUN="$(dirname "$HERE")" +cd "$RUN" || exit 1 +set -a; . "$HERE/serve.env"; set +a +PORT="${PORT:-8000}" +REPO="${REPO:-$RUN/../src}" + +# A box that shares the accelerator can put its own guard beside this script as +# ops/engine-slot.sh (claim|release NAME; not part of the release): its exit 75 (EX_TEMPFAIL) +# is passed through, so the unit waits and retries instead of hot-looping. Whatever stops the +# engine later -- a stop, a crash, a reboot -- the slot goes back, so the other engine is never +# locked out by a claim nobody holds. Registered BEFORE start.sh: a start that fails must not +# leak the claim. +if [ -x "$HERE/engine-slot.sh" ]; then + SLOT="${QSE_SLOT_NAME:-tandem-$PORT}" + bash "$HERE/engine-slot.sh" claim "$SLOT" || exit $? + release_slot() { bash "$HERE/engine-slot.sh" release "$SLOT" >/dev/null 2>&1 || true; } + trap release_slot EXIT +fi + +bash "$HERE/start.sh" + +# start.sh writes the pid it spawned into $REPO/logs/engine.pid. +PIDFILE="$REPO/logs/engine.pid" +PID="" +for _ in $(seq 1 120); do + if [ -s "$PIDFILE" ]; then + PID=$(cat "$PIDFILE" 2>/dev/null || echo "") + [ -n "$PID" ] && break + fi + sleep 1 +done + +if [ -z "$PID" ]; then + # no pidfile: find the engine by what it owns -- the port. Never exit 1 here: this script's + # first version did when the pidfile was missing, and KillMode=control-group killed the + # engine it was written to protect. NEVER let this script exit while the engine is alive. + echo "[unit] no pidfile at $PIDFILE; finding the engine by its port" + for _ in $(seq 1 60); do + PID=$(ss -ltnp 2>/dev/null | grep -m1 ":$PORT " | grep -oP 'pid=\K[0-9]+' | head -1) + [ -n "$PID" ] && break + sleep 2 + done +fi + +# The engine is gone only when its pid is dead AND the port has no listener left: the port can +# outlive the pid by a moment, and a self-respawned engine keeps the port under a new pid, so +# neither check alone is proof. +engine_gone() { + if [ -n "${PID:-}" ] && kill -0 "$PID" 2>/dev/null; then return 1; fi + if ss -ltn 2>/dev/null | grep -q ":$PORT "; then return 1; fi + return 0 +} + +hold_until_gone() { + local fails=0 + while :; do + if curl -sf -m 10 "http://127.0.0.1:$PORT/health" >/dev/null 2>&1; then + fails=0 + else + fails=$((fails+1)) + if [ "$fails" -ge "${QSE_HOLD_FAILS:-6}" ] && engine_gone; then + echo "[unit] the engine stopped answering" \ + "($fails failed probes; pid ${PID:-?} dead, :$PORT not listening)" + exit 0 + fi + echo "[unit] /health probe failed ($fails); the engine is still up, holding" + fi + sleep "${QSE_HOLD_EVERY_S:-5}" + done +} + +if [ -z "$PID" ]; then + echo "[unit] the engine's pid is unknown; holding the unit on the port alone" +fi +echo "[unit] engine pid ${PID:-unknown}; holding the unit until it exits" + +# the engine may respawn itself; keep following the port's owner while it lives +hold_until_gone diff --git a/tests/test_engine_exec_hold.py b/tests/test_engine_exec_hold.py new file mode 100644 index 000000000..098b3e73f --- /dev/null +++ b/tests/test_engine_exec_hold.py @@ -0,0 +1,257 @@ +"""A unit supervisor must hold on death, not on silence. + +ops/engine-exec.sh stays a Type=simple unit's main process while the engine runs, so the unit's +cgroup owns it. The first version left the unit on the first `curl -m 10 /health` timeout; a +long disk-tier save or a GPU sync stalls every Python thread past the timeout while the kernel +still accepts connections, so a keep-alive client finishes its request while the fresh probe +hears nothing -- and the exit took a serving engine down with the unit (2026-11-21, four +bounces of a live engine in one morning). The hold now needs QSE_HOLD_FAILS misses in a row AND +a dead pid AND no listener on the port before it lets the unit go. + +The engine here is a stub: start.sh writes the pid of a `sleep` into logs/engine.pid, and curl +and ss on a stubbin PATH answer from control files, so the test drives the three signals +(probe, pid, port) one at a time. + +The test matrix: + + 1 the script parses (bash -n) and install.sh does not copy it -- a box's unit is its own + file, like qwen38-engine.service test_the_script_parses, test_install_does_not_copy_it + 2 a silent-but-alive engine is held: misses far past the single-probe line, pid alive and + the port claimed, and the supervisor still runs test_a_silent_engine_is_held + 3 a probe that answers again resets the count: miss 5, recover, miss 5, die -- the exit + line reports 6 misses, not 11 test_a_recovered_probe_resets_the_count + 4 a dead pid alone is not death while the port stays claimed (the engine may respawn + itself; the comment in engine-exec.sh) test_a_dead_pid_with_a_claimed_port_holds + 5 the engine really gone -- dead pid, no listener, misses in a row -- and the unit lets go + within seconds test_a_dead_engine_lets_go + +Run: python tests/test_engine_exec_hold.py (Linux only: bash, and a stub curl/ss on PATH) +""" +from __future__ import annotations + +import os +import re +import shutil +import signal +import socket +import stat +import subprocess +import sys +import tempfile +import time + +ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +SCRIPT = os.path.join(ROOT, "ops", "engine-exec.sh") +LINUX = sys.platform.startswith("linux") + + +def _skip() -> bool: + if not LINUX: + print(" (skipped: Linux only)") + return True + for tool in ("bash", "ss", "curl"): + if not shutil.which(tool): + print(f" (skipped: no {tool})") + return True + return False + + +def _port() -> int: + s = socket.socket() + s.bind(("127.0.0.1", 0)) + p = s.getsockname()[1] + s.close() + return p + + +def _fixture() -> dict: + """A fake install: DIR/run/ops with the script, a stub start.sh and serve.env, DIR/src/logs + for the pidfile, and a stubbin dir whose curl/ss read control files.""" + tmp = tempfile.mkdtemp(prefix="engine-exec-test-") + ops = os.path.join(tmp, "run", "ops") + src = os.path.join(tmp, "src") + stubbin = os.path.join(tmp, "stubbin") + ctl = os.path.join(tmp, "ctl") + for d in (ops, os.path.join(src, "logs"), stubbin, ctl): + os.makedirs(d) + + port = _port() + shutil.copy(SCRIPT, os.path.join(ops, "engine-exec.sh")) + + with open(os.path.join(ops, "serve.env"), "w") as fh: + fh.write(f"PORT={port}\nREPO='{src}'\n") + + # the stub engine: start.sh drops the pid of a `sleep` where the real one drops the server's + with open(os.path.join(ops, "start.sh"), "w") as fh: + fh.write("#!/bin/bash\n" + "mkdir -p \"$REPO/logs\"\n" + "nohup sleep 300 /dev/null 2>&1 &\n" + "echo $! > \"$REPO/logs/engine.pid\"\n" + "echo \"[stub start] engine pid $(cat \"$REPO/logs/engine.pid\")\"\n") + + # curl answers from ctl/curl: 'ok' -> a body and 0; anything else -> a refused exit + with open(os.path.join(stubbin, "curl"), "w") as fh: + fh.write(f"#!/bin/bash\n" + f"M=$(cat '{ctl}/curl' 2>/dev/null || echo ok)\n" + f"[ \"$M\" = ok ] && {{ echo '{{\"status\":\"ok\"}}'; exit 0; }}\n" + f"exit 7\n") + + # ss answers from ctl/ss: 'listen' -> a listener on the port; anything else -> none + with open(os.path.join(stubbin, "ss"), "w") as fh: + fh.write("#!/bin/bash\n" + f"M=$(cat '{ctl}/ss' 2>/dev/null || echo listen)\n" + f"[ \"$M\" = listen ] && echo 'LISTEN 0 511 0.0.0.0:{port} 0.0.0.0:*'\n" + f"exit 0\n") + + for n in ("curl", "ss", "start.sh"): + p = os.path.join(stubbin if n != "start.sh" else ops, n) + os.chmod(p, os.stat(p).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) + + with open(os.path.join(ctl, "curl"), "w") as fh: + fh.write("fail") + with open(os.path.join(ctl, "ss"), "w") as fh: + fh.write("listen") + + env = dict(os.environ) + env["PATH"] = stubbin + os.pathsep + env["PATH"] + env["QSE_HOLD_EVERY_S"] = "0.05" # ~20 misses a second; the assertions stay generous + env["QSE_HOLD_FAILS"] = "6" + return {"tmp": tmp, "ops": ops, "src": src, "ctl": ctl, "env": env, "port": port} + + +def _run(box: dict) -> tuple[subprocess.Popen, str]: + log = os.path.join(box["tmp"], "unit.log") + proc = subprocess.Popen(["bash", os.path.join(box["ops"], "engine-exec.sh")], + env=box["env"], stdout=open(log, "w"), stderr=subprocess.STDOUT) + return proc, log + + +def _ctl(box: dict, name: str, value: str) -> None: + with open(os.path.join(box["ctl"], name), "w") as fh: + fh.write(value) + + +def _out(log: str) -> str: + try: + return open(log).read() + except OSError: + return "" + + +def _wait(cond, timeout: float, what: str) -> None: + end = time.time() + timeout + while time.time() < end: + if cond(): + return + time.sleep(0.05) + raise AssertionError(f"timed out waiting for: {what}") + + +def _engine_pid(box: dict) -> int: + return int(open(os.path.join(box["src"], "logs", "engine.pid")).read()) + + +def _misses(log: str) -> int: + return max([int(m) for m in re.findall(r"probe failed \((\d+)\)", _out(log))] or [0]) + + +def _stop(box: dict, proc: subprocess.Popen) -> None: + try: + os.kill(_engine_pid(box), signal.SIGKILL) + except (ProcessLookupError, OSError): + pass + proc.kill() + proc.wait(timeout=10) + shutil.rmtree(box["tmp"], ignore_errors=True) + + +def test_the_script_parses(): + r = subprocess.run(["bash", "-n", SCRIPT], capture_output=True, text=True) + assert r.returncode == 0, r.stderr + + +def test_install_does_not_copy_it(): + # a box's unit is the box's own file, like ops/qwen38-engine.service: copying this beside a + # rendered serve.env is the deploy step, not the installer's job + text = open(os.path.join(ROOT, "install.sh")).read() + m = re.search(r'for s in ([^;]*); do install -m 755 "\$SRC/ops/\$s"', text) + assert "engine-exec.sh" not in m.group(1).split() + + +def test_a_silent_engine_is_held(): + if _skip(): + return + box = _fixture() + proc, log = _run(box) + try: + # probes fail from the start (a stalled engine) but the pid lives and the port is + # claimed: far past the old single-miss line, the supervisor still runs + _wait(lambda: _misses(log) >= 6, 15, "six failed probes") + assert proc.poll() is None, _out(log) + time.sleep(0.5) + assert proc.poll() is None, _out(log) + assert "stopped answering" not in _out(log), _out(log) + finally: + _stop(box, proc) + + +def test_a_recovered_probe_resets_the_count(): + if _skip(): + return + box = _fixture() + proc, log = _run(box) + try: + _wait(lambda: _misses(log) >= 5, 15, "five failed probes") + _ctl(box, "curl", "ok") # the stall ends; the count must forget it + time.sleep(0.4) + _ctl(box, "curl", "fail") + # a second "probe failed (1)" line exists only if the recovery restarted the count; + # without the reset the second streak would keep numbering past the first's + _wait(lambda: len(re.findall(r"probe failed \(1\)", _out(log))) >= 2, + 15, "the count restarting at 1") + os.kill(_engine_pid(box), signal.SIGKILL) + _ctl(box, "ss", "nolisten") + _wait(lambda: proc.poll() == 0, 15, "the unit letting go") + assert "the engine stopped answering" in _out(log), _out(log) + finally: + _stop(box, proc) + + +def test_a_dead_pid_with_a_claimed_port_holds(): + if _skip(): + return + box = _fixture() + proc, log = _run(box) + try: + _wait(lambda: _misses(log) >= 2, 15, "the hold settling in") + os.kill(_engine_pid(box), signal.SIGKILL) # pid dead, port still claimed (a respawn) + _wait(lambda: _misses(log) >= 10, 15, "ten failed probes") + assert proc.poll() is None, _out(log) + assert "stopped answering" not in _out(log), _out(log) + finally: + _stop(box, proc) + + +def test_a_dead_engine_lets_go(): + if _skip(): + return + box = _fixture() + proc, log = _run(box) + try: + _wait(lambda: _misses(log) >= 2, 15, "the hold settling in") + os.kill(_engine_pid(box), signal.SIGKILL) + _ctl(box, "ss", "nolisten") # dead pid AND no listener: really gone + _wait(lambda: proc.poll() == 0, 15, "the unit letting go") + assert "the engine stopped answering" in _out(log), _out(log) + finally: + _stop(box, proc) + + +if __name__ == "__main__": + passed = 0 + for name, fn in sorted(globals().items()): + if name.startswith("test_") and callable(fn): + fn() + print(f" {name:60s} ok") + passed += 1 + print(f"{passed} passed")