Skip to content

The agent that grows with you

fix(bot-desktop): a self-stop no longer waits out the kill grace

kshitijk4poor653603aac5c5
diff --git a/tests/tools/test_bot_desktop_idle_self_stop.py b/tests/tools/test_bot_desktop_idle_self_stop.py
index fe30ee030e3..cc98ec62364 100644
--- a/tests/tools/test_bot_desktop_idle_self_stop.py
+++ b/tests/tools/test_bot_desktop_idle_self_stop.py
@@ -92,6 +92,19 @@ def test_an_abandoned_screen_stops_itself_after_the_idle_window(abandoned_screen
timeout=30, what="the abandoned screen to be stopped the way stop() does")
+def test_a_self_stop_does_not_sit_out_the_kill_grace(abandoned_screen):
+ """The check's stop() TERMs the launcher's group, which holds the loop that ran the check. A loop blocked
+ in ``$(check)`` defers its TERM trap until the check returns, while the check waits for the group to
+ empty: every self-stop sat out stop()'s 5 s grace and ended in SIGKILL, with start.lock held throughout."""
+ sd, launcher = abandoned_screen(idle_stop_minutes=0.02)
+ log = sd / "launcher.log"
+ wait_until(lambda: "stopping" in log.read_text(encoding="utf-8", errors="replace"), timeout=30,
+ what="the idle check to start stopping the screen")
+ began = time.monotonic()
+ wait_until(lambda: not _alive(launcher), timeout=30, what="the screen's process group to be gone")
+ assert time.monotonic() - began < 3, "the self-stop waited for stop()'s SIGKILL grace"
+
+
def test_a_screen_in_use_is_kept_and_a_returning_gateway_reattaches(abandoned_screen, monkeypatch):
from hermes_constants import reset_hermes_home_override, set_hermes_home_override
from tools.bot_desktop import runtime
diff --git a/tools/bot_desktop/launcher.sh b/tools/bot_desktop/launcher.sh
index 94203ae7bba..64c7be3cc38 100755
--- a/tools/bot_desktop/launcher.sh
+++ b/tools/bot_desktop/launcher.sh
@@ -285,16 +285,24 @@ if [[ -n "${HERMES_BD_PYTHON:-}" ]]; then
# A start is use: stamp it now, or an activity file left by an earlier screen could make the first check
# stop this one before runtime.py stamps it.
touch "$(dirname "$HERMES_BD_ENV_FILE")/activity"
- # The TERM trap ends the sleep at once when the screen goes down (stop()'s group signal or the EXIT trap
- # below); during the ~0.4 s check itself bash defers it, well inside stop()'s grace.
+ # The TERM trap ends this loop at once when the screen goes down (stop()'s group signal or the EXIT trap
+ # below). Both the check and the sleep run in the background and are waited on, because `wait` is
+ # interruptible: a `$(check)` would defer the trap until the check returns, while a check that is itself
+ # stopping the screen waits for this group to empty, so every self-stop would sit out stop()'s grace and
+ # end in SIGKILL with start.lock held. The check is in a session of its own and finishes the stop alone.
(
- trap 'kill "$!" 2>/dev/null; exit 0' TERM
+ sleeper=
+ trap 'kill $sleeper 2>/dev/null; exit 0' TERM
+ out="$(dirname "$HERMES_BD_ENV_FILE")/idle-watch.out"
while kill -0 "$$" 2>/dev/null; do
- delay=$(HERMES_HOME="$HERMES_BD_HOME" PYTHONPATH="$HERMES_BD_SRC_ROOT${PYTHONPATH:+:$PYTHONPATH}" \
- "$HERMES_BD_PYTHON" -m tools.bot_desktop.idle_watch "$$") || delay=300 # broken check: retry slowly
+ HERMES_HOME="$HERMES_BD_HOME" PYTHONPATH="$HERMES_BD_SRC_ROOT${PYTHONPATH:+:$PYTHONPATH}" \
+ "$HERMES_BD_PYTHON" -m tools.bot_desktop.idle_watch "$$" >"$out" &
+ if wait "$!"; then delay=$(<"$out"); else delay=300; fi # broken check: retry slowly
[[ -n "$delay" ]] || exit 0 # it stopped the screen
[[ "$delay" =~ ^[0-9]+$ ]] || delay=300
- sleep "$delay" & wait "$!"
+ sleep "$delay" & sleeper=$!
+ wait "$sleeper"
+ sleeper=
done
) &
WATCH_PID=$!