From f45b7d1a67faa9853d96bd5eed952dabdf1d32ab Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Sun, 13 Oct 2024 10:02:30 +0100 Subject: [PATCH] don't clear host_task from CancelScope if __exit__ is called incorrectly --- src/anyio/_backends/_asyncio.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/anyio/_backends/_asyncio.py b/src/anyio/_backends/_asyncio.py index 8dfceef8..b6b42760 100644 --- a/src/anyio/_backends/_asyncio.py +++ b/src/anyio/_backends/_asyncio.py @@ -426,15 +426,16 @@ def __exit__( exc_tb: TracebackType | None, ) -> bool | None: del exc_tb - try: - if not self._active: - raise RuntimeError("This cancel scope is not active") - if current_task() is not self._host_task: - raise RuntimeError( - "Attempted to exit cancel scope in a different task than it was " - "entered in" - ) + if not self._active: + raise RuntimeError("This cancel scope is not active") + if current_task() is not self._host_task: + raise RuntimeError( + "Attempted to exit cancel scope in a different task than it was " + "entered in" + ) + + try: assert self._host_task is not None host_task_state = _task_states.get(self._host_task) if host_task_state is None or host_task_state.cancel_scope is not self: