Skip to content

gh-134471: Fix asyncio.timeout(0) swallowing an unrelated prior cancellation. #134472

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
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
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)

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