Repository navigation
fix: reap claude children without trusting asyncio's child watcher - #16
Merged
Merged
Conversation
Ten <defunct> claude children held every concurrency slot on 2026-10-11 and again on 2026-10-21, failing all claude-* routes with 'no free claude slot within 90s' until a restart. PR #14's hardening did not stop it. Root cause: on Linux, Python 3.12 uses PidfdChildWatcher, which notices a child's exit when its pidfd turns readable in the event loop's selector. A stale selector entry on a reused fd number swallows the pidfd: selectors.modify() skips epoll_ctl when the event mask is unchanged, so the pidfd is never polled, the child stays a zombie and proc.wait() never returns, with the slot held. test_child_reaper.py reproduces it; the old code hangs under the same conditions. - _reap(): asyncio gets 5s, then a blocking waitpid in a thread, and the transport is told so it closes cleanly. - Non-stream path: _drain() instead of communicate() (which ends in the same wait), and start_new_session so _kill_group reaches its children. - 60s watchdog: clears stale selector fds, logging the callback that leaked them, and reaps zombie children seen on two passes. - /health: slots.free and reaper counters. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Fixes the recurring "no free claude slot within 90s" outage (2026-10-11, 2026-10-21): ten
<defunct>claude children held every slot until a restart. PR #14's hardening was loaded and did not stop it.Root cause. Python 3.12 on Linux uses
PidfdChildWatcher: a child's exit is noticed when its pidfd turns readable in the event loop's selector. If a stale selector entry exists on an fd number that the pidfd reuses,selectors.modify()skipsepoll_ctl(event mask unchanged), so the pidfd is never polled. The child stays a zombie andproc.wait()never returns, with the slot held.test_child_reaper.pyreproduces the hang; the old_run_claude_clihangs under the same conditions, holding a slot.Fix
_reap(): asyncio gets 5s, then a blockingwaitpidin a thread, and the transport is told._drain()replacescommunicate()(which ends in the same wait), plusstart_new_sessionso_kill_groupworks there./health:slots.freeandreapercounters.Not yet known: which code leaves the stale registration. The watchdog's log line names the callback the next time it happens.
Tests: 58 passed (7 new).
🤖 Generated with Claude Code