Skip to content

Commit

Permalink
Fixed CancelScope's timeout when entering the context (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
francis-clairicia authored Oct 2, 2023
1 parent a57534e commit d609d8f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/easynetwork_asyncio/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def __init__(self, *, deadline: float = math.inf) -> None:
self.__cancel_called: bool = False
self.__cancelled_caught: bool = False
self.__deadline: float = math.inf
self.__timeout_handle: asyncio.TimerHandle | None = None
self.__timeout_handle: asyncio.Handle | None = None
self.reschedule(deadline)

def __repr__(self) -> str:
Expand Down Expand Up @@ -278,9 +278,9 @@ def __timeout(self) -> None:
if self.__deadline != math.inf:
loop = asyncio.get_running_loop()
if loop.time() >= self.__deadline:
self.cancel()
self.__timeout_handle = loop.call_soon(self.cancel)
else:
self.__timeout_handle = loop.call_at(self.__deadline, self.__timeout)
self.__timeout_handle = loop.call_at(self.__deadline, self.cancel)

@classmethod
def _current_task_scope(cls, task: asyncio.Task[Any]) -> CancelScope | None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,6 @@ async def coroutine() -> None:

await event_loop.create_task(coroutine())

@pytest.mark.xfail(raises=asyncio.CancelledError, reason="Task.cancel() cannot be erased", strict=True)
async def test____cancel_shielded_coroutine____scope_cancellation_edge_case_3(
self,
event_loop: asyncio.AbstractEventLoop,
Expand All @@ -1255,12 +1254,13 @@ async def coroutine() -> None:

await backend.coro_yield()

assert inner_scope.cancel_called()
assert not inner_scope.cancel_called()

assert not inner_scope.cancelled_caught()

await event_loop.create_task(coroutine())

@pytest.mark.xfail(raises=asyncio.CancelledError, reason="Task.cancel() cannot be erased", strict=True)
async def test____cancel_shielded_coroutine____scope_cancellation_edge_case_4(
self,
event_loop: asyncio.AbstractEventLoop,
Expand All @@ -1270,6 +1270,26 @@ async def coroutine() -> None:
current_task = asyncio.current_task()
assert current_task is not None

with backend.open_cancel_scope() as inner_scope:
inner_scope.cancel()

await backend.coro_yield()

assert inner_scope.cancel_called()

assert not inner_scope.cancelled_caught()

await event_loop.create_task(coroutine())

async def test____cancel_shielded_coroutine____scope_cancellation_edge_case_5(
self,
event_loop: asyncio.AbstractEventLoop,
backend: AsyncIOBackend,
) -> None:
async def coroutine() -> None:
current_task = asyncio.current_task()
assert current_task is not None

outer_scope = backend.open_cancel_scope()
inner_scope = backend.open_cancel_scope()
with outer_scope:
Expand All @@ -1292,7 +1312,7 @@ async def coroutine() -> None:

await event_loop.create_task(coroutine())

async def test____cancel_shielded_coroutine____scope_cancellation_edge_case_5(
async def test____cancel_shielded_coroutine____scope_cancellation_edge_case_6(
self,
event_loop: asyncio.AbstractEventLoop,
backend: AsyncIOBackend,
Expand Down

0 comments on commit d609d8f

Please sign in to comment.