Repository navigation
fix: send tracking plugin events from the async GrowthBookClient - #147
Conversation
|
The async wrapper read attributes and url from the caller's user_context after the await, so a context changed while the evaluation waited could be recorded for the wrong user. Capture them first and use the snapshot when tracking.
|
@greptile review |
| if user_context is not None: | ||
| from ..common_types import tracking_user_context | ||
|
|
||
| user_context = tracking_user_context(user_context) |
There was a problem hiding this comment.
Snapshot can break feature evaluation
tracking_user_context calls dataclasses.replace under the hood, which only works on dataclass instances. Any duck-typed user_context now fails the whole call: await client.eval_feature("flag", ctx) returns normally without the plugin and raises TypeError: replace() should be called on dataclass instances with it (reproduced with both a MagicMock and a plain object exposing attributes/url). The same applies to a concurrent mutation of attributes during the recursive copy — RuntimeError: dictionary changed size during iteration propagates out of eval_feature instead of being logged and swallowed.
Every other tracking path in this plugin is deliberately defensive — _track_feature_evaluated wraps its whole body in try/except and reads fields via getattr(..., None), user callbacks go through _safe_execute — because the plugin's invariant is that enabling it never changes evaluation behaviour. This is the one spot that breaks it.
Suggest guarding the snapshot and falling back to the live context: tracking degrades to the pre-fix behaviour for that call instead of taking the evaluation down with it. _track_feature_evaluated already duck-types its input, so the fallback is safe.
| user_context = tracking_user_context(user_context) | |
| try: | |
| user_context = tracking_user_context(user_context) | |
| except Exception as e: | |
| # Tracking never breaks evaluation: fall back to the live context — | |
| # weaker against the race, but the call still succeeds. | |
| self.logger.debug("could not snapshot user context: %s", e) |
Nit while you're here: the from ..common_types import tracking_user_context can move to module level — common_types doesn't import plugins, so there's no cycle to avoid, and it currently runs on every async evaluation.
There was a problem hiding this comment.
Fixed in f18de37: the snapshot is now wrapped in try/except and falls back to the live context with a debug log, so a duck-typed user_context or a mutation mid-copy no longer breaks eval_feature. Added a regression test with a non-dataclass context.
I left the import inside the function on purpose: common_types imports plugins.base, which runs plugins/init.py, which imports growthbook_tracking, so a module-level import of tracking_user_context fails with a circular ImportError.
vazarkevych
left a comment
There was a problem hiding this comment.
Thanks @flcrom for the contribution! The fix looks good and all review comments have been addressed. Approving 🚀
Fixes #146.
With the async
GrowthBookClientthe tracking plugin never sent anything. Three changes ingrowthbook_tracking.py:initialize()now falls back tooptions.client_keywhen the instance has no_client_key, so batches have a key and are no longer dropped.eval_featureis a coroutine function, the plugin wraps it with an async wrapper. The sync wrapper was recording the coroutine object instead of the result.user_contextpassed toeval_feature(positional or keyword), falling back to the instance values as before.Added a test that runs
eval_feature,runandlog_eventthroughGrowthBookClientwith the tracking plugin and checks the events that get posted, for both the positional and keyword user context. The existing suite, mypy, flake8 and pyright pass locally.