Skip to content

Windows: terminal-agent watchdog respawn-loops every 60s — console flash + unbounded bun.exe leak; circuit-breaker can never trip #2295

Closed

Description

@rroojrooj

Summary

On Windows, the browse daemon's terminal-agent watchdog gets stuck in a permanent respawn loop: it spawns a fresh bun run terminal-agent.ts every 60s (AGENT_WATCHDOG_TICK_MS) even though the previous agent is alive and its pid record is fresh. Each spawn flashes a visible bun.exe console window (polyfill drops windowsHide — fix in PR #2294), old agents are never reaped, and the respawn circuit-breaker can mathematically never trip.

Environment

  • Windows 11 Pro, build 26200 (desktop)
  • gstack @ a3259400 (update-check reports UP_TO_DATE 1.60.1.0)
  • Browse daemon running as node.exe dist/server-node.mjs with bun-polyfill.cjs (the documented Windows fallback)

Measured behavior

Respawn cadence — process-creation watcher, one daemon (pid 35744), exact 60s beat:

13:19:43.206 START bun run ...\terminal-agent.ts  (parent 35744)
13:20:43.368 START bun run ...\terminal-agent.ts  (parent 35744)
13:21:43.500 START bun run ...\terminal-agent.ts  (parent 35744)

A second daemon (pid 47248, different project state dir) interleaved its own 60s loop → a console flash every ~30s wall-clock.

Leak — 7 spawns vs 1 exit in a 6-minute capture; process count 3 → 9 in ~10 minutes, ~90 MB working set. One daemon had been orphaned (its parent process dead) and looping like this for 2 days. The daemon-side kill path (spawnTerminalAgent kills the pid in the record before spawning) doesn't cover them: 9 agents alive with only 2 pid records in existence.

The respawn happens despite a healthy agent. At the time of the respawns, the terminal-agent-pid records existed, were freshly written (seconds-old), and pointed at running pids.

isProcessAlive is exonerated. I ran the exact implementation under node --require bun-polyfill.cjs (the daemon's real runtime): it returned true for a live agent pid and false for a dead one, with clean tasklist CSV output. So the mis-detection is somewhere else — possibly a stateDir/projectDir mismatch between where the daemon looks and where the agent writes (I did not isolate it; occasionally a tick is skipped, so the check does sometimes pass).

The circuit-breaker is dead code. RESPAWN_GUARD_MAX = 3 within RESPAWN_GUARD_WINDOW_MS = 60_000, but the watchdog tick is also 60s — entries older than the window are pruned before counting, so respawnHistory can hold at most ~1 entry at each tick and the guard can never reach 3. The "manual restart required" log line is unreachable at default settings.

Suggested directions

  1. Merge PR fix(browse): pass windowsHide in bun-polyfill spawn wrappers (Windows console flash every 60s) #2294 (windowsHide in the polyfill) — stops the visible flashing; no-op off Windows.
  2. Find why the watchdog considers a live, freshly-recorded agent dead (stateDir resolution seems the prime suspect).
  3. Make the guard window meaningfully larger than the tick (e.g. 10 min window / tick 60s), so a genuine crash-loop actually trips it.
  4. Reap: kill the previous agent by pid before spawning even when the record lookup misses, or track children directly — observed agents do self-exit when their daemon dies, but not when superseded.
  5. Consider a daemon-side "am I orphaned?" check — the 2-day looper's parent was long dead.

Happy to provide the raw watcher logs or test a branch on this machine.

🤖 Generated with Claude Code

Activity

  1. capy-ai commented on Oct 2, 2026

    @capy-ai
    Contributor

    Believed fixed on main (v1.91.13.0): every spawn site in browse/src now passes windowsHide (enforced by the browse/test/windows-spawn-hide.test.ts sweep, passing on the Windows CI lane), the Node polyfill forwards it, and the terminal-agent watchdog fix with browse/test/terminal-agent-watchdog.test.ts shipped in v1.67.0.0. We can't prove console suppression or agent survival on a real Windows desktop from CI. Could you confirm on v1.91.13.0 or later? We'll close once you do, or reopen the investigation if it still happens.

  2. capy-ai commented on Oct 8, 2026

    @capy-ai
    Contributor

    Thanks for the very thorough diagnosis. The watchdog was reworked from v1.67 onward: it uses an identity-based agent record, isProcessAlive uses signal 0, and the guard window scales with the tick, so it should no longer respawn-loop or leak bun.exe. We only have static tests on Linux, so a Windows confirmation would help.

    To confirm, upgrade with /gstack-upgrade (or git pull && ./setup) to v1.91.34.0 or later, retry, and reply here with the result and the output of ~/.claude/skills/gstack/bin/gstack-doctor. If we don't hear back in 14 days, we'll close this as fixed (reopening is always welcome).

  3. adammartin0087 commented on Oct 23, 2026

    @adammartin0087
    Contributor

    I checked this on Windows 11 (build 26200) with gstack v1.91.182.0, npm-global Bun 1.4.2, and Windows Terminal as the default terminal. It still happens, but the cause is different from the original report.

    The watchdog rework is doing its job. The respawn happens because the agent really does die. spawnTerminalAgent spawns a bare 'bun'. Under the Node polyfill, cross-spawn resolves that to npm's bun.cmd, which runs as cmd.exe /c bun run terminal-agent.ts:

    • windowsHide hides only cmd.exe. The detached cmd.exe has no console, so bun.exe allocates a new visible one. Windows Terminal hosts it (WindowsTerminal.exe -Embedding), and it steals focus.
    • The reservation records cmd.exe's PID. The agent waits 2 seconds for record.pid === process.pid, throws startup record was not confirmed, and exits. The window closes, and 60 seconds later the watchdog respawns it. Each daemon does this 3 times before the guard trips.

    All 9 project browse-server.log files on this machine show respawned by watchdog ×2–3 followed by respawn guard tripped (3 crashes in 300s).

    PR with a fix and tests: #3357 (spawns the resolved bun.exe directly). I've confirmed it on this machine: no window opens, and the agent stays up.

    gstack-doctor
    gstack doctor 1.91.182.0
    ok  install root  claude global current (1.91.182.0)
    ok  bun           1.4.2 at /c/Users/<user>/AppData/Roaming/npm/bun (floor 1.3.3, tested 1.4.2)
    ok  windows bash  C:/Program Files/Git/bin/bash.exe
    ok  browse bundle browse/dist built
    ok  binaries      find-browse, design, pdf, gstack-global-discover launch (4 of 4)
    14 ok, 0 warn, 2 not configured, 0 fail
    
  4. capy-ai commented on Oct 25, 2026

    @capy-ai
    Contributor

    The fix for this landed in #3388 (v1.91.193.0, ported from #3357 by @adammartin0087). With Bun from npm install -g bun, the browse terminal agent now spawns the real bun.exe instead of npm's bun.cmd shim, so it should stop respawning every 60 seconds and stop flashing a console. @adammartin0087, could you confirm on your Windows 11 setup after upgrading? We'll close this once you do.

  5. adammartin0087 commented on Oct 25, 2026

    @adammartin0087
    Contributor

    Confirmed on Windows 11 (26200), gstack v1.91.211.0, npm-global Bun 1.4.2, Windows Terminal as the default terminal. The terminal agent spawns as bun.exe directly, and the recorded PID matches the agent. Over 4 minutes and three watchdog ticks, no console window opened or took focus, the agent stayed alive, and there was only one respawn with no loop or guard trip. Page state held across separate calls. Fine to close.

    One minor leftover, as noted in #3357: on cold start, the first agent spawned by the CLI still dies once, and the watchdog's replacement survives. It's silent now and only delays the agent by up to 60s after a daemon starts.

  6. added
    S2
    on Oct 31, 2026
  7. capy-ai commented on Oct 31, 2026

    @capy-ai
    Contributor

    Thanks for the fix in #3357 and for confirming on Windows. #3388 (v1.91.193.0) now spawns the real bun.exe, and you verified it holds. Closing; please reopen or file the cold-start respawn separately if it matters.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    S2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions