From ee17cbd807ba976491e4c657be8aa9b9a29fe059 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Sat, 31 Aug 2024 12:06:25 -0700 Subject: _safe_loop: Discard wrapped asyncio.run loop that was closed Since commit cb0c09d8cecb, _get_running_loop can wrap loops from asyncio.run, so these loops need to be discarded if they've been closed. Fixes: cb0c09d8cecb ("Support coroutine exitfuncs for non-main loops") Bug: https://bugs.gentoo.org/938761 Bug: https://bugs.gentoo.org/761538 Signed-off-by: Zac Medico --- lib/portage/util/futures/_asyncio/__init__.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/portage/util/futures/_asyncio/__init__.py b/lib/portage/util/futures/_asyncio/__init__.py index bdacda59c..23c664e76 100644 --- a/lib/portage/util/futures/_asyncio/__init__.py +++ b/lib/portage/util/futures/_asyncio/__init__.py @@ -311,6 +311,13 @@ def _safe_loop(create: Optional[bool] = True) -> Optional[_AsyncioEventLoop]: _thread_weakrefs.loops = weakref.WeakValueDictionary() try: loop = _thread_weakrefs.loops[thread_key] + if loop.is_closed(): + # Discard wrapped asyncio.run loop that was closed. + del _thread_weakrefs.loops[thread_key] + if loop is _thread_weakrefs.mainloop: + _thread_weakrefs.mainloop = None + loop = None + raise KeyError(thread_key) except KeyError: if not create: return None -- cgit v1.2.3-65-gdbad