Repository navigation
fix: preserve flag evaluations tracked during analytics flush - #284
pcbeingused333 wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
There was a problem hiding this comment.
1 issue found across 2 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="flagsmith/analytics.py">
<violation number="1" location="flagsmith/analytics.py:70">
P2: A synchronous `session.post` failure now drops this detached batch because the counters are cleared before submission. Re-queue `analytics_data` under `_lock` when submission raises, then propagate the exception.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
| if not self.analytics_data: | ||
| return | ||
| analytics_data = self.analytics_data | ||
| self.analytics_data = {} |
There was a problem hiding this comment.
P2: A synchronous session.post failure now drops this detached batch because the counters are cleared before submission. Re-queue analytics_data under _lock when submission raises, then propagate the exception.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At flagsmith/analytics.py, line 70:
<comment>A synchronous `session.post` failure now drops this detached batch because the counters are cleared before submission. Re-queue `analytics_data` under `_lock` when submission raises, then propagate the exception.</comment>
<file context>
@@ -56,30 +56,39 @@ def __init__(
+ if not self.analytics_data:
+ return
+ analytics_data = self.analytics_data
+ self.analytics_data = {}
+ self._last_flushed = datetime.now()
+
</file context>
Flag evaluations recorded while an analytics batch is being submitted can disappear:
flush()serializes the counters, submits the request, and then clears the shared dictionary, including evaluations added after serialization.Protect counter updates and batch detachment with a lock. Reset the pending dictionary and flush timestamp atomically, then serialize and submit the detached batch outside the lock. Evaluations arriving during submission stay available for the next flush, following the existing EventProcessor locking pattern.
A deterministic regression test blocks request submission in one thread while another records both an existing flag and a new flag. It verifies that tracking completes during submission and that both evaluations reach the next batch.
Testing and validation
This change and its regression test were generated with assistance from OpenAI Codex.
Summary by cubic
Fixes a race condition in the analytics flush that lost flag evaluations recorded while a batch was being submitted.
flush()now detaches the pending counters under a lock before serializing and posting, so evaluations arriving during submission stay available for the next flush instead of being cleared with the submitted batch.EventProcessorlocking pattern.Written for commit b438740. Summary will update on new commits.