bpo-34769: Thread safety for _asyncgen_finalizer_hook(). (GH-9716)
(cherry picked from commit c880ffe7d2ce2fedb1831918c8a36e3623e0fb76)
Co-authored-by: twisteroid ambassador <twisteroidambassador@users.noreply.github.com>
diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
index a79e123..b3b0755 100644
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -471,10 +471,7 @@
def _asyncgen_finalizer_hook(self, agen):
self._asyncgens.discard(agen)
if not self.is_closed():
- self.create_task(agen.aclose())
- # Wake up the loop if the finalizer was called from
- # a different thread.
- self._write_to_self()
+ self.call_soon_threadsafe(self.create_task, agen.aclose())
def _asyncgen_firstiter_hook(self, agen):
if self._asyncgens_shutdown_called: