Skip to content

fix anyio 4.13 CPU hot-loop: narrow zombie-scope patch - #237

Open
constkolesnyak wants to merge 1 commit into
ClickHouse:mainfrom
constkolesnyak:upstream/anyio-narrow-fix-v3
Open

fix anyio 4.13 CPU hot-loop: narrow zombie-scope patch#237
constkolesnyak wants to merge 1 commit into
ClickHouse:mainfrom
constkolesnyak:upstream/anyio-narrow-fix-v3

Conversation

@constkolesnyak

Copy link
Copy Markdown
Contributor

What

anyio 4.13 changed the internal _deliver_cancellation mechanism 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_cancellation at import time to:

  1. Skip tasks that are already done (zombie-scope guard)
  2. Narrow the scope so the patch only applies to the specific cancel pattern that triggers the loop

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).

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>
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