From 737e2779042d2d5baca1422f7ccaf70fb1877a63 Mon Sep 17 00:00:00 2001 From: Claude Code Bot Date: Mon, 23 Nov 2026 16:18:40 -0800 Subject: [PATCH 1/2] fix(nos-runner): never restart a slot while another slot is down The idle restart took one slot per pass, but a replacement can need several passes to start. A second stale slot was then restarted while the first had no VM, so both were offline and the watchdog saw a miss. restart_stale_slots now restarts slot N only when every other slot has a VM, a registered runner and no dropped marker. Advances #297 --- scripts/nos-runner/supervisor.sh | 17 +++++++++++- scripts/nos-runner/tests/test-supervisor.sh | 30 ++++++++++++++++++--- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/scripts/nos-runner/supervisor.sh b/scripts/nos-runner/supervisor.sh index b2937bb..ce884d0 100755 --- a/scripts/nos-runner/supervisor.sh +++ b/scripts/nos-runner/supervisor.sh @@ -338,7 +338,20 @@ 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. +# slot_up N: true when slot N has a VM and a registered runner and is not marked dropped. Clobbers SL_*. +slot_up() { + slot_load "$1" + [[ -n "${SL_NAME}" && -n "${SL_ID}" && ! -e "${NOS_STATE_DIR}/dropped-$1" ]] +} +# True when every slot except $1 is up. With NOS_SLOTS=1 there is no other slot, so the single slot may restart. +others_up() { + local m + for ((m = 1; m <= NOS_SLOTS; m += 1)); do + [[ "${m}" -eq "$1" ]] || slot_up "${m}" || return 1 + done + return 0 +} +# Restart one stale slot per pass. Delete before stop: a job taken 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 @@ -351,6 +364,8 @@ restart_stale_slots() { [[ -n "${name}" && -n "${id}" && "${started}" =~ ^[0-9]+$ ]] || continue age=$(($(now) - started)) ((age >= NOS_IDLE_MAX_S)) || continue + others_up "${n}" || return 0 + slot_load "${n}" runner_idle "${id}" || continue delete_runner "${id}" || continue : >"${NOS_STATE_DIR}/dropped-${n}" diff --git a/scripts/nos-runner/tests/test-supervisor.sh b/scripts/nos-runner/tests/test-supervisor.sh index 41df605..a2ff8ac 100755 --- a/scripts/nos-runner/tests/test-supervisor.sh +++ b/scripts/nos-runner/tests/test-supervisor.sh @@ -532,10 +532,11 @@ 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" +# A restart needs every other slot up, so the freed slot cannot start in the same pass; the proof is an ls after the stop. +stop_at="$(grep -n '^container stop mimolette-1-' "${S}/order.log" | head -n 1 | cut -d: -f1)" +ls_after="$(tail -n +"$((${stop_at:-0} + 1))" "${S}/order.log" | grep -c '^container ls' || true)" +check "idle_restart_refreshes_vm_list: ls runs again after the stop" test "${ls_after:-0}" -ge 1 echo "-- idle_restart_one_slot_per_pass" reset @@ -545,8 +546,29 @@ 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" +# Pass B only starts slot 1 again; pass C restarts slot 2. +NOS_IDLE_MAX_S=3600 sup once +NOS_IDLE_MAX_S=3600 sup once +check_eq "idle_restart_one_slot_per_pass: other slot restarted once slot 1 is up" 1 count '^DELETE .*/actions/runners/102 ' "${S}/requests.log" + +echo "-- idle_restart_waits_for_other_slot" +reset +sup once +age_slot 1 +age_slot 2 +NOS_IDLE_MAX_S=3600 sup once +# Pass A restarted slot 1 (runner 101 deleted, VM stopped). Slot 1 has no VM until a later pass starts it. +check_eq "idle_restart_waits_for_other_slot: pass A deletes 101" 1 count '^DELETE .*/actions/runners/101 ' "${S}/requests.log" +for i in 1 2 3 4; do + grep -qE '^name=mimolette-1-' "${S}/state/slot-1" && break + NOS_IDLE_MAX_S=3600 sup once + # Slot 1 had no VM when this pass began, so slot 2 must not be touched by it. + check_eq "idle_restart_waits_for_other_slot: pass ${i}: slot 2 not deleted" 0 count '^DELETE .*/actions/runners/102 ' "${S}/requests.log" + check_not "idle_restart_waits_for_other_slot: pass ${i}: slot 2 not stopped" grep -qE '^stop mimolette-2-' "${S}/container.argv" +done +check "idle_restart_waits_for_other_slot: slot 1 got a new VM" grep -qE '^name=mimolette-1-' "${S}/state/slot-1" 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" +check_eq "idle_restart_waits_for_other_slot: slot 2 restarts once slot 1 is up" 1 count '^DELETE .*/actions/runners/102 ' "${S}/requests.log" echo "-- sigterm_mid_pass_starts_no_vm" reset From 611711cfe8e0db57ddf86336123e855ee667d11c Mon Sep 17 00:00:00 2001 From: Claude Code Bot Date: Mon, 23 Nov 2026 16:28:59 -0800 Subject: [PATCH 2/2] fix(nos-runner): log a deferred idle restart once A restart held back because another slot is down was silent. Log it once per idle runner, tracked in deferred-N, and clear the marker on restart. Also guard the refresh_ls test against an empty stop line. Advances #297 --- scripts/nos-runner/supervisor.sh | 11 ++++++++++- scripts/nos-runner/tests/test-supervisor.sh | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/scripts/nos-runner/supervisor.sh b/scripts/nos-runner/supervisor.sh index ce884d0..3781850 100755 --- a/scripts/nos-runner/supervisor.sh +++ b/scripts/nos-runner/supervisor.sh @@ -364,12 +364,21 @@ restart_stale_slots() { [[ -n "${name}" && -n "${id}" && "${started}" =~ ^[0-9]+$ ]] || continue age=$(($(now) - started)) ((age >= NOS_IDLE_MAX_S)) || continue - others_up "${n}" || return 0 + if ! others_up "${n}"; then + # Log once per idle runner: passes run every ~30 s. + if [[ "$(cat "${NOS_STATE_DIR}/deferred-${n}" 2>/dev/null || true)" != "${id}" ]]; then + echo "${id}" >"${NOS_STATE_DIR}/deferred-${n}" + log "slot ${n}: runner ${id} idle for ${age} s; restart deferred, another slot is down" + fi + return 0 + fi slot_load "${n}" runner_idle "${id}" || continue delete_runner "${id}" || continue + "${RM_BIN}" -f "${NOS_STATE_DIR}/deferred-${n}" : >"${NOS_STATE_DIR}/dropped-${n}" log "slot ${n}: runner ${id} idle for ${age} s (limit ${NOS_IDLE_MAX_S} s); restarting" + # refresh_ls keeps RUNNING accurate for later readers in this pass. if stop_slot "${n}"; then refresh_ls; fi return 0 done diff --git a/scripts/nos-runner/tests/test-supervisor.sh b/scripts/nos-runner/tests/test-supervisor.sh index a2ff8ac..1060d9e 100755 --- a/scripts/nos-runner/tests/test-supervisor.sh +++ b/scripts/nos-runner/tests/test-supervisor.sh @@ -535,9 +535,23 @@ age_slot 1 NOS_IDLE_MAX_S=3600 sup once # A restart needs every other slot up, so the freed slot cannot start in the same pass; the proof is an ls after the stop. stop_at="$(grep -n '^container stop mimolette-1-' "${S}/order.log" | head -n 1 | cut -d: -f1)" +check "idle_restart_refreshes_vm_list: slot 1 stopped" test -n "${stop_at}" ls_after="$(tail -n +"$((${stop_at:-0} + 1))" "${S}/order.log" | grep -c '^container ls' || true)" check "idle_restart_refreshes_vm_list: ls runs again after the stop" test "${ls_after:-0}" -ge 1 +echo "-- idle_restart_deferral_logged_once" +reset +sup once +age_slot 1 +age_slot 2 +NOS_IDLE_MAX_S=3600 sup once +# Hold slot 1 down for two passes by keeping its old runner registered. +for i in 1 2; do + printf 'name=\nid=101\nfails=0\nstarted=0\n' >"${S}/state/slot-1" + NOS_IDLE_MAX_S=3600 sup once +done +check_eq "idle_restart_deferral_logged_once: one deferral line for slot 2" 1 count 'slot 2: .*restart deferred' "${S}/sup.err" + echo "-- idle_restart_one_slot_per_pass" reset sup once