Add context manager and close() to ClientBase#121
Merged
hownowstephen merged 2 commits intomainfrom May 8, 2026
Merged
Conversation
Closed
washam-cio
approved these changes
May 7, 2026
Allows using CustomerIO and APIClient as context managers to properly close the underlying requests Session and avoid ResourceWarning for unclosed SSL sockets. Closes #87
4f51730 to
1fefe1c
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 1fefe1c. Configure here.
If session.close() raises, _current_session was never reset to None, leaving the client unable to create a new session on subsequent calls.
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
__enter__,__exit__, andclose()toClientBaseCustomerIOandAPIClientinherit this, enabling:close()is also available standalone for non-context-manager usageCloses #87
Test plan
test_context_manager_closes_session— session closed on__exit__,_current_sessionresettest_close_without_session— callingclose()before any request is safetest_close_resets_session— callingclose()after requests cleans up properlyNote
Low Risk
Low risk: additive session-cleanup behavior with tests, and it only affects resource lifecycle when
close()/context manager is used.Overview
ClientBasenow supportswith-statement usage via__enter__/__exit__and exposes aclose()method to explicitly close and clear the internally pooled HTTP session.Tests are added to verify sessions are closed on context exit, that
close()is safe when no session exists, and that_current_sessionis cleared even ifsession.close()raises.Reviewed by Cursor Bugbot for commit 979dbea. Bugbot is set up for automated code reviews on this repo. Configure here.