Repository navigation
fix: one Mixpanel SDK instance per thread - #16
Merged
Merged
Conversation
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
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 #12.
MixpanelClientkept one SDK instance, so a client shared across threads, like a module-level client in a web app, shared the SDK'sConsumerand its singlerequests.Session, or aBufferedConsumer's unlocked buffers._get_mp()could also build two instances on first use.Changes
threading.localand 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), orconsumer_factory=BufferedConsumer.consumerstays a single shared object, documented as needing to be thread-safe if used from several threads. Passing both raisesValueError.from_env()takesconsumer_factorytoo.flush()reaches every thread. With a factory, the client keeps the consumers it made, andflush()flushes each one; one failing doesn't stop the rest unlesson_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.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
consumerkeeps 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:Consumerper thread, reused within a thread.consumer_factory: called once per thread, with each thread's events going to its own consumer.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
_mpattribute. Pre-commit passes; mypy reports only the error already onmain.🤖 Generated with Claude Code
https://claude.ai/code/session_01CVp1mE8TsPmfiRgNEZPzg5