fix anyio 4.13 CPU hot-loop: narrow zombie-scope patch - #237
Open
constkolesnyak wants to merge 1 commit into
Open
fix anyio 4.13 CPU hot-loop: narrow zombie-scope patch#237constkolesnyak wants to merge 1 commit into
constkolesnyak wants to merge 1 commit into
Conversation
Adds a monkey-patch of anyio.CancelScope._deliver_cancellation that fixes a 100% CPU spin observed in production when a done task lingers in CancelScope._tasks longer than upstream's task_done callback takes to prune it. Bug: upstream sets should_retry=True for every task in _tasks before checking whether a cancel can actually be delivered. For a done task, task.cancel() is a no-op but should_retry stays True → the scope re-arms call_soon(_deliver_cancellation) on every event-loop tick. Observed live (April 24, 2026): three simultaneous zombie-scopes in one process, ~55k epoll_pwait/sec combined, 100% CPU on one core, each scope holding a single done task. Fix: insert a single `if task.done(): continue` at the top of the loop body, before should_retry is touched. Every other branch is byte-for-byte upstream anyio 4.13.0 — deferred self-delivery (s.cancel(); await sleep(N)) and re-delivery after a swallowed CancelledError both behave exactly like upstream. Tests (10/10 passing): * test_patch_is_applied / test_apply_is_idempotent — wiring * test_fail_after_still_works / test_move_on_after_still_works / test_task_group_cancellation_still_works — upstream behavior * test_self_cancel_then_await_sleep_cancels_immediately — preserves anyio's deferred self-delivery contract * test_task_cancels_own_taskgroup_scope_then_awaits — same shape via TaskGroup * test_swallowed_cancelled_error_is_redelivered — preserves anyio's re-delivery contract * test_no_hot_loop_when_only_task_is_done — the zombie-scope regression this patch is for * test_live_task_still_reschedules_alongside_done_task — mixed done+live scope still reschedules for the live task The patch is intentionally as narrow as possible. An earlier wider version (skipping current_task and _must_cancel without setting should_retry) was reverted because it broke deferred self-delivery and re-delivery — see the module docstring for the writeup. Co-Authored-By: Claude Opus 4.7 <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.
What
anyio 4.13 changed the internal
_deliver_cancellationmechanism in a way that causes a CPU hot-loop (100% CPU) under certain task-group cancel patterns.This patch (
nerve/_anyio_patch.py) monkey-patches_deliver_cancellationat import time to:Includes comprehensive tests (
tests/test_anyio_patch.py) that verify the patch doesn't regress the normal cancel paths.Why
100% CPU hot-loop causes nerve to become unresponsive. The patch is scoped narrowly to the problem scenario and includes tests for the fix and regression coverage for normal paths.
Rebased on current upstream/main (2026-07-30).