New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cancelling a TaskGroup in which a task is starting all also cancel the TaskGroup in which it is due to run #685
Comments
I suspect the real lesson here is that nobody is using |
#710 contains a simpler repro of the same issue. The gist of the problem is that when an inner cancel scope cancels the |
The simplest test I could come up with is this: async def test_cancel_escape_nested_scope() -> None:
async def in_task_group(task_status: TaskStatus[None]) -> None:
await sleep_forever()
async with create_task_group() as tg:
with CancelScope() as inner_scope:
inner_scope.cancel()
await tg.start(in_task_group)
assert not tg.cancel_scope.cancel_called |
I think this is what Trio's "eventual nursery" internal concept has to do with. AnyIO needs something similar. |
Things to check first
I have searched the existing issues and didn't find my bug already reported there
I have checked that my bug is still present in the latest release
AnyIO version
4.2
Python version
3.10.13
What happened?
This is related to, but slightly different from, issue #517
Consider this sequence of steps with anyio (asyncio backend) in this way:
await outer_tg.start(foo)
in the context of the inner TaskGroup)task_status.started()
and gets reparented to the outer TaskGroup, cancel the inner task group (and therefore this partially started task).If you do all that, then the outer TaskGroup will be cancelled as well, including any other tasks that were running in it. Somehow the partially started task has leaked the cancellation from one TaskGroup to the other.
How can we reproduce the bug?
One example of how this would look:
If using
TaskGroup.start_soon()
onTaskGroup.start()
looks a bit suspicious, another way would be to do cancellation in the background, for example with a time out:Here are definitions of the coroutines used in the examples:
Here is an example output:
The text was updated successfully, but these errors were encountered: