Skip to content

Commit

Permalink
Fixed current_effective_deadline()'s -inf disparity on asyncio (agron…
Browse files Browse the repository at this point in the history
  • Loading branch information
gschaffner committed Oct 20, 2022
1 parent 446e246 commit 0edcd51
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ This library adheres to `Semantic Versioning 2.0 <http:https://semver.org/>`_.
- Fixed ``start_blocking_portal()`` raising an unwarranted
``RuntimeError: This portal is not running`` if a task raises an exception that causes
the event loop to be closed
- Fixed ``current_effective_deadline()`` not returning ``-inf`` on asyncio when the
currently active cancel scope has been cancelled (PR by Ganden Schaffner)

**3.6.1**

Expand Down
5 changes: 4 additions & 1 deletion src/anyio/_backends/_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1876,7 +1876,10 @@ def current_effective_deadline(cls) -> float:
deadline = math.inf
while cancel_scope:
deadline = min(deadline, cancel_scope.deadline)
if cancel_scope.shield:
if cancel_scope._cancel_called:
deadline = -math.inf
break
elif cancel_scope.shield:
break
else:
cancel_scope = cancel_scope._parent_scope
Expand Down
5 changes: 3 additions & 2 deletions src/anyio/_core/_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ def current_effective_deadline() -> float:
Return the nearest deadline among all the cancel scopes effective for the current
task.
:return: a clock value from the event loop's internal clock (``float('inf')`` if
there is no deadline in effect)
:return: a clock value from the event loop's internal clock (or ``float('inf')`` if
there is no deadline in effect, or ``float('-inf')`` if the current scope has
been cancelled)
:rtype: float
"""
Expand Down
2 changes: 1 addition & 1 deletion src/anyio/abc/_eventloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def current_effective_deadline(cls) -> float:
:return:
- a clock value from the event loop's internal clock
- ``'inf`` if there is no deadline in effect
- ``inf`` if there is no deadline in effect
- ``-inf`` if the current scope has been cancelled
:rtype: float
"""
Expand Down
14 changes: 14 additions & 0 deletions tests/test_taskgroups.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import asyncio
import math
import sys
import time
from collections.abc import AsyncGenerator, Coroutine, Generator
Expand Down Expand Up @@ -527,6 +528,9 @@ async def test_cancel_from_shielded_scope() -> None:
with CancelScope(shield=True) as inner_scope:
assert inner_scope.shield
tg.cancel_scope.cancel()
assert current_effective_deadline() == math.inf

assert current_effective_deadline() == -math.inf

with pytest.raises(get_cancelled_exc_class()):
await sleep(0.01)
Expand All @@ -535,6 +539,16 @@ async def test_cancel_from_shielded_scope() -> None:
await sleep(0.01)


async def test_cancel_shielded_scope() -> None:
with CancelScope(shield=True) as cancel_scope:
assert cancel_scope.shield
cancel_scope.cancel()
assert current_effective_deadline() == -math.inf

with pytest.raises(get_cancelled_exc_class()):
await sleep(0)


@pytest.mark.parametrize("anyio_backend", ["asyncio"])
async def test_cancel_host_asyncgen() -> None:
done = False
Expand Down

0 comments on commit 0edcd51

Please sign in to comment.