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): fail closed on cohort filters when person properties are pending (#82164)

Gustavo H. Strassburger4561dadd0880
diff --git a/rust/feature-flags/src/flags/flag_matching.rs b/rust/feature-flags/src/flags/flag_matching.rs
index bfd0eb7db393..eb4e6452c8b7 100644
--- a/rust/feature-flags/src/flags/flag_matching.rs
+++ b/rust/feature-flags/src/flags/flag_matching.rs
@@ -1678,9 +1678,9 @@ impl FeatureFlagMatcher {
// so an absent key there genuinely means the person lacks the property.
// This is defense in depth: in the batch flow a prep failure errors those
// flags out before evaluation, so the guard protects any path that reaches
- // evaluation with the state still Pending. Cohort filters (evaluated
- // separately below) don't get this guard; under Pending they're currently
- // safe only because cohorts are never loaded when person prep hasn't run.
+ // evaluation with the state still Pending. Cohort filters get the same
+ // treatment below, refusing to evaluate outright since cohort membership
+ // is unknowable under Pending.
let partial_props = filter.prop_type != PropertyType::Group
&& self.flag_evaluation_state.person_properties_pending();
if !match_property(filter, props, partial_props, self.timezone).unwrap_or(false)
@@ -1692,6 +1692,31 @@ impl FeatureFlagMatcher {
// Evaluate cohort filters using person properties (cohorts are person-level).
if !cohort_filters.is_empty() {
+ // Cohort evaluation reads the same person-property map as direct filters, and
+ // both the property-level `negation` inside a cohort definition and cohort-level
+ // NOT_IN would turn a missing key into a match. Membership is unknowable under
+ // Pending either way, so refuse to evaluate rather than guess. Today no prep path
+ // loads cohorts without fetching person properties, so this should be unreachable
+ // — hence the error log, which flags a broken invariant rather than normal traffic.
+ if self.flag_evaluation_state.person_properties_pending() {
+ inc(
+ PROPERTY_CACHE_MISSES_COUNTER,
+ &[
+ ("type".to_string(), "cohort_filters".to_string()),
+ ("reason".to_string(), "db_prep_never_ran".to_string()),
+ ],
+ 1,
+ );
+ with_canonical_log(|log| {
+ log.eval.property_cache_misses += 1;
+ log.eval.person_properties_not_cached = true;
+ });
+ tracing::error!(
+ "Cohort filters not evaluated — DB prep never ran, so the condition fails closed"
+ );
+ return Ok((false, FeatureFlagMatchReason::NoConditionMatch));
+ }
+
let cohorts = match &self.flag_evaluation_state.cohorts {
Some(cohorts) => cohorts.clone(),
None => return Ok((false, FeatureFlagMatchReason::NoConditionMatch)),
diff --git a/rust/feature-flags/src/flags/test_flag_matching.rs b/rust/feature-flags/src/flags/test_flag_matching.rs
index 1184dd670428..a40d8e9dd95c 100644
--- a/rust/feature-flags/src/flags/test_flag_matching.rs
+++ b/rust/feature-flags/src/flags/test_flag_matching.rs
@@ -13,7 +13,7 @@ mod tests {
api::types::{FlagValue, LegacyFlagsResponse},
cohorts::{
cohort_cache_manager::CohortCacheManager,
- cohort_models::{CohortId, CohortType, MembershipStampPolicy},
+ cohort_models::{Cohort, CohortId, CohortType, MembershipStampPolicy},
membership::{CohortMembershipError, CohortMembershipProvider},
},
flags::{
@@ -1778,6 +1778,82 @@ mod tests {
assert_eq!(reason, FeatureFlagMatchReason::ConditionMatch);
}
+ /// Regression test: a `NOT_IN` cohort filter must not match when person-property DB prep
+ /// never ran. Cohort evaluation reads the same property map as direct filters, so under
+ /// `Pending` the person looks like they have no properties, the cohort resolves to "not a
+ /// member", and `NOT_IN` flips that into a match — granting the flag to exactly the people
+ /// the condition excludes. Cohorts are loaded here so the check can't pass by falling
+ /// through the `cohorts: None` branch instead.
+ #[tokio::test]
+ async fn test_is_condition_match_cohort_not_in_fails_closed_when_person_properties_pending() {
+ let context = TestContext::new(None).await;
+ let cohort_cache = Arc::new(CohortCacheManager::new(
+ context.non_persons_reader.clone(),
+ None,
+ None,
+ ));
+ let flag = mock!(FeatureFlag);
+
+ let condition = FlagPropertyGroup {
+ variant: None,
+ properties: Some(vec![PropertyFilter {
+ key: "id".to_string(),
+ value: Some(json!(42)),
+ operator: Some(OperatorType::NotIn),
+ prop_type: PropertyType::Cohort,
+ group_type_index: None,
+ negation: None,
+ compiled_regex: None,
+ extra: Default::default(),
+ }]),
+ rollout_percentage: Some(100.0),
+ ..Default::default()
+ };
+
+ let mut matcher = FeatureFlagMatcher::new(
+ "test_user".to_string(),
+ None,
+ 1,
+ context.create_postgres_router(),
+ cohort_cache,
+ empty_group_type_cache(),
+ None,
+ );
+ matcher
+ .flag_evaluation_state
+ .set_cohorts(Arc::from(vec![Cohort {
+ id: 42,
+ team_id: 1,
+ filters: Some(json!({
+ "properties": {
+ "type": "AND",
+ "values": [
+ {"key": "tenant", "type": "person", "value": "acme", "operator": "exact"}
+ ]
+ }
+ })),
+ ..Default::default()
+ }]));
+ // Left at its default `Pending` state: DB prep never ran for this matcher.
+ assert!(matcher.flag_evaluation_state.person_properties_pending());
+
+ let empty_person = HashMap::new();
+ let empty_groups = HashMap::new();
+ let ctx = PropertyContext {
+ person_properties: Some(&empty_person),
+ group_properties: &empty_groups,
+ aggregation: None,
+ };
+ let (is_match, reason) = matcher
+ .is_condition_match(&flag, &condition, &ctx, None, &None)
+ .unwrap();
+ assert!(
+ !is_match,
+ "unknowable cohort membership must not satisfy NOT_IN"
+ );
+ assert_eq!(reason, FeatureFlagMatchReason::NoConditionMatch);
+ }
+
fn create_test_flag_with_variants(team_id: TeamId) -> FeatureFlag {
mock!(FeatureFlag,
team_id: team_id,