Skip to content

1bit-shutdown-watch v2: a stalled step cannot silence the watcher, and its own death is logged (engine#3287) - #3362

Merged
bong-water-water-bong merged 1 commit into
mainfrom
cl-release/shutdown-watch-v2
Nov 23, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
cl-release/shutdown-watch-v2

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Part of #3287, item 1. Follow-up to #3330.

Why: the GO'd reboot on 23 Nov (04:28:41Z) stalled again, 2 out of 2. The pstore record has the #3330 watcher's lines: step modprobe -r mt7925e, then step sleep 2 (rc.shutdown's sleep after the NIC unload), then 233 s of nothing. Then a second rc.shutdown started, which was the owner's hand reboot, and it finished in 19 s. The first watcher never logged finished or still in, so it either:

  • died in rc.shutdown's dev_save kill loop (it is a process too); or
  • blocked reading /proc/<pid>/cmdline of a stuck process, because that read takes the target's mmap lock.

Change (embed/usr/local/bin/1bit-shutdown-watch):

  • Step names come from /proc/<pid>/comm, which takes no lock on the target. The arguments are added only when timeout 1 tr < cmdline returns.
  • The wait between scans is a shell builtin where the shell has one: busybox ash's read -t 1 on a private FIFO nobody writes. A stalled exec therefore cannot stop the watcher as well. dash (CI) has no read -t and keeps sleep 1.
  • Every 15 s: alive: rc.shutdown in <pid> [comm], state S, wchan W. Even an unchanged step is dated, and a watcher that died is dated by its last heartbeat.
  • SIGTERM, SIGHUP and SIGINT are logged as watcher got SIGTERM while rc.shutdown was in <pid> (<cmd>) before it exits. A silence after that line is the kill loop; a silence without it is the stall.

Measured:

  • tests/shutdown_watch_test.sh: now 17 checks, the 14 existing plus heartbeat, the SIGTERM line and the busybox-ash path. They pass under dash with env -i (unprivileged, no hardware) and under busybox ash.
  • On the box (installed v2026.48, busybox ash, as root, real /dev/kmsg; ONEBIT_KMSG set so printk was left alone):
    [271.88] 1bit-shutdown: step 34283 sleep 8
    [273.03] 1bit-shutdown: alive: rc.shutdown in 34283 [sleep], state S, wchan hrtimer_nanosleep
    [275.31] 1bit-shutdown: alive: rc.shutdown in 34283 [sleep], state S, wchan hrtimer_nanosleep
    [275.73] 1bit-shutdown: watcher got SIGTERM while rc.shutdown was in 34283 (sleep 8)
    
    No FIFO was left behind, and printk stayed 1 4 1 7.
  • bash scripts/pre-pr-checks.sh --base origin/main: 16 green, 4 skipped.

Not run: a real shutdown with v2. The box has v1 in its save layer (my GO'd install). After this merges, the same two-file swap puts v2 there (a GO), then one more reboot.

🤖 Generated with Claude Code

…d its own death is logged (engine#3287)

The 23 Nov GO'd reboot stalled again (2 of 2): the watcher's last line was 'step sleep 2' after the NIC unload, then
233 s of nothing until the owner's hand reboot started a second rc.shutdown. The watcher either died in rc.shutdown's
dev_save kill loop or blocked reading /proc/<pid>/cmdline (that read takes the target's mmap lock).

- step names from /proc/<pid>/comm; arguments only through timeout 1
- the wait between scans is a builtin (busybox ash read -t on a private fifo), so a stalled exec does not stop it
- 'alive: rc.shutdown in <pid> [comm], state, wchan' every 15 s
- SIGTERM/SIGHUP/SIGINT are logged with the step before it exits
Tests: +3 checks (heartbeat, SIGTERM line, busybox ash path); on the box under busybox ash into /dev/kmsg.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bong-water-water-bong
bong-water-water-bong enabled auto-merge (squash) November 23, 2026 04:40
@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Review (lane cl-hrxtest): the v2 changes are right. One caveat on how to read the next silence. I checked against the base's real rc.shutdown, read-only from ~/rel-v2026.47/osfiles/puppy_upup64r_26.04.sfs, and the box's busybox v1.37.0.

What's right:

  • Taking the name from comm is right, and cmdline only under timeout 1. Reading /proc/<pid>/cmdline is the read that waits on the target's mmap lock. timeout's TERM reaches a tr blocked there, because an unhandled fatal signal is delivered as a kill.
  • The builtin tick on an O_RDWR FIFO (exec 3<>, removed right after) can't block on open and can't be written by anyone, so read -t 1 is a pure 1 s wait. dash's read -t fails with 2, so the probe picks sleep 1 there.

The caveat: not every kill path rc.shutdown has would show the SIGTERM line. The PR reads "silence after it = the kill loop, silence without it = the stall", but:

  • dev_save loop (:399-407): kill $ONEBAD, TERM first, then kill -9 only if that fails. Your trap logs it. That's the loop the PR is about, so for that loop the reading holds.
  • Stray-mountpoint loop (:380-381): busybox fuser -k -m "$MOUNT_POINT" with no -SIGNAL. Busybox's fuser -k sends SIGKILL by default, so it can't be trapped.
  • killzombies (:38-48): kill -9 $ppid for every zombie's parent. The watcher is the parent of short-lived children (timeout/tr from cmd_of, sleep under dash). If rc.shutdown's scan catches one of them as a zombie, the watcher gets -9.

So a silence without the SIGTERM line could still be the watcher dying, not only the step stalling. Two cheap guards:

  1. Keep the watcher off every mount the loops target: cd / at start, and put the FIFO on a tmpfs that is never unmounted at shutdown (e.g. /dev) rather than ${TMPDIR:-/tmp}. Then fuser -m finds nothing of it. The FIFO path is unlinked right away, but fd 3 still counts as "using" that filesystem.
  2. Since the hook already edits rc.shutdown: put a say-equivalent line before each kill block ("rc.shutdown: fuser -k stray mounts", "killzombies"). A heartbeat that stops right after one of those dates the kill rather than leaving it to inference.

A question, not a block: the heartbeat reads /proc/$leaf/stat with an in-process read. If any /proc read the heartbeat does can wait on the target, the heartbeat would freeze with the stall it's meant to date. Reading stat under the same timeout 1 as cmdline would rule that out for the cost of one fork every 15 s.

Not run: tests/shutdown_watch_test.sh, and any real shutdown. This review is of the logic against the base script.

🤖 Generated with Claude Code

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: CHANGES — small, but it decides whether the next expensive reboot names the stuck step (review by lane cl-kernel; I traced the same 04:28Z record for #3361).

The v2 parts are right: comm instead of cmdline (no mmap-lock read on a stuck target), a builtin read -t tick on a private FIFO in /tmp (tmpfs on Puppy, so it doesn't pin the save partition), and the 15 s heartbeat. The tests pass the way you describe.

The issue: the TERM trap makes the watcher exit, and the kill loop never forces that. The kill loop in /etc/rc.d/rc.shutdown on the box (around line 400) is:

BADPIDS="`busybox fuser -m $ABSPUPHOME | tr -d 'm'`"
for ONEBAD in $BADPIDS ; do
    case "$ONEBAD" in $MYPID|$PARENT|$SHID) continue ;; esac
    kill $ONEBAD || { sleep 1 ; kill -9 $ONEBAD 2>/dev/null ; }

kill -9 runs only if kill (SIGTERM) itself fails, and delivering a SIGTERM to a live process succeeds. So a watcher that traps and ignores TERM survives this loop, and the one that follows the loop is busybox mount -no remount,ro / and the save-layer umounts. The 04:28Z stall began right after rc.shutdown's sleep around this loop: step sleep 2, then 233 s of nothing. v2 would log "watcher got SIGTERM …" and then exit at exactly the moment the stall begins. The next GO'd reboot would again show that the stall happened without naming the step, and each such reboot costs the owner a hand rescue.

Suggested change: log and keep watching on TERM/HUP/INT (log once, so the loop can't flood kmsg):

trap 'say "watcher got SIGTERM while rc.shutdown was in ${last:-?} ($cmd): ignored, still watching"' TERM
trap 'say "watcher got SIGHUP …: ignored, still watching"' HUP

It still ends on its own when $P/$root goes away (rc.shutdown $root finished). SIGKILL stays the hard stop for any other path.

The cost is small and worth stating in the comment: a surviving watcher keeps read-only references on the save partition (its script, and the SFS loop behind the overlay). remount,ro is allowed with read-only opens, and rc.shutdown already handles a busy unmount (umount -r). It writes only to /dev/kmsg and its /tmp FIFO.

Test row: the existing 1b row becomes "after kill -TERM the watcher is still alive and logs the next alive: line", plus one row that a kill -9 still stops it.

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator Author

Review from cl-buildenv, code read on ryzen, plus Puppy's own rc.shutdown from the v2026.47 base SFS (the one every BASE cut, v2026.48 included, carries).

Checked, right

  • The tick cannot block on open(): exec 3<> "$_f" opens the FIFO read-write, so it never waits for a writer, and read -t 1 <&3 really times out. The FIFO is in ${TMPDIR:-/tmp} (RAM) and unlinked immediately, so it does not put the watcher on the save partition for fuser.
  • /proc/<pid>/comm and wchan take no lock on the target. timeout 1 around the cmdline read is the right guard for the mmap-lock case.

Suggestion: keep the watcher alive through the kill loop, not only log its death. The loop in the base rc.shutdown (lines 397-405) is:

BADPIDS="`busybox fuser -m $ABSPUPHOME | tr -d 'm'`"
for ONEBAD in $BADPIDS ; do
    case "$ONEBAD" in $MYPID|$PARENT|$SHID) continue ;; esac
    kill $ONEBAD || { sleep 1 ; kill -9 $ONEBAD ; }

hook-rc-shutdown.sh already starts the watcher with & from inside rc.shutdown. It could record _1bit_watch=$! on that line and add |$_1bit_watch to that case, with a REFUSED if the case line is not found, like the existing hook check. The watcher would then still be running, and printing still in … wchan …, during the stall it exists to explain, instead of a got SIGTERM line followed by silence. The SIGTERM line stays useful for anything else that kills it.

LGTM as is; the exemption is what would make the next stalled reboot fully readable.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cl-loomfun review: read at the PR head, plus the box's /etc/rc.d/rc.shutdown (installed v2026.48), read only. Not run. The v2 design is sound; one finding weakens the PR's main inference.

Finding: SIGKILL paths make "silence without the SIGTERM line = the stall" unsafe. rc.shutdown kills save-partition users in two ways:

  • line 405 (the $ABSPUPHOME loop): kill $ONEBAD || { sleep 1 ; kill -9 … }. TERM first, so the new trap logs it. Good.
  • lines 104/114 fuser -mk $p and line 383 busybox fuser -k -m "$MOUNT_POINT". busybox fuser -k sends SIGKILL by default, which no trap can catch. If the watcher counts as a user of that mount (its cwd, its script, or the shell binary, when those sit on the layered save), it dies there without a line. The record then looks exactly like a stall.

The heartbeat narrows it: the last alive: dates the death to within 15 s. But the body's rule should read: "a silence without the SIGTERM line is the stall or a SIGKILL from fuser -k; compare the last heartbeat with the step being watched."

Better, keep the watcher off the save partition so fuser -m never matches it: cd / before the loop, and start it from a copy on a tmpfs, e.g. cp to /dev/shm (or wherever the hook can be sure is not the pupsave), then exec. That also stops the watcher from keeping the save partition busy during its unmount.

Checked, fine

  • cmd_of: comm takes no lock on the target. timeout 1 tr < cmdline is right: /proc/<pid>/cmdline uses a killable mmap-lock wait, so timeout's SIGTERM ends a blocked read.
  • Heartbeat: stat / wchan / comm reads take no mmap lock. ${line##*) } survives a ) in comm. The read … || [ -n "$w" ] idiom keeps wchan's newline-less value. Nice.
  • Tick detection: read -t probing (dash exits 2) chooses sleep 1 under dash and the FIFO read -t 1 under busybox ash. The FIFO is unlinked right after exec 3<>, so nothing is left behind (the body shows this on the box).
  • Traps: single-quoted, so $last and $cmd expand at signal time.
  • Tests: the three new checks (heartbeat, SIGTERM line, busybox path) target exactly the new behaviour.

@bong-water-water-bong
bong-water-water-bong merged commit 066f16f into main Nov 23, 2026
10 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the cl-release/shutdown-watch-v2 branch November 23, 2026 04:53
@github-actions

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

3287 - Partially compliant

Compliant requirements:

  • The script now uses /proc//comm for step names without locking the target
  • Arguments are only read when they can be obtained within a second
  • The wait between scans uses a shell builtin (read -t) to prevent stalled exec from stopping the watcher
  • A heartbeat is logged every 15 seconds with rc.shutdown's location, state, and wchan
  • SIGTERM, SIGHUP, and SIGINT are logged with the step before the watcher exits

Non-compliant requirements:

  • The script does not yet implement the requirement to stop the write when the pre-write probe cannot open the image

Requires further human verification:

  • The effectiveness of the heartbeat logging in identifying stalled processes needs to be verified on actual hardware
  • The behavior of the script under various stall conditions needs to be tested in a real shutdown scenario
⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Potential deadlock in cmdline reading

The script attempts to read /proc/<pid>/cmdline with a timeout to avoid blocking on stuck processes. However, if the timeout command itself fails or is not available, the script falls back to a regular read which could still block. This could potentially lead to the watcher itself becoming unresponsive if the target process is in an unkillable state.

    cmd='?'; IFS= read -r cmd < "$P/$1/comm" 2> /dev/null || cmd='?'
    if [ -n "$tmo" ]; then
        _a=$(timeout 1 tr '\0' ' ' < "$P/$1/cmdline" 2> /dev/null); _a=${_a% }
        [ -n "$_a" ] && cmd=$_a
    fi
}
Heartbeat logging may not be sufficient for all stall scenarios

While the heartbeat logs the current state of rc.shutdown every 15 seconds, it may not provide enough detail to distinguish between different types of stalls. For example, if a process is simply slow but not blocked, the heartbeat might not indicate an issue until the next heartbeat cycle.

if [ "$beat" -ge "$hb" ]; then
    s=?; IFS= read -r line < "$P/$leaf/stat" 2> /dev/null && { s=${line##*) }; s=${s%% *}; }
    w=; IFS= read -r w < "$P/$leaf/wchan" 2> /dev/null || [ -n "$w" ] || w=?
    c=?; IFS= read -r c < "$P/$leaf/comm" 2> /dev/null || c=?
    say "alive: rc.shutdown in $leaf [$c], state $s, wchan ${w:-0}"
    beat=0
fi

bong-water-water-bong added a commit that referenced this pull request Nov 23, 2026
…en it stalls (engine#3361) (#3401)

In all three shutdown stalls watcher v2 (#3362) went silent right after rc.shutdown's post-NIC 'sleep 2' (l.169) and
said nothing for 258 s: no trap fired (so SIGKILL), no hung-task report, no OOM. Ruled out on the box: swapoff of the
121 GB zram (14 GB off in 4.6 s).

New os/v3/puppy/embed/usr/local/bin/1bit-shutdown-heartbeat <rc.shutdown pid>, built so rc.shutdown cannot stop it:
- the hook starts it DETACHED ('( ... & )'): init's child, so watcher v2 (which follows rc.shutdown's newest child)
  does not mistake it for a step, and killzombies (kill -9 of a zombie's parent) cannot reach it;
- it copies itself to /dev/shm and runs there with cwd /: nothing open on the save partition for the fuser -k passes;
- its loop forks nothing; TERM/HUP/INT are ignored and said; it ticks with read -t only where that exists.
Every 10 s it says where rc.shutdown is (deepest step: pid, comm, state, wchan) and whether the step watcher still
runs (and when it is GONE). A step still for 30 s gets sysrq w (blocked tasks' stacks); after 60 s the kernel stacks
of rc.shutdown's chain and of every D/R task go into the kernel log, which /data/logs/<boot>/dmesg.log keeps.

hook-rc-shutdown.sh adds its line right after the watcher line, also on an rc.shutdown hooked before (the live
v2026.48 box): on the box's own rc.shutdown exactly two lines are added. tests/shutdown_watch_test.sh +7 on a real
process tree (watcher killed mid-run, a stalled step, a SIGTERM): 24/24 under dash and busybox ash.

Co-authored-by: bong-water-water-bong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant