Skip to content

fix: send tracking plugin events from the async GrowthBookClient - #147

Merged
vazarkevych merged 3 commits into
growthbook:mainfrom
flcrom:fix-async-client-tracking-plugin
Oct 12, 2026
Merged

vazarkevych merged 3 commits into
growthbook:mainfrom
flcrom:fix-async-client-tracking-plugin

Conversation

@flcrom

@flcrom flcrom commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Fixes #146.

With the async GrowthBookClient the tracking plugin never sent anything. Three changes in growthbook_tracking.py:

  • initialize() now falls back to options.client_key when the instance has no _client_key, so batches have a key and are no longer dropped.
  • When eval_feature is a coroutine function, the plugin wraps it with an async wrapper. The sync wrapper was recording the coroutine object instead of the result.
  • Feature events use the attributes and url from the user_context passed to eval_feature (positional or keyword), falling back to the instance values as before.

Added a test that runs eval_feature, run and log_event through GrowthBookClient with 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.

@greptile-apps

greptile-apps Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

RetriggerConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the previous wrong-user tracking issue is fixed.

Reviews (2) · Last reviewed commit: "fix: snapshot the user context before aw..." · Reviewed by Greptile

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.
@vazarkevych

Copy link
Copy Markdown
Collaborator

@greptile review

if user_context is not None:
from ..common_types import tracking_user_context

user_context = tracking_user_context(user_context)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Suggested change
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 vazarkevych left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @flcrom for the contribution! The fix looks good and all review comments have been addressed. Approving 🚀

@vazarkevych
vazarkevych merged commit aed5f43 into growthbook:main Oct 12, 2026
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.

Tracking plugin sends no events with the async GrowthBookClient

2 participants