Skip to content

Add context manager and close() to ClientBase#121

Merged
hownowstephen merged 2 commits intomainfrom
MESS-674_add-context-manager-support
May 8, 2026
Merged

Add context manager and close() to ClientBase#121
hownowstephen merged 2 commits intomainfrom
MESS-674_add-context-manager-support

Conversation

@hownowstephen
Copy link
Copy Markdown
Contributor

@hownowstephen hownowstephen commented May 7, 2026

Summary

  • Adds __enter__, __exit__, and close() to ClientBase
  • Both CustomerIO and APIClient inherit this, enabling:
    with CustomerIO(site_id, api_key) as cio:
        cio.identify(id="123", name="Alice")
    # session is automatically closed, no ResourceWarning
  • close() is also available standalone for non-context-manager usage

Closes #87

Test plan

  • test_context_manager_closes_session — session closed on __exit__, _current_session reset
  • test_close_without_session — calling close() before any request is safe
  • test_close_resets_session — calling close() after requests cleans up properly
  • Full test suite passes (31 tests)
  • Lint passes

Note

Low Risk
Low risk: additive session-cleanup behavior with tests, and it only affects resource lifecycle when close()/context manager is used.

Overview
ClientBase now supports with-statement usage via __enter__/__exit__ and exposes a close() 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_session is cleared even if session.close() raises.

Reviewed by Cursor Bugbot for commit 979dbea. Bugbot is set up for automated code reviews on this repo. Configure here.

@hownowstephen hownowstephen mentioned this pull request 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
@hownowstephen hownowstephen force-pushed the MESS-674_add-context-manager-support branch from 4f51730 to 1fefe1c Compare May 8, 2026 02:59
Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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.

Comment thread customerio/client_base.py Outdated
If session.close() raises, _current_session was never reset to None,
leaving the client unable to create a new session on subsequent calls.
@hownowstephen hownowstephen merged commit 7b23a68 into main May 8, 2026
10 checks passed
@hownowstephen hownowstephen deleted the MESS-674_add-context-manager-support branch May 8, 2026 03:17
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.

ResourceWarning unclosed ssl.SSLSocket

2 participants