Windows: watchdog self-kill must os._exit, not no-op sys.exit from a bg thread#51
Open
danielhertz1999-bit wants to merge 2 commits into
Open
Conversation
The liveness watchdog runs in a background daemon thread (name="iai-liveness-watchdog"). When it decides to kill a wedged daemon, _self_kill() branches on hasattr(signal, "SIGKILL"): POSIX sends SIGKILL to its own pid (kills the whole process), but the Windows fallback called sys.exit(1) -- which only raises SystemExit in the calling thread. From a background thread that terminates the thread, not the process, so the wedged daemon kept running. Task Scheduler's IgnoreNew instance policy then treated the task as still "Running" and silently refused to start a replacement, so the daemon stayed down until a manual taskkill. Use os._exit(1) on the no-SIGKILL path: it terminates the whole process immediately from any thread and bypasses interpreter shutdown, which could otherwise hang on the same wedge the watchdog is trying to escape. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The existing _self_kill tests are all gated on @_REQUIRES_SIGKILL, so they skip on Windows -- which is exactly how the no-op sys.exit path shipped uncaught. The new test simulates the no-SIGKILL branch (so it runs on every platform) and asserts _self_kill calls os._exit, never sys.exit. Verified it fails against the pre-fix source. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
17 tasks
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
The liveness watchdog can detect a wedged daemon but fails to actually kill it on Windows, so the daemon stays down until a manual
taskkill.The watchdog runs in a background daemon thread (
name="iai-liveness-watchdog",daemon/__init__.py). When it decides to kill,_self_kill()indaemon/_watchdog.pybranches onhasattr(signal, "SIGKILL"):On Windows there is no
SIGKILL, so it falls tosys.exit(1)— which only raisesSystemExitin the calling thread. From the background watchdog thread that unwinds the thread, not the process, so the wedged daemon keeps running. Task Scheduler's<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>then treats the task as still "Running" and silently refuses to start a replacement, so the daemon stays down until someone kills the zombie by hand.Observed in the wild: watchdog logs
daemon_wedge_kill pid=<pid>(intent recorded) but the process with that pid is still alive minutes later.Fix
Use
os._exit(1)on the no-SIGKILLpath. It terminates the whole process immediately from any thread and bypasses interpreter shutdown — which is what you want here, since normal cleanup could itself hang on the same wedge the watchdog is escaping. POSIX behavior (SIGKILL) is unchanged.Test
The three existing
_self_killtests are all gated on@_REQUIRES_SIGKILL, so they skip on Windows — which is precisely why the no-op path shipped uncaught. The new test simulates the no-SIGKILLbranch (viamonkeypatch.delattr(signal, "SIGKILL")) so it runs on every platform, and asserts_self_killcallsos._exitand neversys.exit/os.kill. Verified both ways: passes on the fix, fails against the pre-fix source withFailed: sys.exit is a no-op from the watchdog thread.Windows-only behavior change; POSIX paths are unaffected.