From d6151095e6e0813853c64075dd936fc2f4525768 Mon Sep 17 00:00:00 2001 From: "Peter Griess (Camus)" Date: Fri, 9 Feb 2024 08:52:44 -0600 Subject: [PATCH] close_session: clean up loop handling (#606) Co-authored-by: Martin Durant --- gcsfs/core.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/gcsfs/core.py b/gcsfs/core.py index 59008976..f425afc0 100644 --- a/gcsfs/core.py +++ b/gcsfs/core.py @@ -330,16 +330,24 @@ def base(self): def project(self): return self.credentials.project + # Clean up the aiohttp session + # + # This can run from the main thread if invoked via the weakref callbcak. + # This can happen even if the `loop` parameter belongs to another thread + # (e.g. the fsspec IO worker). The control flow here is intended to attempt + # in-thread asynchronous cleanup first, then fallback to synchronous + # cleanup (which can handle cross-thread calls). @staticmethod def close_session(loop, session): if loop is not None and session is not None: if loop.is_running(): try: - loop = asyncio.get_event_loop() - loop.create_task(session.close()) + current_loop = asyncio.get_running_loop() + current_loop.create_task(session.close()) return except RuntimeError: pass + try: sync(loop, session.close, timeout=0.1) except fsspec.FSTimeoutError: