diff --git a/Lib/asyncio/tasks.py b/Lib/asyncio/tasks.py index c32fc4af34e6df..782d1194c1be44 100644 --- a/Lib/asyncio/tasks.py +++ b/Lib/asyncio/tasks.py @@ -273,6 +273,7 @@ def __eager_start(self): finally: if self.done(): self._coro = None + self = None # Needed to break cycles when an exception occurs. else: _register_task(self) @@ -365,6 +366,8 @@ def __step_run_and_handle_result(self, exc): new_exc = RuntimeError(f'Task got bad yield: {result!r}') self._loop.call_soon( self.__step, new_exc, context=self._context) + finally: + self = None # Needed to break cycles when an exception occurs. def __wakeup(self, future): try: