Add close() and context manager support to Client (#87) - #341
Open
yashbudhia wants to merge 1 commit into
Open
Conversation
Client creates a requests.Session on construction and never closed it, so long-running processes and test suites saw ResourceWarning: unclosed socket for every client that went out of scope. Client.close() now closes the session and releases its pooled connections, and Client supports the with statement, closing the session on exit even when the block raises. A session passed in by the caller is closed too, which is documented in the README. Existing code that never closes the client behaves exactly as before. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011ZJQzxtgWvkWobxsDcuLoW
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.
Fixes #87.
Client.__init__creates arequests.Session()when none is passed in, but nothing ever closes it. In long-running processes, and in any test suite run with warnings enabled, every client that goes out of scope producesResourceWarning: unclosed <socket.socket ...>as the pooled connections are garbage collected, which is the symptom reported in #87.Change
Client.close()closes the underlying session and releases its pooled connections.Clientsupports thewithstatement, so the session is closed on exit even if the block raises:A session passed in by the caller is closed by
close()as well, matching the behaviour ofrequests.Sessionitself. This is documented in the README.Existing code that never closes the client is unaffected.
Tests
tests/test_client_session.pycoversclose(), the context manager return value, closing on normal exit, closing when the block raises, and the default session being closed. Full suite: 161 passed, 1 skipped.Docs
README gets a short "close it when you are done" snippet under Usage, and CHANGELOG has an entry under Unreleased.