Skip to content

[CTX-2466] fix(stability): opt-in main-loop stall watchdog and long-run soak (#2483) - #2488

Merged
Xuepoo merged 4 commits into
mainfrom
ctx-2483/diag-longrun-freeze
Oct 22, 2026
Merged

Xuepoo merged 4 commits into
mainfrom
ctx-2483/diag-longrun-freeze

Conversation

@Xuepoo

@Xuepoo Xuepoo commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

What

Issue #2483's two engineering follow-ups, with the owner's freeze shape reproduced and captured live:

  1. Opt-in main-loop stall watchdog (crates/bitty-terminal/src/stall_watchdog.rs): when a tick/event phase makes no progress for N ms, a monitor thread logs the last phase, uptime, bounded app context, process totals, and a bounded thread-state snapshot (state, wchan, syscall number, comm). It also writes one truncated XDG state slot so a terminated session still leaves evidence.
  2. Bounded long-run soak harness (scripts/longrun-soak.sh): isolated Xvfb + XDG roots, real output/scrollback/image/input/resize load, a liveness probe on every sample, and an opt-in --stall-probe that reproduces the blocking-PTY-write class.

No product behavior changes: the watchdog is quiet unless BITTY_STALL_WATCHDOG opts in, and phase marks are one relaxed store plus a timestamp.

Why

The owner saw Application Not Responding after a long session with no hung process and no captured log. The issue asks for (a) a bounded watchdog that records the last phase/stage/uptime and thread state and (b) an audit of long-uptime blocking/growth paths, validated by a soak.

Proven findings

P1 — a non-reading raw-mode child parked the event loop in write(2) (captured pre-fix; fixed by #2457)

While #2457 was still unmerged, the event loop wrote PTY input inline: PtyWriter::write_all forwarded to the blocking sink (crates/bitty-pty/src/writer.rs:45), called from write_input_best_effort (crates/bitty-runtime/src/runtime/input.rs:2050) on the runtime owner thread (route_input_bytes, same file).

Live capture from scripts/longrun-soak.sh --stall-probe --expect-stall (isolated Xvfb, real fish, sh -c 'stty raw -echo; sleep 900' in the pane), on a pre-b0ad911a base:

stall-probe: send 1 16 ms ... send 6 19 ms
stall-probe: send 7 BLOCKED rc=124 after 3004 ms
stall-probe: BLOCKED (event loop parked; evidence in .../stall-evidence.txt)

bitty: main-loop stall detected: no progress for 2029 ms (threshold 2000 ms, phase=ctl-drain, app_uptime 19211 ms, progress=6630)
  focus=view-1 leaves=1 panes=0 scrollback=1323 lines presented=15
  plugins=1 [bitty-terminal.bar] bands=1 [bitty-terminal.bar]
  process: threads=12 fds=61 rss=198964 KiB
  [main] tid=1453304 state=S wchan=wait_woken syscall=1 comm=bitty
  tid=1453308 state=S wchan=__x64_sys_ppoll.cold syscall=271 comm=bitty
  tid=1453310 state=S wchan=wait_woken syscall=- comm=bitty-pty-reade
  tid=1453311 state=S wchan=hrtimer_nanosleep syscall=230 comm=bitty
  tid=1453312 state=R wchan=- syscall=- comm=bitty-stall-wat
  ...
bitty: main-loop stall recovered after 15104 ms (phase=pty-pump)

syscall=1 is write(2) on x86_64; phase=ctl-drain is the controller-queue drain that applies terminal send. This is the paru/#2446 class, reproduced against the long-run shape, and it validated the watchdog's phase/thread snapshot end-to-end.

Canonical mode does not block (the kernel buffers hundreds of KiB); raw mode with a non-reading child is the bounded-queue shape, which is why the probe sets stty raw -echo. The probe waits for the child in the process table before bursting, so a burst can never land in a still-cooked prompt.

#2457 is now merged as b0ad911a (#2446). After rebasing onto it, the same probe stays bounded:

stall-probe: send 1 8 ms ... send 6 19 ms
stall-probe: send 7 28 ms
stall-probe: send 8 32 ms
stall-probe: PASS (bounded writes, runtime responsive)

samples=15  liveness_timeouts=0  stall_reports=0  probe=pass

On a pre-#2457 base the same probe in regression mode exits 1 (FAIL stall probe blocked); on current main it exits 0, so the harness is a regression check for the fix.

P2 — no long-uptime growth or stall under mixed load (5-minute soak)

scripts/longrun-soak.sh --duration-secs 300 --interval-secs 10 --workload mixed with the bar plugin active (output bursts, Kitty transmit/place/delete, input, X11 resize):

samples=30  liveness_timeouts=0  stall_reports=0  probe=skip
ctl latency 6..27 ms throughout
rss 197,548 -> 200,364 KiB (+2.8 MiB, 1.4%; dipped to 196,268 mid-run)
threads 11, fds 58 (constant)

A separate idle instance stayed responsive after 25 minutes (ctl 20-40 ms, 15 threads, 62 fds), so idle parking alone does not drift.

P3 — existing long-run bounds verified (static)

Path Bound Evidence
scrollback 100,000 lines hard max, default 5,000 bitty-runtime/src/config.rs:68, :1352; invariant check bitty-term-state/src/state/invariants.rs:239
cold event queue 256 bitty-runtime/src/config.rs:1128
ctl input command queue 8, drop-newest counted bitty-terminal/src/terminal_app.rs:331, :2609
PTY forward stage 128 KiB (CHANNEL_CAPACITY_CHUNKS × READ_CHUNK_SIZE) bitty-runtime/src/runtime/pty.rs:638
PTY replies 4 KiB cap, overflow counted bitty-runtime/src/runtime/panes.rs:1056
notifications 8 bitty-runtime/src/runtime/bell.rs:48
component events 256 bitty-runtime/src/runtime/component_host.rs:30
plugin timers 64/plugin, <=24 h delay, <=512 fired per pump bitty-lua/src/host.rs:373, bitty-runtime/src/plugin_runtime/mod.rs:1878
plugin callbacks 50 ms RC-1 wall budget + instruction budget bitty-plugin-host/src/event.rs:721
Kitty store 64 images / 256 MiB / 128 placements, 64 MiB decode bitty-rich/src/lib.rs:47-53
config watcher 500 ms throttled, full parse only on change bitty-terminal/src/config_reload.rs:471
session save exit/close paths only, no periodic autosave bitty-terminal/src/terminal_app.rs:911 call sites
audio sampling off the activation thread, 10 Hz control floor bitty-plugin-host/src/audio.rs:677

Suspected, not reproduced

  • GPU device init/resume on the main thread (pollster::block_on(GpuContext::initialize_for_surface), terminal_app.rs:3611). A driver-level hang would look identical to the owner's ANR, but it is a one-shot window/resume path, not long-uptime growth. Not triggered in the soak.
  • Plugin timer callbacks run Lua on the main thread under the 50 ms wall budget (pump_due_timers_at). A host call that blocks past the budget would pause a tick; no such stall was observed with the bar plugin's 1 s clock and modules.
  • Config reload parses on the main thread when the file changes (throttled to 500 ms). A pathological config could pause one tick; not observed.

None of these are claimed as the owner's trigger. The watchdog is designed to name the phase on the next occurrence.

Watchdog contract

  • BITTY_STALL_WATCHDOG=1|true|on|yes enables it; BITTY_STALL_WATCHDOG=<ms> enables with that threshold; BITTY_STALL_WATCHDOG_MS overrides (clamped 50..=600000 ms). Absent/0/false/off/garbage leaves it disabled, so CI and normal sessions spawn no thread and emit nothing.
  • Report: threshold, idle time, phase, app uptime, progress marks, focus/leaf/pane/scrollback counts, plugin and band census (bounded ids), process threads/fds/RSS, and up to 64 thread rows (tid, state, wchan, syscall name/number, comm). At most 8 KiB per report, 5 reports per stall 30 s apart, one recovery line, then suppression.
  • Latest stall report is also written to $XDG_STATE_HOME/bitty/stall-watchdog.log (single truncated slot; recovery lines never overwrite it).
  • No host paths in any report; emission crosses the same redact_text_for_stderr gate as other diagnostics.
  • The app publishes its context sample at most twice a second; a disabled watchdog costs one null check per mark.

Soak harness

# 10-minute mixed soak, isolated display, watchdog on
bash scripts/longrun-soak.sh --duration-secs 600

# capture mode on a pre-#2457 base (exit 0 when a stall is captured)
bash scripts/longrun-soak.sh --duration-secs 120 --stall-probe --expect-stall

# regression mode on current main (fails if the probe blocks)
bash scripts/longrun-soak.sh --duration-secs 120 --stall-probe

The harness starts only its own Xvfb and app process group, records those PIDs, and kills only them. Evidence (samples.csv, summary.txt, bitty.log, stall-evidence.txt, optional stall-stacks.txt) stays under --out-dir (default recording/longrun-soak, gitignored) and carries counts, not host paths. just longrun-soak-test runs the headless contract test in CI.

Verification

  • cargo fmt --all -- --check — pass
  • cargo clippy -p bitty-terminal --all-targets --locked -- -D warnings — pass
  • cargo test -p bitty-terminal --bin bitty stall_watchdog — 13/13 pass
  • just longrun-soak-test — PASS
  • scripts/check-scratch-paths.sh — OK
  • cargo check -p bitty-terminal --target x86_64-pc-windows-gnu --all-targets --locked with RUSTFLAGS=-D warnings — pass (also covers the not(target_os = "linux") watchdog branch)
  • cargo check -p bitty-terminal --target aarch64-apple-darwin --all-targets --locked with RUSTFLAGS=-D warnings — pass
  • Live: 300 s mixed soak (P2), 120 s mixed soak (12/12 samples, 0 stalls), pre-[CTX-1447] fix(pty): bound event-loop PTY writes off-thread (#2446) #2457 stall-probe capture (P1), and post-[CTX-1447] fix(pty): bound event-loop PTY writes off-thread (#2446) #2457 regression-mode probe (probe=pass, exit 0), all on isolated Xvfb.
  • The earlier M1/Compat aggregate failures were fallout: the macOS/Windows jobs died on the unused Linux-only constant before uploading results, so both aggregates reported MISSING; both cross-target checks now pass.
  • just ci-local / act not run in this environment (commander directive); remote CI is the gate.

Follow-ups

  • [CTX-1447] fix(pty): bound event-loop PTY writes off-thread (#2446) #2457 (queued PTY writer) merged as b0ad911a and fixes P1; this PR is the diagnostics that captured it plus the harness that now guards it.
  • The watchdog report is the evidence source for the next owner occurrence: ~/.local/state/bitty/stall-watchdog.log.
  • GPU-init blocking remains a suspected one-shot path; a separate task if the next capture names window-create.

Priority: P0 | Area: stability | Labels: fix,P0,area:stability | Milestone: 0.0.26 | RFC: OQ-xxx | Task: CTX-2466
Closes #2483

@Xuepoo Xuepoo added this to the 0.0.26 milestone Oct 22, 2026
@Xuepoo Xuepoo added fix Bug fix P0 Priority: highest area:stability Stability hardening labels Oct 22, 2026
@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 51 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI (base), Organization UI (inherited)
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ce4d8c6b-61f5-425a-9e52-8c62aac09996
📥 Commits

Reviewing files that changed from the base of the PR and between ec0af37 and 416e7be.

📒 Files selected for processing: 6
  • crates/bitty-terminal/src/main.rs
  • crates/bitty-terminal/src/stall_watchdog.rs
  • crates/bitty-terminal/src/terminal_app.rs
  • justfile
  • scripts/longrun-soak.sh
  • scripts/tests/longrun-soak.test.sh
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Xuepoo
Xuepoo force-pushed the ctx-2483/diag-longrun-freeze branch 2 times, most recently from 60886b3 to e66365d Compare October 22, 2026 11:30
)

The owner's long-run freeze left no process or log behind. Add a bounded,
quiet-by-default observer: the event loop marks its current phase, a monitor
thread samples the heartbeat from outside, and a no-progress window writes
the last phase, uptime, bounded app context, process totals, and a bounded
thread-state snapshot (state/wchan/syscall/comm).

The latest stall report also lands in one truncated XDG state slot so a
terminated session still leaves evidence. Disabled unless
BITTY_STALL_WATCHDOG opts in; no product behavior change.

Refs #2483
…2483)

scripts/longrun-soak.sh runs an isolated Xvfb display and XDG roots, drives
output/scrollback, kitty images, input, and resizes, probes ctl liveness and
RSS/threads/fds every sample, and counts watchdog reports. --stall-probe
reproduces the blocking raw-mode PTY write and captures the watchdog report.

The live path owns only its recorded process groups; CI runs the headless
contract test.

Refs #2483
…2483)

Readiness now comes from the process table (the non-reading child is a real
process), not grid text: in a no-WM Xvfb session the presented grid may not
carry the marker, and a wrapped marker would never match as one string.

The probe also records a diagnostic dump on failure, and an inconclusive
start fails closed instead of reporting a silent pass. Regression mode
(--stall-probe without --expect-stall) now fails when the burst blocks.

Refs #2483
The thread sampler reads /proc/self/task, so MAX_THREAD_ROWS is only used on
Linux. The macOS and Windows bin builds run with -D warnings and rejected the
unused constant; gate it at the definition with the reason. Those jobs then
produced no M1/compat artifacts, which is why both Tier 1 aggregates reported
MISSING results.

Verified with -D warnings for x86_64-pc-windows-gnu and aarch64-apple-darwin.

Refs #2483
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:stability Stability hardening fix Bug fix P0 Priority: highest

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Long-running Bitty becomes unresponsive (Application Not Responding)

1 participant