feat(elasticsearch): add close(), aclose(), and context manager support to ElasticsearchDocumentStore#3523
Open
chakshu-dhannawat wants to merge 2 commits into
Conversation
ElasticsearchDocumentStore holds open HTTP connection pools (urllib3 for the sync client, aiohttp for the async client). Without explicit cleanup, these leak in long-running apps and test suites. Add: - close() — closes both clients synchronously - aclose() — closes async client with await, sync client inline - __enter__ / __exit__ — sync context manager (calls close) - __aenter__ / __aexit__ — async context manager (calls aclose) All four are no-ops when called before the clients are initialized. Closes deepset-ai#1628
…utine In sync close(), AsyncElasticsearch.close() is a coroutine and calling it without await creates an unused coroutine (mypy unused-coroutine error). close() now only closes the sync client; aclose() closes both. Updated test to reflect the correct behavior.
Contributor
Coverage report (elasticsearch)Click to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #1628.
ElasticsearchDocumentStoreholds open HTTP connection pools (urllib3for the syncElasticsearchclient,aiohttpfor the asyncAsyncElasticsearchclient). Without explicit cleanup, connections leak in long-running services and test suites — especially the async transport which owns anaiohttp.ClientSession.This PR adds:
close()aclose()await, sync client inline__enter__/__exit__close)__aenter__/__aexit__aclose)All four are no-ops when called before initialization (clients are
None).Usage after this PR:
Test plan
Three new unit tests (no ES cluster needed):
test_close_calls_client_close— verifiesclose()calls.close()on both mock clientstest_close_before_init_is_safe— verifies no error when closing before initializationtest_context_manager_calls_close— verifies__exit__calls close via the sync context manager