Skip to content

Commit

Permalink
_safe_loop: Discard wrapped asyncio.run loop that was closed
Browse files Browse the repository at this point in the history
Since commit cb0c09d, _get_running_loop can wrap loops from
asyncio.run, so these loops need to be discarded if they've been
closed.

Fixes: cb0c09d ("Support coroutine exitfuncs for non-main loops")
Bug: https://bugs.gentoo.org/938761
Bug: https://bugs.gentoo.org/761538
Signed-off-by: Zac Medico <zmedico@gentoo.org>
  • Loading branch information
zmedico committed Aug 31, 2024
1 parent 747b2d2 commit ee17cbd
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/portage/util/futures/_asyncio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ee17cbd

Please sign in to comment.