Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion scripts/nos-runner/supervisor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -351,10 +364,21 @@ restart_stale_slots() {
[[ -n "${name}" && -n "${id}" && "${started}" =~ ^[0-9]+$ ]] || continue
age=$(($(now) - started))
((age >= NOS_IDLE_MAX_S)) || continue
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
Expand Down
44 changes: 40 additions & 4 deletions scripts/nos-runner/tests/test-supervisor.sh
Original file line number Diff line number Diff line change
Expand Up @@ -532,10 +532,25 @@ 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)"
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
Expand All @@ -545,8 +560,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
Expand Down
Loading