-
Notifications
You must be signed in to change notification settings - Fork 141
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
CancelScope.cancel() does not work as expected if called within move_on_after() context #370
Comments
I have some time in my hand and zoomed in further into the issue from anyio import move_on_after, sleep, run, CancelScope
async def main(shield_scope2):
print(f'** {shield_scope2=}')
with CancelScope(shield=False) as scope1:
with CancelScope(shield=shield_scope2) as scope2: ##
with CancelScope(shield=True) as scope3:
scope1.cancel()
scope2.cancel()
print(' -- Cancel completed')
print(f'{scope1.cancel_called=}')
print(f'{scope2.cancel_called=}')
print(f'{scope3.cancel_called=}')
print(f'{scope1.shield=}')
print(f'{scope2.shield=}')
print(f'{scope3.shield=}')
await sleep(0)
print('!! Unshielded')
run(main, True); print()
run(main, False); print() Outcome:
I would expect no difference in behaviour when |
It only prints |
Another curious thing is that it really does need those three levels of cancel scopes to repro the bug. |
I turned this into a regression test that currently fails on asyncio but passes on trio: async def test_triple_nested_shield() -> None:
"""Regression test for #370."""
got_past_checkpoint = False
async def taskfunc() -> None:
nonlocal got_past_checkpoint
with CancelScope() as scope1:
with CancelScope() as scope2:
with CancelScope(shield=True):
scope1.cancel()
scope2.cancel()
await checkpoint()
got_past_checkpoint = True
async with create_task_group() as tg:
tg.start_soon(taskfunc)
assert not got_past_checkpoint |
I think the gist of the issue is in the combination of |
I've released v3.3.2 with the fix for this issue. |
I am using the new release in my project and it is running as expected. Big thank you for your effort. |
I ran this:
And the outcome is:
This came from
win10
,python 3.9.6
,asyncio
,anyio-3.3.1
From my reading of the documentation both main1 and main2 should be equivalent. If that is the case, I would say there is an issue with main2. Thank you.
The text was updated successfully, but these errors were encountered: