mirror of
https://github.com/python/cpython.git
synced 2025-08-04 17:08:35 +00:00
gh-102780: Fix uncancel() call in asyncio timeouts (GH-102815)
Also use `raise TimeOut from <CancelledError instance>` so that the CancelledError is set
in the `__cause__` field rather than in the `__context__` field.
(cherry picked from commit 04adf2df39
)
Co-authored-by: Kristján Valur Jónsson <sweskman@gmail.com>
Co-authored-by: Guido van Rossum <gvanrossum@gmail.com>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
f1b96737a4
commit
a9ece4a839
4 changed files with 50 additions and 6 deletions
|
@ -84,6 +84,7 @@ class Timeout:
|
|||
async def __aenter__(self) -> "Timeout":
|
||||
self._state = _State.ENTERED
|
||||
self._task = tasks.current_task()
|
||||
self._cancelling = self._task.cancelling()
|
||||
if self._task is None:
|
||||
raise RuntimeError("Timeout should be used inside a task")
|
||||
self.reschedule(self._when)
|
||||
|
@ -104,10 +105,10 @@ class Timeout:
|
|||
if self._state is _State.EXPIRING:
|
||||
self._state = _State.EXPIRED
|
||||
|
||||
if self._task.uncancel() == 0 and exc_type is exceptions.CancelledError:
|
||||
# Since there are no outstanding cancel requests, we're
|
||||
if self._task.uncancel() <= self._cancelling and exc_type is exceptions.CancelledError:
|
||||
# Since there are no new cancel requests, we're
|
||||
# handling this.
|
||||
raise TimeoutError
|
||||
raise TimeoutError from exc_val
|
||||
elif self._state is _State.ENTERED:
|
||||
self._state = _State.EXITED
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue