Skip to content
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

close_session: clean up loop handling #606

Merged
merged 2 commits into from
Feb 9, 2024

Conversation

pgcamus
Copy link
Contributor

@pgcamus pgcamus commented Feb 8, 2024

Previously we were potentially running cleanup on a different
event loop, even if the session's associated event loop was
active. This manifested in RuntimeError instances being raised
when synchronous cleanup was attempted. Change cleanup to
always use the session's associated event loop and add some
logging when things have gone wrong.

- Previously we were attemting syncronous cleanup on the current
  thread's event loop (even if it wasn't active, having been just
  created above). Instead, retain our `loop` parameter and use it to
  perform synchronous cleanup.
gcsfs/core.py Outdated
Comment on lines 338 to 346
loop = asyncio.get_event_loop()
loop.create_task(session.close())
current_loop = asyncio.get_event_loop()
current_loop.create_task(session.close())
Copy link
Member

Choose a reason for hiding this comment

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

So the difference is, that loop in sync() below is still the original. I don't see how this makes any difference, because the only way to avoid the first return is if get_event_loop() fails, and so no assign happens. Do you find there could be a RuntimeError in create_task() ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah. From what I can tell

  1. The GC weakref callback runs in the main thread.
  2. The main thread has no event loop, so when we call get_event_loop() it fails because it creates a new one which has never been started.
  3. create_task() then fails because it refuses to create a task on a closed loop, and BaseEventLoop._check_closed() raises the exception.

With this fix, we don't see the exception anymore, but we do see the following, which is related but I don't think is caused by this change.

RuntimeWarning: coroutine 'ClientSession.close' was never awaited

gcsfs/core.py Outdated Show resolved Hide resolved
Co-authored-by: Martin Durant <[email protected]>
@pgcamus pgcamus requested a review from martindurant February 9, 2024 14:47
@martindurant martindurant merged commit d615109 into fsspec:main Feb 9, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants