Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(flags): quietly handle missing self team in flag provider (#61278)

Matheus Batista77db4cccc48a
diff --git a/products/feature_flags/backend/sdk_cache_provider.py b/products/feature_flags/backend/sdk_cache_provider.py
index 3aa6abdf6ebc..84dbede70c1d 100644
--- a/products/feature_flags/backend/sdk_cache_provider.py
+++ b/products/feature_flags/backend/sdk_cache_provider.py
@@ -2,6 +2,8 @@ from __future__ import annotations
from typing import TYPE_CHECKING, Optional
+from django.core.exceptions import ObjectDoesNotExist
+
import structlog
from posthoganalytics.flag_definition_cache import FlagDefinitionCacheData
@@ -58,6 +60,11 @@ class HyperCacheFlagProvider:
# all modules finish loading. The SDK's next poll cycle will retry.
logger.debug("hypercache_flag_provider_import_pending", team_id=self._team_id)
return None
+ except ObjectDoesNotExist:
+ # Self-hosted/local instances often lack the configured self team
+ # (POSTHOG_SELF_TEAM_ID, default 2). Returning None lets the SDK fall back to its API fetch.
+ logger.debug("hypercache_flag_provider_team_missing", team_id=self._team_id)
+ return None
except Exception:
logger.exception("hypercache_flag_provider_read_error", team_id=self._team_id)
return None
diff --git a/products/feature_flags/backend/test/test_sdk_cache_provider.py b/products/feature_flags/backend/test/test_sdk_cache_provider.py
index a27719902a59..d1c2f00a3eaa 100644
--- a/products/feature_flags/backend/test/test_sdk_cache_provider.py
+++ b/products/feature_flags/backend/test/test_sdk_cache_provider.py
@@ -5,6 +5,8 @@ from django.test import SimpleTestCase
from parameterized import parameterized
from posthoganalytics.client import Client
+from posthog.models import Team
+
from products.feature_flags.backend.sdk_cache_provider import HyperCacheFlagProvider
@@ -71,6 +73,20 @@ class TestHyperCacheFlagProvider(SimpleTestCase):
def test_get_flag_definitions_returns_none_on_circular_import(self, _mock):
assert self.provider.get_flag_definitions() is None
+ @patch("products.feature_flags.backend.sdk_cache_provider.logger")
+ @patch("products.feature_flags.backend.local_evaluation.flag_definitions_hypercache")
+ def test_get_flag_definitions_returns_none_on_missing_team(self, mock_hypercache, mock_logger):
+ # The self team (POSTHOG_SELF_TEAM_ID, default 2) is absent on most local/self-hosted instances.
+ # Team.DoesNotExist subclasses ObjectDoesNotExist, so it must be caught and logged at debug, not error.
+ self.provider._hypercache = None
+ mock_hypercache.get_from_cache.side_effect = Team.DoesNotExist("Team matching query does not exist.")
+
+ result = self.provider.get_flag_definitions()
+
+ assert result is None
+ mock_logger.debug.assert_called_once_with("hypercache_flag_provider_team_missing", team_id=2)
+ mock_logger.exception.assert_not_called()
+
@patch("products.feature_flags.backend.local_evaluation.flag_definitions_hypercache")
def test_caches_hypercache_reference(self, mock_hypercache):
self.provider._hypercache = None