From 6420e4efb49a01fc010d6172e486cfe1df9084c4 Mon Sep 17 00:00:00 2001 From: Sarbajit Sengupta <73594751+Sarbajit-Sengupta@users.noreply.github.com> Date: Sun, 1 Nov 2026 16:38:08 +0530 Subject: [PATCH 1/2] fix(flags): treat concurrent cache eviction as a cache miss --- .../test/test_flag_cache_eviction_reads.py | 65 +++++++++++++++++++ posthog/utils.py | 16 ++--- 2 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 posthog/test/test_flag_cache_eviction_reads.py diff --git a/posthog/test/test_flag_cache_eviction_reads.py b/posthog/test/test_flag_cache_eviction_reads.py new file mode 100644 index 000000000..514ec9c75 --- /dev/null +++ b/posthog/test/test_flag_cache_eviction_reads.py @@ -0,0 +1,65 @@ +import pytest +from requests.exceptions import ConnectionError + +from posthog.client import Client +from posthog.types import FeatureFlagResult +from posthog.utils import FlagCache + + +class _EvictDuringRead(dict): + """Model a writer removing a key between a read's lookup operations.""" + + def __contains__(self, key): + present = super().__contains__(key) + self.pop(key, None) + return present + + def get(self, key, default=None): + self.pop(key, None) + return super().get(key, default) + + +@pytest.mark.parametrize("read_stale", [False, True]) +@pytest.mark.parametrize("evict_user", [False, True]) +def test_evicted_cache_entry_is_a_miss(read_stale, evict_user): + cache = FlagCache() + result = FeatureFlagResult.from_value_and_payload("flag", True, None) + cache.set_cached_flag("user", "flag", result, 1) + if evict_user: + cache.cache = _EvictDuringRead(cache.cache) + else: + cache.cache["user"] = _EvictDuringRead(cache.cache["user"]) + + if read_stale: + actual = cache.get_stale_cached_flag("user", "flag") + else: + actual = cache.get_cached_flag("user", "flag", 1) + + assert actual is None + assert not cache.cache if evict_user else not cache.cache["user"] + + +@pytest.mark.parametrize("evict_user", [False, True]) +def test_public_flag_lookup_survives_eviction_during_outage(monkeypatch, evict_user): + client = Client("test-key", send=False, enable_local_evaluation=False) + cache = FlagCache() + cache.set_cached_flag( + "user", "flag", FeatureFlagResult.from_value_and_payload("flag", True, None), 1 + ) + if evict_user: + cache.cache = _EvictDuringRead(cache.cache) + else: + cache.cache["user"] = _EvictDuringRead(cache.cache["user"]) + client.flag_cache = cache + + def unavailable(*args, **kwargs): + raise ConnectionError("offline test") + + monkeypatch.setattr(client, "_get_feature_flag_details_from_server", unavailable) + try: + result = client.get_feature_flag_result( + "flag", "user", send_feature_flag_events=False + ) + assert result is None + finally: + client.shutdown() diff --git a/posthog/utils.py b/posthog/utils.py index 05608983f..24e48a781 100644 --- a/posthog/utils.py +++ b/posthog/utils.py @@ -256,14 +256,14 @@ def _is_version_current(self, version): def get_cached_flag(self, distinct_id, flag_key, current_flag_version): current_time = time.time() - if distinct_id not in self.cache: + user_flags = self.cache.get(distinct_id) + if user_flags is None: return None - user_flags = self.cache[distinct_id] - if flag_key not in user_flags: + entry = user_flags.get(flag_key) + if entry is None: return None - entry = user_flags[flag_key] if self._is_version_current(entry.flag_definition_version) and entry.is_valid( current_time, self.default_ttl, current_flag_version ): @@ -278,14 +278,14 @@ def get_stale_cached_flag(self, distinct_id, flag_key, max_stale_age=None): current_time = time.time() - if distinct_id not in self.cache: + user_flags = self.cache.get(distinct_id) + if user_flags is None: return None - user_flags = self.cache[distinct_id] - if flag_key not in user_flags: + entry = user_flags.get(flag_key) + if entry is None: return None - entry = user_flags[flag_key] if self._is_version_current( entry.flag_definition_version ) and entry.is_stale_but_usable(current_time, max_stale_age): From d93e0c7ed765a42f24e00ff0b6d2460acd276edd Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Mon, 2 Nov 2026 16:45:05 +0100 Subject: [PATCH 2/2] chore: add changeset for fallback cache eviction fix --- .sampo/changesets/sullen-wolfkin-erika.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .sampo/changesets/sullen-wolfkin-erika.md diff --git a/.sampo/changesets/sullen-wolfkin-erika.md b/.sampo/changesets/sullen-wolfkin-erika.md new file mode 100644 index 000000000..3c6e6c064 --- /dev/null +++ b/.sampo/changesets/sullen-wolfkin-erika.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: patch +--- + +Fix a race condition where concurrent feature flag fallback-cache eviction could raise KeyError instead of returning a cache miss, including during request failures.