Repository navigation
[PR] fix: event-loop-watchdog survives an OS freeze instead of self-exiting into a dead port - #7493
ranxianglei wants to merge 6 commits into
Conversation
…ck gap and resets its baseline instead of self-exiting (#7482) On an unattended suspending device (phone, NAS, deep-sleep laptop) the OS freezes the WHOLE process — main thread AND worker. The watchdog worker could not tell that freeze apart from a genuine main-thread wedge, so it tripped on the accumulated silence and SIGKILLed the lane into a dead port with no spawner left to respawn it (the port then sat dead for hours). The worker's OWN tick interval is the signal: on a real wedge the worker keeps ticking on schedule, but on a freeze it misses its own ticks. When the tick gap exceeds 2x checkIntervalMs the worker now resets lastBeat/warned/tripped and logs an info line instead of tripping, so the lane survives the freeze and is still alive when the device wakes. A genuine wedge spanning a freeze is delayed by one freeze cycle, never masked: the heartbeat gap grows again from the fresh baseline and trips as before. Adds watchdogFreezeVerdict (pure, unit-tested) and the worker-side isFrozen check. The existing wedge test still trips, confirming no false-positive on a real wedge.
…, not off (#7482) BILI_NATIVE_CLAUDE=0 opts the claude lane into passthrough mode (compression fully off) — the lane STILL starts and binds its port. It does NOT prevent the lane from starting, unlike the other BILI_NATIVE_* lanes where =0 turns the lane off entirely. The configuration table (en/zh/ja) said only 'marks a claude-native plugin process' with no =0 semantics, and lifecycle.md lumped claude in with the others as '=0 整体退出' — both wrong for claude. Split the lifecycle row to state claude's passthrough behavior separately.
…#7482) Adds a note to the claude-native section (en/zh/ja): the lane's watchdog now survives an OS freeze (the worker resets its baseline instead of self-exiting), so a lane alive when the device slept is still alive when it wakes. But the lane's self-heal still depends on a session being present to trigger a respawn after a genuine (non-freeze) wedge — on an unattended device with no session, a wedged lane leaves its port dead until the next session. For maximum robustness on such devices, the daemon posture (bili start + an external supervisor) is still the safest choice.
📦 Built Package ArtifactBranch: Latest build — install this exact versionnpm install -g billion-context@0.0.11462-pr.7493.41d621cThis exact version is immutable and reproducible. The rolling Recent builds (commit → version)
Download artifact
tar xzf billion-context-pr7493.tgz
npm install -g packageInstall instructions are refreshed on each push. |
… so docs-sync regeneration is drift-free (#7482) The passthrough clarification landed as a hand-edit of the GENERATED docs/configuration.{md,zh-CN.md,ja.md} tables, while the yaml seed kept the old one-line description — docs-sync regenerates from the seed and fails on the drift. Move the text into the seed (verified against src/claude-native-bootstrap.ts:71 and src/agent/native-bootstrap.ts:22); regeneration now reproduces the committed tables byte-for-byte.
(ai: vllm-qwen/qwen3.8-27b) Reviewed PR #7493 (event-loop-watchdog OS-freeze detection, fixes #7482). The core fix is sound and correct; the one CI red ( Core fix — correct. The discriminator is the worker's own tick interval, which is valid because the watchdog is a
The 2× threshold (10s default) is far below the 300s hard timeout, so any freeze long enough to cause a false trip is caught first. A genuine wedge spanning a freeze is delayed one freeze cycle, never masked permanently. Verified by reading the code and running the targeted test:
CI state (live, head 结构化八问
合并判定: 核心修复正确、无回归、测试到位; |
Field instrumentation for the anti-orphan gap: on hosts where the /proc walk and the sh -c fallback both miss the claude host, the parent watcher silently points at a long-lived ancestor and the lane outlives every session. Record the chosen pid (and the direct parent for comparison) in the hook log so the exact fallback can be pinned with one instrumented spawn.
… undefined instead of the direct parent (#7482) The chooseWatchdogParentPid fallback returned the direct parent pid when the /proc walk missed the claude host. On Android chroot and other session-holder environments (herdr, tmux, mosh), the direct parent is a long-lived ancestor (interactive bash, herdr-core) that outlives the claude session — the watcher pins to it and the anti-orphan contract is voided: the lane survives the session exit indefinitely. Fix: return undefined when the walk misses and the direct parent is not a transient shell. The watcher arms without a parent pid (gated on BILI_LAUNCHER_LANE so daemons stay daemons) and relies on the MCP child to register itself via POST /__bili/watcher. The startup gap (set empty before the MCP child registers) is handled with an everHadWatcher flag that defers the idle grace period until the first registration. Files: - src/claude-native-bootstrap.ts: chooseWatchdogParentPid returns number|undefined; debug log updated. - src/launcher.ts: watchPid = opts.parentPid (no fallback to process.pid); BILI_PARENT_PID = String(opts.parentPid ?? ); registerOwner returns "refused" when watchPid is undefined; all startOwnerHeartbeat calls guarded with watchPid !== undefined; markWatcherPendingRecovery cast. - src/server.ts: laneWatcherArmed = initialWatcherPid !== null || BILI_LAUNCHER_LANE !== undefined; everHadWatcher flag defers idle grace until first watcher registration. - src/server/admin.ts: POST /__bili/watcher allows registration when BILI_LAUNCHER_LANE is set (even if initialWatcherPid is null). - tests/claude-native.test.ts: updated the legacy direct-parent test to expect undefined (new behavior).
What
On an unattended suspending device (phone, NAS, deep-sleep laptop) the OS freezes the WHOLE process — main thread AND worker. The event-loop watchdog's worker could not tell that freeze apart from a genuine main-thread wedge, so it tripped on the accumulated silence and SIGKILLed the lane into a dead port with no spawner left to respawn it. The port then sat dead for hours until the next session started (the reporter's repro: a Claude Code native lane on an Android chroot, frozen overnight, ECONNREFUSED on the next-morning first request).
A second, independent defect surfaced in the same thread: when the /proc walk misses the claude host (e.g. under herdr/tmux/mosh session holders on Android chroot), the watcher pins to the direct parent — a long-lived ancestor (interactive bash, herdr-core) that outlives the claude session. The anti-orphan contract is voided: the lane survives the session exit indefinitely (21h observed).
Why this fix
Freeze detection: The worker's OWN tick interval is the signal that separates the two cases:
checkIntervalMs.When the tick gap exceeds 2×
checkIntervalMsthe worker now resetslastBeat/warned/trippedand logs an info line instead of tripping, so the lane survives the freeze and is still alive when the device wakes — the next session attaches to it instead of racing a cold spawn. A genuine wedge that happens to span a freeze is delayed by one freeze cycle, never masked permanently: after the reset the heartbeat gap grows again from the fresh baseline and trips as before.Anti-orphan: When the walk misses and the direct parent is not a transient shell,
chooseWatchdogParentPidnow returnsundefinedinstead of the parent pid. The watcher arms without a parent pid (gated onBILI_LAUNCHER_LANEso daemons stay daemons) and relies on the MCP child to register itself viaPOST /__bili/watcher. The lane dies when the MCP child dies (session end), which is the correct anti-orphan behavior. The startup gap (set empty before the MCP child registers) is handled with aneverHadWatcherflag that defers the idle grace period until the first registration.Changes
src/event-loop-watchdog.ts: addwatchdogFreezeVerdict(pure, unit-tested) + the worker-sideisFrozencheck intick(); storecheckIntervalMsand tracklastWorkerTickin the worker; document the behavior in the module header.tests/event-loop-watchdog.test.ts: add thewatchdogFreezeVerdictboundary test. The existing wedge test still trips, confirming no false-positive on a real wedge.src/claude-native-bootstrap.ts:chooseWatchdogParentPidreturnsnumber|undefined—undefinedwhen the walk misses and the direct parent is not a transient shell (was: the parent pid). Debug log updated.src/launcher.ts:watchPid = opts.parentPid(no fallback toprocess.pid);BILI_PARENT_PID = String(opts.parentPid ?? "");registerOwnerreturns"refused"whenwatchPidis undefined; allstartOwnerHeartbeatcalls guarded withwatchPid !== undefined;markWatcherPendingRecoverycast.src/server.ts:laneWatcherArmed = initialWatcherPid !== null || BILI_LAUNCHER_LANE !== undefined;everHadWatcherflag defers idle grace until first watcher registration.src/server/admin.ts:POST /__bili/watcherallows registration whenBILI_LAUNCHER_LANEis set (even ifinitialWatcherPidis null).tests/claude-native.test.ts: updated the legacy direct-parent test to expectundefined(new behavior).docs/configuration.{md,zh-CN.md,ja.md}+docs/lifecycle.md: clarifyBILI_NATIVE_CLAUDE=0is passthrough (the lane still starts, compression off) — it does NOT prevent the lane from starting, unlike the otherBILI_NATIVE_*lanes where=0turns the lane off entirely. (Drive-by: the lifecycle line lumped claude in with the others as=0 整体退出, which was wrong for claude.)docs/technical-notes.{md,zh-CN.md,ja.md}: note the unattended/suspending-device behavior — the lane now survives an OS freeze, but its self-heal after a genuine wedge still depends on a session being present; for maximum robustness on such devices the daemon posture + external supervisor is still the safest choice.Behavior change
Freeze detection:
Anti-orphan:
Not in this PR (needs owner decision)
What the watchdog should do on a genuine wedge + no spawner (not an OS freeze, e.g. a real deadlock on an unattended host): keep self-exiting (status quo) / self re-exec / a built-in
bili supervise. Tracked in a separate decision-request issue; this PR does not change that path.Closes #7482