-
Notifications
You must be signed in to change notification settings - Fork 138
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: odd behaviour with TaskGroup.start()
#517
Comments
I should've made clear - this came up while I was writing an automated test (for aioresult), not because I need it for any real-world situation. Typically the start part of a task is something very quick, like opening a port of listening, so in practice this is perhaps not likely to be a big deal. |
There are corner cases where cancellation might not work as expected, but I'm working to resolve these for AnyIO 4.0. It's a daunting task (cancellation is hard to get right), so I'm not sure when it's going to be released. |
"cancellation is hard to get right" I can imagine it is! And this is an especially obscure case, and doesn't even actually affect me. I'm happy to choose this of you like, I just wanted to let you know about it. |
I investigated this today, and the main task ( |
While I wasn't able to figure out a proper fix yet, I've at least added a minimal reproducing test to the test suite. |
I did in fact figure it out! @arthur-tacca would you mind reviewing the PR? |
Consider the following code:
sleep_and_raise
raises an exception, which propagates intostart_tg
(becausetask_status.started()
hasn't been called yet, so the task is still runningstart_tg
). This happens after 3 seconds, sosleep_only()
(which is also still running understart_tg
) gets cancelled.Except,
sleep_only()
doesn't get cancelled, and the whole thing waits for it to complete. At that point, the exception is noticed and the exception gets raised.If you add
await anyio.sleep(0)
immediately beforeprint("there")
then a cancellation happens then instead, so it's not a matter of having to reach the end of the task group context manager.Changing
await run_tg.start(sleep_only)
toawait start_tg.start(sleep_only)
also fixes the problem.The text was updated successfully, but these errors were encountered: