fix(query): add regression tests for cross-task cancel-scope error during async-generator teardown#1009
Open
Oxygen56 wants to merge 1 commit into
Open
Conversation
…scope error
Query.close() previously used anyio.TaskGroup which raised
RuntimeError("Attempted to exit cancel scope in a different task")
when close() was called from a different task during async-generator
teardown (GeneratorExit). This was fixed by PRs anthropics#746 and anthropics#870, which
replaced the task group with backend-agnostic spawn_detached()
from _task_compat.py.
This PR adds targeted regression tests that reproduce the exact
pattern from the issue:
- An async generator wrapping the message stream calls close() in
its `finally` block, matching the pattern in _process_query_inner
- The consumer breaks the loop, triggering GeneratorExit which
runs the finally block on a (potentially) different task
- Verifies that no RuntimeError is raised on either backend
Closes anthropics#983.
Co-Authored-By: Claude Opus 4.8 <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
Closes #983.
Background
Query.close()previously usedanyio.TaskGroupwhich raisedRuntimeError("Attempted to exit cancel scope in a different task")whenclose()was called from a different task during async-generator teardown (GeneratorExit). This occurred because anyio cancel scopes have task affinity:__aenter__and__aexit__must run on the same task.The root cause was fixed by earlier PRs:
f39ebeb) -- Replacedanyio.TaskGroupwithasyncio.create_task()to avoid cancel-scope task affinity entirely.62c8bfd) -- Refactored into backend-agnosticspawn_detached()in_task_compat.py, restoring trio compatibility.This PR
Adds targeted regression tests that reproduce the exact pattern from issue #983:
close()in itsfinallyblock, matching the pattern in_process_query_innerGeneratorExitwhich runs thefinallyblock on a (potentially) different taskRuntimeErroris raised on either asyncio or trio backendsTesting
```
tests/test_query.py::TestQueryCrossTaskCleanup::test_close_from_different_task_does_not_raise PASSED
tests/test_query.py::TestQueryCrossTaskCleanup::test_close_from_same_task_still_works PASSED
tests/test_query.py::TestQueryCrossTaskCleanup::test_close_during_generator_teardown_asyncio PASSED
tests/test_query.py::TestQueryCrossTaskCleanup::test_close_during_generator_teardown_trio PASSED
```
All 47 tests in `test_task_compat.py` and `test_query.py` pass.