Skip to content
Draft
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
12 changes: 8 additions & 4 deletions Lib/asyncio/timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ async def __aenter__(self) -> "Timeout":
self._state = _State.ENTERED
self._task = task
self._cancelling = self._task.cancelling()
self._must_cancel = self._task._must_cancel
Copy link
Author

@okhaliavka okhaliavka Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel uneasy messing around with this internal _must_cancel but i didn't find any other way to fix this. changing the cancellation counting logic breaks timeout usage while handling CancelledError, where it should indeed disregard the previous cancellation (the one it's handling). I couldn't get both cases to work by only relying on counting (cancelling).

so i needed something more "surgical" to be able to tell whether we're currently handling CancelledError or we're just in an already cancelled task that didn't get thrown a CancelledError yet

self.reschedule(self._when)
return self

Expand All @@ -106,10 +107,13 @@ async def __aexit__(

if self._state is _State.EXPIRING:
self._state = _State.EXPIRED

if self._task.uncancel() <= self._cancelling and exc_type is not None:
# Since there are no new cancel requests, we're
# handling this.
if (
self._task.uncancel() <= self._cancelling
and exc_type is not None
and not self._must_cancel
):
# Since there are no new cancel requests
# and the task doesn't _have to_ raise CancelledError, we're handling this.
if issubclass(exc_type, exceptions.CancelledError):
raise TimeoutError from exc_val
elif exc_val is not None:
Expand Down
15 changes: 14 additions & 1 deletion Lib/test/test_asyncio/test_timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import time

import asyncio
from asyncio import CancelledError

from test.test_asyncio.utils import await_without_task

Expand Down Expand Up @@ -298,6 +299,18 @@ async def test_nested_timeout_in_finally(self):
self.assertIs(e2.__context__, e22)

async def test_timeout_after_cancellation(self):
asyncio.current_task().cancel()
with self.assertRaises(asyncio.CancelledError):
async with asyncio.timeout(0.1):
await asyncio.sleep(1)

async def test_timeout_immediately_after_cancellation(self):
asyncio.current_task().cancel()
with self.assertRaises(asyncio.CancelledError):
async with asyncio.timeout(0.0):
await asyncio.sleep(1)
Comment on lines +307 to +311
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this added test fails on all python versions and the proposed fix makes it pass without breaking any other tests


async def test_timeout_while_handling_cancellation(self):
try:
asyncio.current_task().cancel()
await asyncio.sleep(1) # work which will be cancelled
Expand All @@ -308,7 +321,7 @@ async def test_timeout_after_cancellation(self):
async with asyncio.timeout(0.0):
await asyncio.sleep(1) # some cleanup

async def test_cancel_in_timeout_after_cancellation(self):
async def test_cancel_in_timeout_while_handling_cancellation(self):
try:
asyncio.current_task().cancel()
await asyncio.sleep(1) # work which will be cancelled
Expand Down
Loading