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

create new session if refresh is True #939

Merged
merged 4 commits into from
Feb 18, 2025

Conversation

hutch3232
Copy link
Contributor

@hutch3232 hutch3232 commented Feb 11, 2025

Copy link
Member

@martindurant martindurant left a comment

Choose a reason for hiding this comment

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

I have a couple of small comments on wording.

On the kwargs: this is probably a leftover from previous code. It could be removed, but let's not do that in this PR.

s3fs/core.py Outdated
Examples
--------
>>> s3 = S3FileSystem(profile="<profile name>") # doctest: +SKIP
>>> s3.set_session() # doctest: +SKIP
Copy link
Member

Choose a reason for hiding this comment

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

This method is actually async, so you would only call it with await within a coroutine, as happens elsewhere in the class. The exact alias is _connect (with the underscore), whereas connect (without the underscore) is the blocking version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the clarifications. I haven't directly worked with async before and overlooked that. Let me know if you have additional suggestions for my edits.

s3fs/core.py Outdated
@@ -480,9 +480,26 @@ def _prepare_config_kwargs(self):

async def set_session(self, refresh=False, kwargs={}):
"""Establish S3 connection object.

``connect`` is an alias of ``set_session``. This method called with
Copy link
Member

Choose a reason for hiding this comment

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

This method is called by any operation on an s3fs instance.

You might call this directly from async code, so that you can assign the client object to a local variable
(^ this is implied in the documentation on async, but should no longer be important for most people).

@martindurant
Copy link
Member

I suppose a test would be nice for completeness: call connect(), and assert that the ._s3 and .session attributes have both changed. I wonder if this will cause warnings that those resources weren't closed, or if garbage collection will take care of it.

@hutch3232
Copy link
Contributor Author

hutch3232 commented Feb 17, 2025

I wonder if this will cause warnings that those resources weren't closed, or if garbage collection will take care of it.

I added a test that s3 and session has changed but I'm not too sure how to test that the resources are closed.

This line seems to be working:

weakref.finalize(self, self.close_session, self.loop, self._s3creator)

I (locally) added a print message in here:

print(f"closing session for {s3}!!!\n")

s3fs/s3fs/core.py

Lines 566 to 567 in 744d9c6

def close_session(loop, s3):
if loop is not None and loop.is_running():

And when I run pytest it printed:

closing session for <aiobotocore.session.ClientCreatorContext object at 0x7f2f21b4dd00>!!!

closing session for <aiobotocore.session.ClientCreatorContext object at 0x7f2f22c0d1c0>!!!

@martindurant
Copy link
Member

I (locally) added a print message

That will do, thank you.

If they were not getting closed, it would result in warnings at the end of the test run. I did a merge to main, to make sure all tests would run and will check for warnings when done.

@martindurant
Copy link
Member

There is one warning of the correct type, but because this is involving zarr, I don't think this PR is implicated. Will merge.

s3fs/tests/test_s3fs.py::test_with_xzarr
  /usr/share/miniconda/envs/test/lib/python3.10/asyncio/base_events.py:674: RuntimeWarning: coroutine 'ClientCreatorContext.__aexit__' was never awaited
    self._ready.clear()

@martindurant martindurant merged commit b4d3fa8 into fsspec:main Feb 18, 2025
21 checks passed
@hutch3232 hutch3232 deleted the session_refresh branch February 18, 2025 15:07
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