fix(windows): guard daemons never exit and duplicate — POSIX-only lifecycle probes - #182
Open
vdebellabre wants to merge 3 commits into
Open
vdebellabre wants to merge 3 commits into
vdebellabre wants to merge 3 commits into
Conversation
…de-pid detection, orphan backstop Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…seam tests Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…ing the user's real global hooks Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Author
|
Follow-up commit pushed: the test suite's HOME-only env patching is inert on Windows ( |
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Problem
On Windows, guard daemons never exit and accumulate — 10 to 30
python/pythonwprocesses pile up in the task manager across Claude Desktop restarts, surviving the app itself. Worse, sessions frequently end up with duplicate daemons: two guards for the same session id, spawned seconds apart.Root cause
The daemon lifecycle layer is POSIX-only. Three independent breaks compound on Windows:
find_claude_pid()always returnsNone. It walks ancestry withps -o ppid=,comm=— Git Bash'spsdoesn't support-oat all. With no Claude PID, the daemon's Claude-exit watchdog (its only exit path for a normal-size session) never arms, so every guard polls its transcript forever.os.kill(pid, 0)is not a liveness probe on Windows. Signal 0 isCTRL_C_EVENT, which CPython forwards toGenerateConsoleCtrlEvent— a console-group broadcast whose result depends on console topology, not PID existence. Reproduced: it raisesOSErrorfor a LIVE detached daemon. Every probe built on it (spawn_lock._is_process_alive,reload_lock._is_process_alive,_is_guard_running_for_session,_wait_for_exit,_cleanup_legacy_pid,reload_self_daemon's stale check, doctor's checks) therefore misreads live processes as dead. Concretely:_is_guard_running_for_sessionclassifies a live daemon's pidfile as stale once past the 5s fresh window, unlinks it, and the claim re-spawns → duplicate daemon per session whenever two SessionStart hooks fire >5s apart (reproduced deterministically)._is_cozempic_guard_process/ identity checks areps-based — they error on Windows and fail closed, so even a correct liveness probe would fall into the "alive but not a guard" freshness-unlink branch.Fix
All Windows-gated; POSIX code paths are byte-identical:
helpers._pid_is_alive_windows— native probe viaOpenProcess(PROCESS_QUERY_LIMITED_INFORMATION)+GetExitCodeProcess == STILL_ACTIVE(access-denied → alive, invalid-parameter → dead). No subprocess, no console window.helpers._pid_is_alive,spawn_lock._is_process_alive, andreload_lock._is_process_alivedelegate to it on Windows.guard._kill0_probe— drop-in for the rawos.kill(pid, 0)probes (raisesProcessLookupErrorwhen dead) so the existing except-clauses at all call sites keep working unchanged; wired into_is_guard_running_for_session,_wait_for_exit,_cleanup_legacy_pid,reload_self_daemon, and doctor.session.find_claude_pid— Windows branch walking ancestry over aCreateToolhelp32Snapshotprocess map (ctypes; pure walk extracted as_walk_up_to_claudefor unit testing). It resolves in the SessionStart hook's CLI — a live descendant of Claude — andstart_guard_daemonalready forwards the result via--claude-pid, so the daemon's watchdog now arms.guard._is_cozempic_guard_process_windows— same argv token rules as thepsversion, applied to the command line read natively viaNtQueryInformationProcess(ProcessCommandLineInformation)(quote-aware argv[0] split forProgram Files-style paths). The Claude-side identity check already had atasklistfallback and is unchanged.COZEMPIC_GUARD_ORPHAN_EXIT_SECONDS(default 2h, 0 disables), the daemon checkpoints and exits instead of living forever. Dormant whenever a Claude PID is known — the watchdog owns that case.Verification (Windows 11)
guard --daemoncalls 8s apart → 2 live daemons, second overwrote the first's pidfile. After: second call returnsGuard already running (PID …), pidfile intact.Guard stopping (Claude exited), pidfile unlinked); daemon with no resolvable Claude PID and a stale transcript exits via the orphan backstop (Guard stopping (orphaned …)).tests/test_windows_lifecycle.py: pure-function coverage of the ancestry walk, cmdline split, and env knob (run on all platforms), plus Windows-gated integration tests that reproduce the exact broken topology (detached, console-less child in its own process group) against the new probes.main(same pre-existing platform-specific failures). POSIX paths untouched by construction (os.name == "nt"gates).