Skip to content

fix: one Mixpanel SDK instance per thread - #16

Merged
lukwam merged 1 commit into
mainfrom
fix/per-thread-sdk-client
Oct 26, 2026
Merged

lukwam merged 1 commit into
mainfrom
fix/per-thread-sdk-client

Conversation

@lukwam

@lukwam lukwam commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

Fixes #12. MixpanelClient kept one SDK instance, so a client shared across threads, like a module-level client in a web app, shared the SDK's Consumer and its single requests.Session, or a BufferedConsumer's unlocked buffers. _get_mp() could also build two instances on first use.

Changes

  • Per-thread SDK instance. It lives in a threading.local and is created on first use in each thread, as Lived's own _mixpanel_client() does. With the default consumer, each thread therefore also gets its own HTTP session. This also removes the first-use race.
  • consumer_factory, called once per thread, for custom consumers in threaded apps: consumer_factory=lambda: Consumer(request_timeout=5) (what Lived needs), or consumer_factory=BufferedConsumer. consumer stays a single shared object, documented as needing to be thread-safe if used from several threads. Passing both raises ValueError. from_env() takes consumer_factory too.
  • flush() reaches every thread. With a factory, the client keeps the consumers it made, and flush() flushes each one; one failing doesn't stop the rest unless on_error="raise". It's documented as a shutdown call: a buffering consumer isn't safe to flush while its own thread is adding to it.
  • README "Threads" section; CHANGELOG entry.

The issue's choice between a consumer factory and documenting that a shared consumer must be thread-safe: this does both. The factory is how to get per-thread custom consumers, and consumer keeps working for a thread-safe or single-threaded consumer.

Testing

8 new tests (107 total; the thread tests passed in 5 repeated runs). Threads start together on a Barrier. The tests cover:

  • With the default consumer: one SDK instance and one Consumer per thread, reused within a thread.
  • consumer_factory: called once per thread, with each thread's events going to its own consumer.
  • A shared consumer: used by every thread.
  • flush(): flushes every thread's consumer; with no thread having tracked, the factory is never called; when logging, a failing consumer doesn't stop the rest.
  • consumer + consumer_factory: raises.
  • from_env: forwards the factory.

Four existing tests now read the per-thread instance instead of the removed _mp attribute. Pre-commit passes; mypy reports only the error already on main.

🤖 Generated with Claude Code

https://claude.ai/code/session_01CVp1mE8TsPmfiRgNEZPzg5

MixpanelClient kept a single SDK instance, so a client shared across
threads (a module-level client in a web app) shared the SDK's Consumer
and its requests.Session, or a BufferedConsumer's unlocked buffers, and
_get_mp() could build two instances on first use.

- The SDK instance lives in a threading.local, created on first use in
  each thread; the default consumer (and HTTP session) is per-thread too.
- consumer_factory: called once per thread, for custom consumers in
  threaded apps. consumer stays a single shared object; passing both
  raises ValueError. from_env() takes consumer_factory as well.
- flush() flushes every consumer consumer_factory made, so buffered
  events in any thread are sent; documented as a shutdown call.
- README "Threads" section; CHANGELOG entry.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01CVp1mE8TsPmfiRgNEZPzg5
@lukwam
lukwam merged commit 97893b6 into main Oct 26, 2026
5 checks passed
@lukwam
lukwam deleted the fix/per-thread-sdk-client branch October 26, 2026 22:53
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.

fix: MixpanelClient shares one SDK client and HTTP session across threads

1 participant