Skip to content

Commit d615109

Browse files
close_session: clean up loop handling (#606)
Co-authored-by: Martin Durant <[email protected]>
1 parent f526d96 commit d615109

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

gcsfs/core.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,16 +330,24 @@ def base(self):
330330
def project(self):
331331
return self.credentials.project
332332

333+
# Clean up the aiohttp session
334+
#
335+
# This can run from the main thread if invoked via the weakref callbcak.
336+
# This can happen even if the `loop` parameter belongs to another thread
337+
# (e.g. the fsspec IO worker). The control flow here is intended to attempt
338+
# in-thread asynchronous cleanup first, then fallback to synchronous
339+
# cleanup (which can handle cross-thread calls).
333340
@staticmethod
334341
def close_session(loop, session):
335342
if loop is not None and session is not None:
336343
if loop.is_running():
337344
try:
338-
loop = asyncio.get_event_loop()
339-
loop.create_task(session.close())
345+
current_loop = asyncio.get_running_loop()
346+
current_loop.create_task(session.close())
340347
return
341348
except RuntimeError:
342349
pass
350+
343351
try:
344352
sync(loop, session.close, timeout=0.1)
345353
except fsspec.FSTimeoutError:

0 commit comments

Comments
 (0)