fix(orphan-killer): SCHED_IDLE + scan deadline to prevent health-probe starvation (#808) - #811
Merged
Conversation
…e starvation (#808) The orphan-killer daemon thread could spin at 100% CPU for ~15 minutes when a D-state process delayed readlink() in the /proc scan. On 1-CPU containers this starved the uvicorn event loop, causing health probes to time out and the circuit breaker to open. Two complementary fixes: 1. `_set_idle_priority()`: sets the daemon thread to SCHED_IDLE (Linux) or nice(19) (macOS/other POSIX) so it only runs when no higher-priority thread wants the CPU — health probes and reader threads win. 2. `_scan_deadline` (default _ORPHAN_SCAN_WALL_SECONDS=8): per-iteration monotonic check that aborts the /proc loop once the budget expires, bounding total CPU time regardless of D-state delays. 8s is safely inside the existing 11s asyncio.wait_for ceiling. Updates test mock signature (_scan_deadline=None) and adds: - TestKillOrphanPipeWriters.test_scan_deadline_stops_scan_early - TestSetIdlePriority (does_not_raise, idempotent) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <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.
Summary
os.readlink()in the/procscan. On 1-CPU containers this starved the uvicorn event loop, causing health probes to time out and the circuit breaker to open._set_idle_priority(): sets the daemon thread toSCHED_IDLE(Linux) ornice(19)(macOS/POSIX) so it only runs when no higher-priority thread wants the CPU — uvicorn health probes win even while the scan is looping._scan_deadline: per-iterationtime.monotonic()check with an 8-second budget (_ORPHAN_SCAN_WALL_SECONDS) aborts the/procloop once the budget expires, bounding total scan time regardless of D-state delays. 8s is inside the existing 11sasyncio.wait_forceiling.Root cause
Previous PRs (#650, #730, #747) bounded waiting behavior (how long
drain_reader_threadsblocks the caller) but the daemon thread itself was never stopped — it continued spinning at default priority for up to ~917 seconds. On a 1-CPU container, a thread at 100% CPU prevents all other runnable threads (including uvicorn's event loop) from running, causing health probe timeouts and circuit breaker activation (#631).Changes
docker/base-image/agent_server/utils/subprocess_pgroup.py:_ORPHAN_SCAN_WALL_SECONDS = 8— budget constant_set_idle_priority()— SCHED_IDLE/nice(19), no-op on unsupported platforms_kill_orphan_pipe_writers()— added_scan_deadline: Optional[float] = Nonekwarg + per-iteration deadline check_run_orphan_killer()— calls_set_idle_priority()and passesscan_deadlinetests/unit/test_subprocess_pgroup.py:_slow_orphan_killer(fd, our_pgid, _scan_deadline=None)TestKillOrphanPipeWriters.test_scan_deadline_stops_scan_early(Linux-only, verifies expired deadline aborts scan immediately)TestSetIdlePriority(does_not_raise, idempotent — runs on all platforms)Test Plan
pytest tests/unit/test_subprocess_pgroup.py -v— 14 passed, 5 skipped (Linux-only on macOS)_scan_deadlineis keyword-only with a default ofNone(backward compatible)Fixes #808
🤖 Generated with Claude Code