Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/sullen-wolfkin-erika.md
Original file line number Diff line number Diff line change
@@ -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.
65 changes: 65 additions & 0 deletions posthog/test/test_flag_cache_eviction_reads.py
Original file line number Diff line number Diff line change
@@ -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()
16 changes: 8 additions & 8 deletions posthog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Expand All @@ -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):
Expand Down
Loading