| # Detects the two failure modes the retry loop inside run_all.sh cannot see. | |
| # | |
| # (setsid nohup bash scripts/watchdog.sh > work/logs/watchdog.log 2>&1 < /dev/null &) | |
| # | |
| # WHAT THE RETRY LOOP ALREADY HANDLES: a step that exits non-zero. It retries three times. | |
| # | |
| # WHAT IT CANNOT HANDLE, AND WHY THIS EXISTS: | |
| # 1. A DEAD TRACK. When retr_F4 failed three times, its track function returned and the | |
| # track simply stopped - silently, with the other track still running and the status | |
| # file showing nothing new. Nothing noticed for seven minutes. This restarts a track | |
| # whose steps are unfinished but whose process is gone. Restarting is safe because | |
| # every step skips work whose output already exists. | |
| # 2. A HUNG STEP. A process that is alive but has written nothing for a long time is | |
| # worse than a crash: the retry loop waits on it forever. CUDA deadlocks and NFS | |
| # stalls both look like this. After STALL_S with no log growth the step is killed, and | |
| # run_all's own retry then restarts it from its checkpoint. | |
| # | |
| # It never touches anyone else's processes: every kill is matched on our own script paths | |
| # under this directory, checked against the command line, never on a bare name. | |
| set -uo pipefail | |
| cd "$(dirname "$0")/.." | |
| ROOT=$(pwd) | |
| W="${BERX_WORK:-$ROOT/work}" | |
| L="$W/logs" | |
| STALL_S="${STALL_S:-1500}" # 25 min with no log growth = hung | |
| POLL_S="${POLL_S:-60}" | |
| mkdir -p "$L" | |
| log() { echo "[$(date '+%H:%M:%S')] $*"; } | |
| track_alive() { # $1 = 1 or 2 | |
| # Per-track pidfile written by run_all.sh. Counting run_all.sh processes cannot distinguish | |
| # the tracks - with either one running the count is identical - so a dead track hid behind | |
| # a live one. That is the bug this watchdog exists to catch, so it must not repeat it. | |
| local pf="$L/track$1.pid" | |
| [ -f "$pf" ] || return 1 | |
| local pid; pid=$(cat "$pf" 2>/dev/null) | |
| [ -n "$pid" ] && [ -d "/proc/$pid" ] | |
| } | |
| track_done() { # track 1 is done once the submission exists; track 2 once shards exist | |
| case "$1" in | |
| 1) [ -f "$W/output_v2/matching_results.tsv" ] || grep -q '"pass": false' \ | |
| "$W/rescore/gate.json" 2>/dev/null ;; | |
| 2) [ -f "$W/shards/test_manifest.json" ] ;; | |
| esac | |
| } | |
| restart_track() { | |
| local t="$1" | |
| log "TRACK $t appears dead with work outstanding - restarting (steps with outputs skip)" | |
| ( cd "$ROOT" && TRACKS="$t" setsid nohup bash scripts/run_all.sh \ | |
| > "$L/run_track${t}_restart_$(date +%H%M%S).log" 2>&1 < /dev/null & ) | |
| } | |
| log "watchdog up. stall=${STALL_S}s poll=${POLL_S}s work=$W" | |
| while true; do | |
| sleep "$POLL_S" | |
| # ---- 1. hung stage, judged by CPU TIME, not by log mtime. | |
| # | |
| # A log that has not grown does NOT mean the job is stuck: Python block-buffers stdout to | |
| # a file, so a stage started without -u writes nothing for its entire run. t1_fit.log sat | |
| # at 0 bytes while the fit burned 198% CPU. Killing on log mtime alone would have | |
| # destroyed a healthy 40-minute job. | |
| # | |
| # A process that is genuinely wedged - CUDA deadlock, NFS stall - stops accumulating CPU | |
| # time. That is the signal. The log is only used as a secondary confirmation. | |
| for pid in $(pgrep -u "$USER" -f "scripts/[0-9][0-9]_.*\.py" 2>/dev/null); do | |
| cmd=$(tr '\0' ' ' < "/proc/$pid/cmdline" 2>/dev/null) | |
| # Only ever our own scripts, under our own root. Other users run their own pipelines on | |
| # this box and one of them is a scripts/run_p4_verifier.py. | |
| case "$cmd" in | |
| *"$ROOT/scripts/"*|*" scripts/0"*|*" scripts/1"*) ;; | |
| *) continue ;; | |
| esac | |
| # utime+stime in clock ticks, fields 14 and 15 of /proc/<pid>/stat | |
| cpu=$(awk '{print $14+$15}' "/proc/$pid/stat" 2>/dev/null) || continue | |
| seen="$L/.cpu_$pid" | |
| if [ -f "$seen" ]; then | |
| read -r oldcpu oldts < "$seen" | |
| if [ "$cpu" -gt "$oldcpu" ]; then | |
| echo "$cpu $(date +%s)" > "$seen" # progressing | |
| else | |
| idle=$(( $(date +%s) - oldts )) | |
| if [ "$idle" -ge "$STALL_S" ]; then | |
| log "STALL pid $pid used no CPU for ${idle}s: $cmd" | |
| log " killing it so run_all retries the step from its checkpoint" | |
| kill -TERM "$pid" 2>/dev/null; sleep 10; kill -KILL "$pid" 2>/dev/null | |
| rm -f "$seen" | |
| fi | |
| fi | |
| else | |
| echo "$cpu $(date +%s)" > "$seen" | |
| fi | |
| done | |
| # forget bookkeeping for processes that have exited | |
| for f in "$L"/.cpu_*; do | |
| [ -f "$f" ] || continue | |
| p=${f##*/.cpu_} | |
| [ -d "/proc/$p" ] || rm -f "$f" | |
| done | |
| # ---- 2. dead track with work outstanding | |
| for t in 1 2; do | |
| track_done "$t" && continue | |
| if ! track_alive "$t"; then | |
| # confirm over two polls so a momentary gap between steps is not mistaken for death | |
| sleep 15 | |
| track_alive "$t" && continue | |
| restart_track "$t" | |
| sleep 30 | |
| fi | |
| done | |
| # ---- 3. announce the milestones, so the log alone tells the story | |
| if [ -f "$W/rescore/gate.json" ] && [ ! -f "$L/.announced_gate" ]; then | |
| log "GATE A: $(tr -d '\n ' < "$W/rescore/gate.json")"; touch "$L/.announced_gate" | |
| fi | |
| if [ -f "$W/shards/test_manifest.json" ] && [ ! -f "$L/.announced_shards" ]; then | |
| log "CE SHARDS READY - Colab lanes can start"; touch "$L/.announced_shards" | |
| fi | |
| if [ -f "$W/output_v2/matching_results.tsv" ] && [ ! -f "$L/.announced_sub" ]; then | |
| log "SUBMISSION WRITTEN: $W/output_v2/matching_results.tsv"; touch "$L/.announced_sub" | |
| fi | |
| done | |