-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
asyncio.TaskGroup does not close unawaited coroutines #115957
Comments
I'm told |
I see. Reproduced and understood. The issue is in this code at the top of cpython/Lib/asyncio/taskgroups.py Lines 156 to 161 in 6087315
If we inserted If the It's also the case that if someone squirrels away a reference to the coroutine object, they can tell that it hasn't been closed (it's tricky, you basically have to try to call its So I think this is a reasonable fix, but I wouldn't want to backport it to 3.12 or before. Would you be interested in submitting a PR? The fix itself is trivial, but it would be nice if there was a test for the new behavior (this can be adapted from your example, using Regarding |
Sorry for jumping in, but this looks quite relevant to what I'm working on, so I had a go at implementing the simple change, in #116009, and modified the tests accordingly. |
Bug report
Bug description:
Consider the following code:
This gives the following output
Arguably, when you call
tg.create_task()
on a task group that is shutting down or has finished, the calling code "knows" about the error because it gets aRuntimeError
exception (as you can see above), so there is no need to get a warning about a coroutine that was not awaited. So, when aTaskGroup
encounters this situation, it should close the coroutine before raising the error.The other argument would be that this still represents a design mistake so should still get the warning. I can see both points of view but I'm raising this issue so a conscious decision can be made.
For comparison, when you do this on a Trio Nursery or AnyIO TaskGroup that has already closed, a coroutine never even gets created in the first place, because you use a different syntax (
nursery.start_soon(foo, 1, 2)
rather thantg.create_task(foo(1, 2))
), so it's a lot like if asyncio were to close the coroutine. The situation is a bit different for a nursery that is shutting down: then it runs till the first (unshielded)await
and is cancelled at that point, which is possible because they use level-based cancellation rather than edge-based cancellation.CPython versions tested on:
3.12
Operating systems tested on:
Windows
Linked PRs
The text was updated successfully, but these errors were encountered: