Skip to content

Commit

Permalink
Fixed current_effective_deadline() for shielded cancellation scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed May 4, 2019
1 parent 1b18920 commit 2b52ee3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
5 changes: 4 additions & 1 deletion anyio/_backends/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,10 @@ async def current_effective_deadline():
cancel_scope = _task_states[current_task()].cancel_scope
while cancel_scope:
deadline = min(deadline, cancel_scope.deadline)
cancel_scope = cancel_scope._parent_scope
if cancel_scope.shield:
break
else:
cancel_scope = cancel_scope._parent_scope

return deadline

Expand Down
5 changes: 4 additions & 1 deletion anyio/_backends/curio.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ async def current_effective_deadline():
cancel_scope = _task_states[await curio.current_task()].cancel_scope
while cancel_scope:
deadline = min(deadline, cancel_scope.deadline)
cancel_scope = cancel_scope._parent_scope
if cancel_scope.shield:
break
else:
cancel_scope = cancel_scope._parent_scope

return deadline

Expand Down
2 changes: 2 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ This library adheres to `Semantic Versioning <http://semver.org/>`_.

- Fixed some corner cases of cancellation where behavior on asyncio and curio did not match with
that of trio. Thanks to Joshua Oreman for help with this.
- Fixed ``current_effective_deadline()`` not taking shielded cancellation scopes into account on
asyncio and curio
- Added the ``current_time()`` function

**1.0.0rc1**
Expand Down
10 changes: 9 additions & 1 deletion tests/test_taskgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from anyio import (
create_task_group, sleep, move_on_after, fail_after, open_cancel_scope, wait_all_tasks_blocked,
current_effective_deadline)
current_effective_deadline, current_time)
from anyio._backends import asyncio
from anyio.exceptions import ExceptionGroup

Expand Down Expand Up @@ -393,3 +393,11 @@ async def parent(tg):
async with create_task_group() as tg:
await tg.spawn(parent, tg)
await tg.cancel_scope.cancel()


@pytest.mark.anyio
async def test_shielded_deadline():
async with move_on_after(10):
async with open_cancel_scope(shield=True):
async with move_on_after(1000):
assert await current_effective_deadline() - await current_time() > 900

0 comments on commit 2b52ee3

Please sign in to comment.