From b43874020de1252f6dc154009e9a213971db3068 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Sun, 15 Nov 2026 12:36:28 -0400 Subject: [PATCH 1/3] fix: preserve flag evaluations tracked during analytics flush --- flagsmith/analytics.py | 25 ++++++++++++++++-------- tests/test_analytics.py | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/flagsmith/analytics.py b/flagsmith/analytics.py index f5af134..05aa568 100644 --- a/flagsmith/analytics.py +++ b/flagsmith/analytics.py @@ -56,17 +56,23 @@ def __init__( self._last_flushed = datetime.now() self.analytics_data: typing.MutableMapping[str, typing.Any] = {} self.timeout = timeout or 3 + self._lock = threading.Lock() def flush(self) -> None: """ Sends all the collected data to the api asynchronously and resets the timer """ - if not self.analytics_data: - return + with self._lock: + if not self.analytics_data: + return + analytics_data = self.analytics_data + self.analytics_data = {} + self._last_flushed = datetime.now() + session.post( self.analytics_endpoint, - data=json.dumps(self.analytics_data), + data=json.dumps(analytics_data), timeout=self.timeout, headers={ "X-Environment-Key": self.environment_key, @@ -74,12 +80,15 @@ def flush(self) -> None: }, ) - self.analytics_data.clear() - self._last_flushed = datetime.now() - def track_feature(self, feature_name: str) -> None: - self.analytics_data[feature_name] = self.analytics_data.get(feature_name, 0) + 1 - if (datetime.now() - self._last_flushed).seconds > ANALYTICS_TIMER: + with self._lock: + self.analytics_data[feature_name] = ( + self.analytics_data.get(feature_name, 0) + 1 + ) + should_flush = ( + datetime.now() - self._last_flushed + ).seconds > ANALYTICS_TIMER + if should_flush: self.flush() diff --git a/tests/test_analytics.py b/tests/test_analytics.py index c077766..b08f1bb 100644 --- a/tests/test_analytics.py +++ b/tests/test_analytics.py @@ -1,5 +1,7 @@ import json +from concurrent.futures import ThreadPoolExecutor from datetime import datetime, timedelta +from threading import Event from unittest import mock from flagsmith.analytics import ( @@ -92,3 +94,43 @@ def test_analytics_processor_posts_to_analytics_url_when_set() -> None: session.post.assert_called_once() assert session.post.call_args[0][0] == "http://core-api/analytics/flags/" assert "edge-proxy" not in session.post.call_args[0][0] + + +def test_analytics_processor_preserves_evaluations_during_flush( + analytics_processor: AnalyticsProcessor, +) -> None: + submitted = Event() + release = Event() + analytics_processor.track_feature("my_feature") + + def post(*args: object, **kwargs: object) -> None: + submitted.set() + assert release.wait(5) + + with mock.patch("flagsmith.analytics.session.post", side_effect=post) as post_mock: + with ThreadPoolExecutor(max_workers=2) as executor: + flushing = executor.submit(analytics_processor.flush) + try: + assert submitted.wait(5) + # Tracking must finish while submission is still blocked. + executor.submit(analytics_processor.track_feature, "my_feature").result( + timeout=5 + ) + executor.submit( + analytics_processor.track_feature, "another_feature" + ).result(timeout=5) + finally: + release.set() + flushing.result(timeout=5) + + assert json.loads(post_mock.call_args.kwargs["data"]) == {"my_feature": 1} + assert analytics_processor.analytics_data == { + "my_feature": 1, + "another_feature": 1, + } + analytics_processor.flush() + assert json.loads(post_mock.call_args.kwargs["data"]) == { + "my_feature": 1, + "another_feature": 1, + } + assert analytics_processor.analytics_data == {} From d6dcbf4eef37dcb6d4d8770d46def39f5b78ca9a Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 29 Nov 2026 08:46:44 -0400 Subject: [PATCH 2/3] fix: restore analytics counts when submission raises --- flagsmith/analytics.py | 27 ++++++++++++++++++--------- tests/test_analytics.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/flagsmith/analytics.py b/flagsmith/analytics.py index 05aa568..9d9fdc8 100644 --- a/flagsmith/analytics.py +++ b/flagsmith/analytics.py @@ -70,15 +70,24 @@ def flush(self) -> None: self.analytics_data = {} self._last_flushed = datetime.now() - session.post( - self.analytics_endpoint, - data=json.dumps(analytics_data), - timeout=self.timeout, - headers={ - "X-Environment-Key": self.environment_key, - "Content-Type": "application/json", - }, - ) + data = json.dumps(analytics_data) + try: + session.post( + self.analytics_endpoint, + data=data, + timeout=self.timeout, + headers={ + "X-Environment-Key": self.environment_key, + "Content-Type": "application/json", + }, + ) + except Exception: + with self._lock: + for feature_name, count in analytics_data.items(): + self.analytics_data[feature_name] = ( + self.analytics_data.get(feature_name, 0) + count + ) + raise def track_feature(self, feature_name: str) -> None: with self._lock: diff --git a/tests/test_analytics.py b/tests/test_analytics.py index b08f1bb..559b475 100644 --- a/tests/test_analytics.py +++ b/tests/test_analytics.py @@ -3,6 +3,7 @@ from datetime import datetime, timedelta from threading import Event from unittest import mock +import pytest from flagsmith.analytics import ( ANALYTICS_ENDPOINT, @@ -134,3 +135,40 @@ def post(*args: object, **kwargs: object) -> None: "another_feature": 1, } assert analytics_processor.analytics_data == {} + + +def test_analytics_processor_restores_batch_when_submission_raises( + analytics_processor: AnalyticsProcessor, +) -> None: + submitted = Event() + release = Event() + analytics_processor.track_feature("my_feature") + + def post(*args: object, **kwargs: object) -> None: + submitted.set() + assert release.wait(5) + raise RuntimeError("submission failed") + + with mock.patch("flagsmith.analytics.session.post", side_effect=post): + with ThreadPoolExecutor(max_workers=2) as executor: + flushing = executor.submit(analytics_processor.flush) + try: + assert submitted.wait(5) + executor.submit(analytics_processor.track_feature, "my_feature").result( + timeout=5 + ) + executor.submit( + analytics_processor.track_feature, "another_feature" + ).result(timeout=5) + finally: + release.set() + with pytest.raises(RuntimeError, match="submission failed"): + flushing.result(timeout=5) + + with mock.patch("flagsmith.analytics.session.post") as post_mock: + analytics_processor.flush() + assert json.loads(post_mock.call_args.kwargs["data"]) == { + "my_feature": 2, + "another_feature": 1, + } + assert analytics_processor.analytics_data == {} From e583d8f87739626dc52e6898ba3ef9b169baccba Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 29 Nov 2026 09:09:13 -0400 Subject: [PATCH 3/3] style: separate standard library and pytest imports --- tests/test_analytics.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_analytics.py b/tests/test_analytics.py index 559b475..9daa73d 100644 --- a/tests/test_analytics.py +++ b/tests/test_analytics.py @@ -3,6 +3,7 @@ from datetime import datetime, timedelta from threading import Event from unittest import mock + import pytest from flagsmith.analytics import (