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

Fix memory leak in AsyncClient #457

Merged
merged 5 commits into from
Jan 22, 2025
Merged

Fix memory leak in AsyncClient #457

merged 5 commits into from
Jan 22, 2025

Conversation

pufit
Copy link
Member

@pufit pufit commented Jan 21, 2025

Summary

It's known that ThreadPoolExecutor doesn't deallocate memory without .shutdown being called. Python's GC doesn't stop running thread pools, so the code like

while True:
    client = await get_async_client(...)
    ...
    client.close()

will be leaking memory since each new AsyncClient instance will have a new executor.

Closes #424

Checklist

Delete items not relevant to your PR:

  • Unit and integration tests covering the common scenarios were added
  • A human-readable description of the changes was provided to include in CHANGELOG
  • For significant changes, documentation in https://github.com/ClickHouse/clickhouse-docs was updated with further explanations or tutorials

Copy link
Collaborator

@genzgd genzgd left a comment

Choose a reason for hiding this comment

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

Thanks for digging into this! My only question is whether this can be simpler by just shutting down the executor synchronously. It looks like you added the __aenter__ and __aexit__ methods to ensure this autocloses when using it as an AsyncContext?

@@ -64,11 +64,12 @@ def min_version(self, version_str: str) -> bool:
"""
return self.client.min_version(version_str)

def close(self):
async def close(self):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this have to be async? Can we just call the executor.shutdown method synchronously?

@pufit
Copy link
Member Author

pufit commented Jan 22, 2025

Thanks for digging into this! My only question is whether this can be simpler by just shutting down the executor synchronously. It looks like you added the __aenter__ and __aexit__ methods to ensure this autocloses when using it as an AsyncContext?

This way is more Pythonic. Usually, you open and close clients inside the event loop. If you try to wait for executor shutdown in sync mode it will hang the loop. If you close the thread pool without wait=True, you won't be able to guarantee the graceful shutdown of your application.

Here is an example of how it is usually done in other libraries: https://github.com/aio-libs/aiohttp/blob/master/examples/curl.py#L11

@genzgd genzgd merged commit eec6d2a into ClickHouse:main Jan 22, 2025
33 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.

Memory leaks AsyncClient
2 participants