fix(flags): only install HyperCacheFlagProvider in US#56768
Closed
sampennington wants to merge 3 commits into
Closed
fix(flags): only install HyperCacheFlagProvider in US#56768sampennington wants to merge 3 commits into
sampennington wants to merge 3 commits into
Conversation
The provider is keyed by team_id (defaults to 2), which only refers to the PostHog company project in US Postgres. In EU and other regions team 2 is an unrelated org, so the SDK reads the wrong flag definitions and local evaluation of company flags returns None. Compounding the issue, the provider always returns a truthy dict (even for an empty flags list), which the SDK accepts as authoritative — so the emergency API fallback never fires and the bad cache state sticks until restart. Gate the provider installation on CLOUD_DEPLOYMENT == "US". Outside US, leave flag_definition_cache_provider unset so the SDK falls back to its normal API polling against posthoganalytics.host (us.i.posthog.com), which is the cross-region behavior that worked before #50737. Generated-By: PostHog Code Task-Id: 2dfd1690-4890-4d79-b62e-167289f38dca
Contributor
Prompt To Fix All With AIThis is a comment left during a code review.
Path: posthog/feature_flags/test_sdk_cache_provider.py
Line: 227-250
Comment:
**Non-parameterized US and EU override tests**
The four test methods in this class test the same function with varying `(CLOUD_DEPLOYMENT, POSTHOG_SELF_TEAM_ID, expected_result)` combinations. Per the project's preference for parameterised tests, these could be consolidated into a single `@parameterized.expand` case list, eliminating the repeated `setUp`/`tearDown` env-isolation boilerplate and making it trivial to add new regions or env-var combinations:
```python
@parameterized.expand([
("us_default", "US", None, 2),
("us_env_override", "US", "42", 42),
("eu_no_env", "EU", None, None),
("eu_env_override", "EU", "42", None),
("dev", "DEV", None, None),
("e2e", "E2E", None, None),
("none", None, None, None),
])
@patch.dict(os.environ, {}, clear=False)
def test_provider(self, _name, deployment, env_team_id, expected_team_id):
if env_team_id is not None:
os.environ["POSTHOG_SELF_TEAM_ID"] = env_team_id
else:
os.environ.pop("POSTHOG_SELF_TEAM_ID", None)
with override_settings(CLOUD_DEPLOYMENT=deployment):
provider = get_default_flag_definition_cache_provider()
if expected_team_id is None:
assert provider is None
else:
assert isinstance(provider, HyperCacheFlagProvider)
assert provider._team_id == expected_team_id
```
This covers all seven current cases in one place and makes the separate `setUp`/`tearDown` unnecessary.
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "fix(flags): only install HyperCacheFlagP..." | Re-trigger Greptile |
Consolidate the four region/env-override test methods into a single
parameterized test_provider, per AGENTS.md preference for parameterized
tests. Drops the setUp/tearDown env-isolation boilerplate in favor of a
@patch.dict(os.environ, {}, clear=False) decorator at the method level.
Generated-By: PostHog Code
Task-Id: 2dfd1690-4890-4d79-b62e-167289f38dca
- Add LOCAL row to parameterized test (documented CLOUD_DEPLOYMENT value).
- Drop test class docstring per AGENTS.md ("Python tests: do not add doc
comments").
- Rewrite env handling declaratively: each row carries an env_overrides
dict and the test uses patch.dict(..., clear=True) for per-row isolation
instead of mutating os.environ inside the test body.
- Restore the E2E cold-cache note in apps.py.
Generated-By: PostHog Code
Task-Id: 2dfd1690-4890-4d79-b62e-167289f38dca
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Local feature-flag evaluation in our server-side
posthoganalyticsSDK returnsNoneinstead ofTrue/Falsein EU pods (works in US). Customer-facing example:posthoganalytics.feature_enabled("persons-on-events-v2-reads-enabled", ..., only_evaluate_locally=True)finds no flag definition.apps.pykeysHyperCacheFlagProviderbyteam_id=2(no env override is set anywhere), assuming "local team 2 == PostHog company project". That holds in US Postgres, not in EU. The provider also returns a truthy dict for an empty flags list, so the SDK accepts the EU cache as authoritative and never falls back to its API path.Changes
Gate provider install on
settings.CLOUD_DEPLOYMENT == "US". Outside US, leaveflag_definition_cache_providerunset so the SDK pollsposthoganalytics.host(us.i.posthog.com) directly — the cross-region behavior that worked before #50737. Gate lives in a smallget_default_flag_definition_cache_provider()helper insdk_cache_provider.pyso it's testable without invokingAppConfig.ready().How did you test this code?
I'm an agent (PostHog Code). Added
TestGetDefaultFlagDefinitionCacheProviderparameterized overUS/EU/DEV/E2E/LOCAL/ unset, with and withoutPOSTHOG_SELF_TEAM_IDoverride (8 cases). Existing tests untouched. Did not run the full pytest suite — sandbox lacked the flox venv. Verifiedruff checkclean and smoke-tested the helper against all 8 cases.Publish to changelog?
no
🤖 Agent context
apps.py:79-88,sdk_cache_provider.py:43-68, andposthoganalytics 7.13.0::client.py::_load_feature_flags(lines 1253-1296 — the truthy-payload-accepted-as-authoritative path).apps.py, becauseAppConfig.ready()forcesposthoganalytics.disabled=TrueinTESTmode and runs many other side effects, making a true regression test fragile. The factory captures the same semantic gate and is directly testable.get_flag_definitions()returns empty flags;CLOUD_DEPLOYMENT in {"US"}allowlist constant for future US-WEST style splits.