Skip to content

fix: reap claude children without trusting asyncio's child watcher - #16

Merged
chefboyrdave21 merged 3 commits into
mainfrom
fix/slot-leak-reaper
Oct 22, 2026
Merged

chefboyrdave21 merged 3 commits into
mainfrom
fix/slot-leak-reaper

Conversation

@chefboyrdave21

Copy link
Copy Markdown
Owner

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() skips epoll_ctl (event mask 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 the hang; the old _run_claude_cli hangs under the same conditions, holding a slot.

Fix

  • _reap(): asyncio gets 5s, then a blocking waitpid in a thread, and the transport is told.
  • Non-stream path: _drain() replaces communicate() (which ends in the same wait), plus start_new_session so _kill_group works there.
  • 60s watchdog: clears stale selector fds, logging the callback that registered them (the lead to the leaker), and reaps zombie children seen on two passes.
  • /health: slots.free and reaper counters.

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

Lumina and others added 3 commits October 21, 2026 23:12
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>
@chefboyrdave21
chefboyrdave21 merged commit 666cd15 into main Oct 22, 2026
8 checks passed
@chefboyrdave21
chefboyrdave21 deleted the fix/slot-leak-reaper branch October 22, 2026 03:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant