Skip to content

fix(flags): fix failed flag overwrite on individual evaluation error#104

Merged
matheus-vb merged 1 commit intomasterfrom
matheus-vb/fix-failed-flag-overwrite
Feb 6, 2026
Merged

fix(flags): fix failed flag overwrite on individual evaluation error#104
matheus-vb merged 1 commit intomasterfrom
matheus-vb/fix-failed-flag-overwrite

Conversation

@matheus-vb
Copy link
Contributor

@matheus-vb matheus-vb commented Feb 6, 2026

Problem

When the /flags endpoint fails to evaluate a specific flag due to a transient error (e.g. DB timeout), the server returns that flag with enabled: false and a new failed: true field. The SDK then includes this failed result when transforming the v4 response, which can overwrite a previously valid value. In getAllFlags(), array_merge overwrites a locally evaluated true with the server's erroneous false.

See PostHog/posthog#46204.

Changes

lib/Client.php

  • In normalizeFeatureFlags(): skip flags with failed: true during the v4-to-v3 response transformation. Failed flags are excluded from featureFlags and featureFlagPayloads, so they cannot overwrite cached or locally evaluated values.
  • In getFeatureFlagResult(): exclude failed flags from flagDetail lookup. This prevents a null access warning when reading $flagDetail['reason']['description'] for flags whose reason may be null, and avoids attaching invalid metadata to analytics events.

test/FeatureFlagErrorTest.php

  • testFailedFlagIsFilteredOutByFailedField: verifies that the failed field drives the filtering. Uses three flags — failed=true, enabled=false+failed=false (legitimately disabled), and enabled=true+failed=false — to prove that only failed=true is filtered, not all disabled flags.
  • testGetAllFlagsDoesNotOverwriteLocalResultWithFailedServerFlag: reproduces the actual overwrite scenario. A flag evaluates locally as true, the server returns it with failed=true, and the test asserts the local value is preserved after array_merge.

Tests

All 155 tests pass, no new PHP warnings introduced.

Copy link
Contributor

@dustinbyrne dustinbyrne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@github-project-automation github-project-automation bot moved this from In Review to Approved in Feature Flags Feb 6, 2026
@matheus-vb matheus-vb merged commit c7b211c into master Feb 6, 2026
14 checks passed
@github-project-automation github-project-automation bot moved this from Approved to Done in Feature Flags Feb 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants