8000 Fixed current_effective_deadline()'s -inf disparity on asyncio (#485) · IBMZ-Linux-OSS-Python/anyio@0edcd51 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0edcd51

Browse files
authored
Fixed current_effective_deadline()'s -inf disparity on asyncio (agronholm#485)
1 parent 446e246 commit 0edcd51

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

docs/versionhistory.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
3838
- Fixed ``start_blocking_portal()`` raising an unwarranted
3939
``RuntimeError: This portal is not running`` if a task raises an exception that causes
4040
the event loop to be closed
41+
- Fixed ``current_effective_deadline()`` not returning ``-inf`` on asyncio when the
42+
currently active cancel scope has been cancelled (PR by Ganden Schaffner)
4143

4244
**3.6.1**
4345

src/anyio/_backends/_asyncio.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1876,7 +1876,10 @@ def current_effective_deadline(cls) -> float:
18761876
deadline = math.inf
18771877
while cancel_scope:
18781878
deadline = min(deadline, cancel_scope.deadline)
1879-
if cancel_scope.shield:
1879+
if cancel_scope._cancel_called:
1880+
deadline = -math.inf
1881+
break
1882+
elif cancel_scope.shield:
18801883
break
18811884
else:
18821885
cancel_scope = cancel_scope._parent_scope

src/anyio/_core/_tasks.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ def current_effective_deadline() -> float:
139139
Return the nearest deadline among all the cancel scopes effective for the current
140140
task.
141141
142-
:return: a clock value from the event loop's internal clock (``float('inf')`` if
143-
there is no deadline in effect)
142+
:return: a clock value from the event loop's internal clock (or ``float('inf')`` if
143+
there is no deadline in effect, or ``float('-inf')`` if the current scope has
144+
been cancelled)
144145
:rtype: float
145146
146147
"""

src/anyio/abc/_eventloop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def current_effective_deadline(cls) -> float:
146146
147147
:return:
148148
- a clock value from the event loop's internal clock
149-
- ``'inf`` if there is no deadline in effect
149+
- ``inf`` if there is no deadline in effect
150150
- ``-inf`` if the current scope has been cancelled
151151
:rtype: float
152152
"""

tests/test_taskgroups.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import asyncio
4+
import math
45
import sys
56
import time
67
from collections.abc import AsyncGenerator, Coroutine, Generator
@@ -527,6 +528,9 @@ async def test_cancel_from_shielded_scope() -> None:
527528
with CancelScope(shield=True) as inner_scope:
528529
assert inner_scope.shield
529530
tg.cancel_scope.cancel()
531+
assert current_effective_deadline() == math.inf
532+
533+
assert current_effective_deadline() == -math.inf
530534

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

537541

542+
async def test_cancel_shielded_scope() -> None:
543+
with CancelScope(shield=True) as cancel_scope:
544+
assert cancel_scope.shield
545+
cancel_scope.cancel()
546+
assert current_effective_deadline() == -math.inf
547+
548+
with pytest.raises(get_cancelled_exc_class()):
549+
await sleep(0)
550+
551+
538552
@pytest.mark.parametrize("anyio_backend", ["asyncio"])
539553
async def test_cancel_host_asyncgen() -> None:
540554
done = False

0 commit comments

Comments
 (0)
0