From 671b56f91b02e58edb28a759992521b6a12e3c96 Mon Sep 17 00:00:00 2001 From: Claude Code Bot Date: Mon, 23 Nov 2026 15:08:53 -0800 Subject: [PATCH 1/2] fix(nos-runner): restart a slot whose runner waits too long for a job A JIT runner that waits idle for hours goes stale and stops getting jobs. The supervisor now deletes the runner and restarts the slot once it has waited NOS_IDLE_MAX_S (default 3600 s, 0 disables). It checks that GitHub reports the runner idle, deletes before stopping so a busy runner (HTTP 422) keeps its VM, and restarts one slot per pass. Advances #297 --- docs/runbooks/nos-runner.md | 4 + scripts/nos-runner/config.sh | 3 + scripts/nos-runner/supervisor.sh | 23 +++++ scripts/nos-runner/tests/test-supervisor.sh | 100 +++++++++++++++++++- 4 files changed, 129 insertions(+), 1 deletion(-) diff --git a/docs/runbooks/nos-runner.md b/docs/runbooks/nos-runner.md index 7249c85..2e89e19 100644 --- a/docs/runbooks/nos-runner.md +++ b/docs/runbooks/nos-runner.md @@ -114,6 +114,7 @@ The updater LaunchAgent runs every 600 s. It pulls the clone and runs `install.s The updater needs `NTFY_TOPIC` in its plist, which `install.sh` now writes from the env file. The updater cannot reload its own plist, so one hand run of `install.sh` (section 4, step 3) is needed to turn the alert on. Until then the updater logs `NTFY_TOPIC is unset` and sends nothing. +- On a quiet night the supervisor restarts each slot about hourly (`NOS_IDLE_MAX_S`). That is normal. - A pf rule change needs `install-pf.sh` again with sudo. The updater never touches root files. ## 6. Hold, release and full stop @@ -222,6 +223,9 @@ Key lines to look for: - `was still registered after its VM exited; deleted`: normal after a job. GitHub keeps a JIT runner listed for a while, so the supervisor deletes it. A VM exit is not a failed start: a runner that dies before its job is not detected yet. +- `slot N: runner ID idle for S s (limit L s); restarting`: the runner waited longer than `NOS_IDLE_MAX_S` (default 3600, `0` disables) + for a job, so the supervisor deleted it and stops the VM. A fresh VM starts in its place. One slot restarts per pass. + A `delete failed (HTTP 422)` line instead means the runner had just taken a job; the VM keeps running. - `container run failed`: a failed start. Three in a row fail over. - `NOS_RUNNER set to ubuntu-latest`: the supervisor failed over. The reason follows. - `stopping: switching NOS_RUNNER`: the supervisor got SIGTERM. diff --git a/scripts/nos-runner/config.sh b/scripts/nos-runner/config.sh index a601415..5417a23 100755 --- a/scripts/nos-runner/config.sh +++ b/scripts/nos-runner/config.sh @@ -14,6 +14,9 @@ _nos_conf_dir="$(CDPATH='' cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" : "${NOS_INTERVAL:=30}" : "${NOS_CONTAINER_TIMEOUT:=60}" +# Seconds a runner may wait idle for a job before its slot is restarted; 0 disables. A JIT runner idle too long goes stale (dev-env#297). +: "${NOS_IDLE_MAX_S:=3600}" + # Worst-case seconds from SIGTERM to exit. Task 5 sets the plist ExitTimeOut above this. : "${NOS_SHUTDOWN_MAX_S:=1030}" diff --git a/scripts/nos-runner/supervisor.sh b/scripts/nos-runner/supervisor.sh index 76dcf04..d9145fa 100755 --- a/scripts/nos-runner/supervisor.sh +++ b/scripts/nos-runner/supervisor.sh @@ -338,6 +338,28 @@ delete_runner() { log "runner $1: delete failed (HTTP ${API_CODE}); will retry" return 1 } +# Restart one idle-too-long slot per pass. Delete before stop: a runner that took a job meanwhile gets HTTP 422 and keeps its VM. +restart_stale_slots() { + local n id name started age + [[ "${NOS_IDLE_MAX_S}" -gt 0 ]] || return 0 + for ((n = 1; n <= NOS_SLOTS; n += 1)); do + slot_load "${n}" + name="${SL_NAME}" + id="${SL_ID}" + started="${SL_STARTED}" + [[ -n "${name}" && -n "${id}" && "${started}" =~ ^[0-9]+$ ]] || continue + age=$(($(now) - started)) + ((age >= NOS_IDLE_MAX_S)) || continue + runner_idle "${id}" || continue + delete_runner "${id}" || continue + : >"${NOS_STATE_DIR}/dropped-${n}" + log "slot ${n}: runner ${id} idle for ${age} s (limit ${NOS_IDLE_MAX_S} s); restarting" + stop_slot "${n}" || true + return 0 + done + return 0 +} + # deregister_pending: delete runners whose VM is gone (name= empty, id= set). A failed DELETE keeps id= for retry. # After a VM exit, the slot's fail count resets: 2xx and 404 both follow a finished job (see the comment inside). @@ -526,6 +548,7 @@ cmd_once() { return 1 fi deregister_pending + restart_stale_slots running || return 1 if ! slots_ok; then log "self-check failed: a slot failed 3 starts in a row" diff --git a/scripts/nos-runner/tests/test-supervisor.sh b/scripts/nos-runner/tests/test-supervisor.sh index f6b13cd..e3d37eb 100755 --- a/scripts/nos-runner/tests/test-supervisor.sh +++ b/scripts/nos-runner/tests/test-supervisor.sh @@ -137,6 +137,7 @@ case "\${method} \${url}" in if [[ -n "\${STUB_TOKEN_FAIL:-}" ]]; then reply '{"message":"boom"}' 500; else reply '{"token":"ghs_stubtok"}' 201; fi ;; "GET "*/actions/runners/*) + if [[ -n "\${STUB_RUNNER_GET_FAIL:-}" ]]; then reply '{"message":"boom"}' 500; exit 0; fi id="\${url##*/}" if grep -qx "\${id}" "\${S}/busy" 2>/dev/null; then b=true; else b=false; fi reply "{\"id\":\${id},\"busy\":\${b}}" 200 @@ -161,7 +162,9 @@ case "\${method} \${url}" in ;; "POST "*/actions/variables) setvar; reply '' 201 ;; "DELETE "*/actions/runners/*) - if [[ -n "\${STUB_DELETE_FAIL:-}" ]]; then + if [[ -n "\${STUB_DELETE_BUSY:-}" ]]; then + reply '{"message":"Runner \\"x\\" is currently running a job and cannot be deleted."}' 422 + elif [[ -n "\${STUB_DELETE_FAIL:-}" ]]; then reply '{"message":"boom"}' 500 elif grep -qx "\${url##*/}" "\${S}/gone" 2>/dev/null; then reply '{"message":"Not Found"}' 404 @@ -441,6 +444,101 @@ check "sigterm_stops_idle_keeps_busy: runner 101 deleted" grep -qE '^DELETE .*/a check_not "sigterm_stops_idle_keeps_busy: runner 102 kept" grep -qE '^DELETE .*/actions/runners/102 ' "${S}/requests.log" check "sigterm_stops_idle_keeps_busy: slot 2 state kept" grep -qE '^name=mimolette-2-' "${S}/state/slot-2" +# age_slot N: make slot N's VM look 2 hours old. +age_slot() { + local old + old="$(( $(date +%s || true) - 7200 ))" + sed "s/^started=.*/started=${old}/" "${S}/state/slot-$1" >"${S}/state/slot-$1.new" && mv "${S}/state/slot-$1.new" "${S}/state/slot-$1" +} + +echo "-- idle_restart_replaces_old_idle_slot" +reset +sup once +age_slot 1 +NOS_IDLE_MAX_S=3600 sup once +del_at="$(grep -n '^curl DELETE .*/runners/101' "${S}/order.log" | head -n 1 | cut -d: -f1)" +stop_at="$(grep -n '^container stop mimolette-1-' "${S}/order.log" | head -n 1 | cut -d: -f1)" +check "idle_restart_replaces_old_idle_slot: delete before stop" test -n "${del_at}" -a -n "${stop_at}" -a "${del_at:-0}" -lt "${stop_at:-0}" +check "idle_restart_replaces_old_idle_slot: logged" grep -q 'slot 1: runner 101 idle for' "${S}/sup.err" +NOS_IDLE_MAX_S=3600 sup once +check_eq "idle_restart_replaces_old_idle_slot: one new VM" 3 runs + +echo "-- idle_restart_skips_young_slot" +reset +sup once +NOS_IDLE_MAX_S=3600 sup once +check_eq "idle_restart_skips_young_slot: no delete" 0 count '^DELETE .*/actions/runners/101 ' "${S}/requests.log" +check_not "idle_restart_skips_young_slot: no stop" grep -qE '^stop mimolette-1-' "${S}/container.argv" + +echo "-- idle_restart_skips_busy" +reset +sup once +age_slot 1 +echo 101 >"${S}/busy" +NOS_IDLE_MAX_S=3600 sup once +check_eq "idle_restart_skips_busy: no delete" 0 count '^DELETE .*/actions/runners/101 ' "${S}/requests.log" +check_not "idle_restart_skips_busy: no stop" grep -qE '^stop mimolette-1-' "${S}/container.argv" + +echo "-- idle_restart_skips_unknown" +reset +sup once +age_slot 1 +STUB_RUNNER_GET_FAIL=1 NOS_IDLE_MAX_S=3600 sup once +check_eq "idle_restart_skips_unknown: no delete" 0 count '^DELETE .*/actions/runners/101 ' "${S}/requests.log" +check_not "idle_restart_skips_unknown: no stop" grep -qE '^stop mimolette-1-' "${S}/container.argv" + +echo "-- idle_restart_delete_fails_keeps_vm" +reset +sup once +age_slot 1 +STUB_DELETE_FAIL=1 NOS_IDLE_MAX_S=3600 sup once +check_not "idle_restart_delete_fails_keeps_vm: no stop" grep -qE '^stop mimolette-1-' "${S}/container.argv" +check "idle_restart_delete_fails_keeps_vm: slot keeps VM" grep -qE '^name=mimolette-1-' "${S}/state/slot-1" + +echo "-- idle_restart_delete_422_keeps_vm" +reset +sup once +age_slot 1 +STUB_DELETE_BUSY=1 NOS_IDLE_MAX_S=3600 sup once +check_not "idle_restart_delete_422_keeps_vm: no stop" grep -qE '^stop mimolette-1-' "${S}/container.argv" +check_not "idle_restart_delete_422_keeps_vm: no dropped marker" test -e "${S}/state/dropped-1" +check "idle_restart_delete_422_keeps_vm: slot keeps VM" grep -qE '^name=mimolette-1-' "${S}/state/slot-1" +check "idle_restart_delete_422_keeps_vm: slot keeps id" grep -qx 'id=101' "${S}/state/slot-1" +check "idle_restart_delete_422_keeps_vm: logs HTTP 422" grep -q 'HTTP 422' "${S}/sup.err" + +echo "-- idle_restart_stop_fails_marks_dropped" +reset +sup once +age_slot 1 +STUB_STOP_FAIL=1 NOS_IDLE_MAX_S=3600 sup once +check "idle_restart_stop_fails_marks_dropped: marker" test -e "${S}/state/dropped-1" +check_eq "idle_restart_stop_fails_marks_dropped: nothing started" 2 runs + +echo "-- idle_restart_not_a_fail" +reset +sup once +age_slot 1 +NOS_IDLE_MAX_S=3600 sup once +check "idle_restart_not_a_fail: fails=0" grep -qx 'fails=0' "${S}/state/slot-1" + +echo "-- idle_restart_disabled_at_zero" +reset +sup once +age_slot 1 +NOS_IDLE_MAX_S=0 sup once +check_eq "idle_restart_disabled_at_zero: no delete" 0 count '^DELETE .*/actions/runners/101 ' "${S}/requests.log" + +echo "-- idle_restart_one_slot_per_pass" +reset +sup once +age_slot 1 +age_slot 2 +NOS_IDLE_MAX_S=3600 sup once +check_eq "idle_restart_one_slot_per_pass: one delete this pass" 1 count '^DELETE .*/actions/runners/10[12] ' "${S}/requests.log" +check_eq "idle_restart_one_slot_per_pass: other slot untouched this pass" 0 count '^DELETE .*/actions/runners/102 ' "${S}/requests.log" +NOS_IDLE_MAX_S=3600 sup once +check_eq "idle_restart_one_slot_per_pass: other slot restarted next pass" 1 count '^DELETE .*/actions/runners/102 ' "${S}/requests.log" + echo "-- sigterm_mid_pass_starts_no_vm" reset bg_run STUB_LS_SLEEP=2 From eeefc6ecde38ffbcb9fbc9a49fd65fc324b06022 Mon Sep 17 00:00:00 2001 From: Claude Code Bot Date: Mon, 23 Nov 2026 15:25:22 -0800 Subject: [PATCH 2/2] fix(nos-runner): refresh the VM list after an idle restart After a good idle restart the cached container ls still listed the stopped VM, so an empty slot in the same pass saw too many VMs and waited a pass. Refresh the list as reconcile does. Also record the remaining busy-assignment race in the comment. Advances #297 --- scripts/nos-runner/supervisor.sh | 3 ++- scripts/nos-runner/tests/test-supervisor.sh | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/scripts/nos-runner/supervisor.sh b/scripts/nos-runner/supervisor.sh index d9145fa..b2937bb 100755 --- a/scripts/nos-runner/supervisor.sh +++ b/scripts/nos-runner/supervisor.sh @@ -339,6 +339,7 @@ delete_runner() { return 1 } # Restart one idle-too-long slot per pass. Delete before stop: a runner that took a job meanwhile gets HTTP 422 and keeps its VM. +# Remaining race: a job assigned before GitHub reports busy=true is lost (accepted 2026-11-18). restart_stale_slots() { local n id name started age [[ "${NOS_IDLE_MAX_S}" -gt 0 ]] || return 0 @@ -354,7 +355,7 @@ restart_stale_slots() { delete_runner "${id}" || continue : >"${NOS_STATE_DIR}/dropped-${n}" log "slot ${n}: runner ${id} idle for ${age} s (limit ${NOS_IDLE_MAX_S} s); restarting" - stop_slot "${n}" || true + if stop_slot "${n}"; then refresh_ls; fi return 0 done return 0 diff --git a/scripts/nos-runner/tests/test-supervisor.sh b/scripts/nos-runner/tests/test-supervisor.sh index e3d37eb..41df605 100755 --- a/scripts/nos-runner/tests/test-supervisor.sh +++ b/scripts/nos-runner/tests/test-supervisor.sh @@ -528,6 +528,15 @@ age_slot 1 NOS_IDLE_MAX_S=0 sup once check_eq "idle_restart_disabled_at_zero: no delete" 0 count '^DELETE .*/actions/runners/101 ' "${S}/requests.log" +echo "-- idle_restart_refreshes_vm_list" +reset +sup once +age_slot 1 +# Slot 2 is empty but a stray mimolette VM still shows in the pass's first ls: a stale list blocks the start (2 listed). +printf 'name=\nid=\nfails=0\nstarted=0\n' >"${S}/state/slot-2" +NOS_IDLE_MAX_S=3600 sup once +check_eq "idle_restart_refreshes_vm_list: empty slot 2 starts in the same pass" 1 count '^mimolette-2-' "${S}/running" + echo "-- idle_restart_one_slot_per_pass" reset sup once