Skip to content
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

Fixed CancelScope's timeout when entering the context #134

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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