Skip to content

GH-117536: ensure asyncgens GCd before runner teardown get aclosed #117577

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions Lib/asyncio/base_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,12 @@ def __init__(self):
# A weak set of all asynchronous generators that are
# being iterated by the loop.
self._asyncgens = weakref.WeakSet()
# A strong set of asynchronous generators that are being closed
# by asyncgen_finalizer_hook
self._closing_asyncgens = set()
# A strong set of Handles for callbacks about to schedule async gen
# aclose tasks
self._asyncgens_aclose_handles = set()
# Set to True when `loop.shutdown_asyncgens` is called.
self._asyncgens_shutdown_called = False
# Set to True when `loop.shutdown_default_executor` is called.
Expand Down Expand Up @@ -556,9 +562,15 @@ def _check_default_executor(self):
raise RuntimeError('Executor shutdown has been called')

def _asyncgen_finalizer_hook(self, agen):
self._asyncgens.discard(agen)
self._closing_asyncgens.add(agen)

def do_aclose():
self._closing_asyncgens.discard(agen)
self._asyncgens_aclose_handles.discard(handle)
self.create_task(agen.aclose())

if not self.is_closed():
self.call_soon_threadsafe(self.create_task, agen.aclose())
handle = self.call_soon_threadsafe(do_aclose)

def _asyncgen_firstiter_hook(self, agen):
if self._asyncgens_shutdown_called:
Expand All @@ -573,13 +585,18 @@ async def shutdown_asyncgens(self):
"""Shutdown all active asynchronous generators."""
self._asyncgens_shutdown_called = True

if not len(self._asyncgens):
closing_agens = list(self._asyncgens)
closing_agens.extend(self._closing_asyncgens.copy())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
closing_agens.extend(self._closing_asyncgens.copy())
closing_agens.extend(self._closing_asyncgens)

self._closing_asyncgens.clear()
self._asyncgens.clear()

if not closing_agens:
# If Python version is <3.6 or we don't have any asynchronous
# generators alive.
return

closing_agens = list(self._asyncgens)
self._asyncgens.clear()
for handle in self._asyncgens_aclose_handles.copy():
handle.cancel()

results = await tasks.gather(
*[ag.aclose() for ag in closing_agens],
Expand Down