diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-10-21 19:48:53 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-21 19:48:53 (GMT) |
commit | 028f47754c78864abf6eb1930493871727925b6a (patch) | |
tree | d60711f8529ac9acf1025780d27e392921b7dad0 /Lib/asyncio/timeouts.py | |
parent | 322f79f5a2c47507b881e868071c078c343d4301 (diff) | |
download | cpython-028f47754c78864abf6eb1930493871727925b6a.zip cpython-028f47754c78864abf6eb1930493871727925b6a.tar.gz cpython-028f47754c78864abf6eb1930493871727925b6a.tar.bz2 |
[3.12] gh-111085: Fix invalid state handling in TaskGroup and Timeout (GH-111111) (GH-111171)
asyncio.TaskGroup and asyncio.Timeout classes now raise proper RuntimeError
if they are improperly used.
* When they are used without entering the context manager.
* When they are used after finishing.
* When the context manager is entered more than once (simultaneously or
sequentially).
* If there is no current task when entering the context manager.
They now remain in a consistent state after an exception is thrown,
so subsequent operations can be performed correctly (if they are allowed).
(cherry picked from commit 6c23635f2b7067ef091a550954e09f8b7c329e3f)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: James Hilton-Balfe <gobot1234yt@gmail.com>
Diffstat (limited to 'Lib/asyncio/timeouts.py')
-rw-r--r-- | Lib/asyncio/timeouts.py | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/Lib/asyncio/timeouts.py b/Lib/asyncio/timeouts.py index 029c468..30042ab 100644 --- a/Lib/asyncio/timeouts.py +++ b/Lib/asyncio/timeouts.py @@ -49,8 +49,9 @@ class Timeout: def reschedule(self, when: Optional[float]) -> None: """Reschedule the timeout.""" - assert self._state is not _State.CREATED if self._state is not _State.ENTERED: + if self._state is _State.CREATED: + raise RuntimeError("Timeout has not been entered") raise RuntimeError( f"Cannot change state of {self._state.value} Timeout", ) @@ -82,11 +83,14 @@ class Timeout: return f"<Timeout [{self._state.value}]{info_str}>" async def __aenter__(self) -> "Timeout": + if self._state is not _State.CREATED: + raise RuntimeError("Timeout has already been entered") + task = tasks.current_task() + if task is None: + raise RuntimeError("Timeout should be used inside a task") self._state = _State.ENTERED - self._task = tasks.current_task() + self._task = task self._cancelling = self._task.cancelling() - if self._task is None: - raise RuntimeError("Timeout should be used inside a task") self.reschedule(self._when) return self |