Skip to content

fix: always invoke reloadFeatureFlags callback#546

Open
turnipdabeets wants to merge 2 commits into
mainfrom
fix/reload-feature-flags-callback
Open

fix: always invoke reloadFeatureFlags callback#546
turnipdabeets wants to merge 2 commits into
mainfrom
fix/reload-feature-flags-callback

Conversation

@turnipdabeets
Copy link
Copy Markdown
Contributor

@turnipdabeets turnipdabeets commented Jun 2, 2026

💡 Motivation and Context

https://posthoghelp.zendesk.com/agent/tickets/58925

reloadFeatureFlags(onFeatureFlags) promises to invoke onFeatureFlags when the reload finishes, but several early-returns skipped it:

  • if (!isEnabled()) — SDK disabled or opted-out
  • if (distinctId.isBlank()) in loadFeatureFlagsRequest

When the callback is never invoked, any caller awaiting it stalls. This surfaced via the Flutter SDK, whose reloadFeatureFlags() returns a Future that completes from this callback — await Posthog().reloadFeatureFlags() would hang forever in those states. (Once execution reaches the network load, the callback already fires on every outcome via executeFeatureFlags, so only the front-door guards were affected.)

💚 How did you test it?

./gradlew :posthog:compileKotlin passes. Existing reloadFeatureFlags behavior is unchanged on the normal path; the change only adds the callback invocation to the two guard returns. Happy to add a unit test asserting the callback fires when !isEnabled() if preferred.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran pnpm changeset to generate a changeset file

The reloadFeatureFlags(onFeatureFlags) early-returns (SDK disabled/opted-out,
blank distinctId) skipped the completion callback, so callers awaiting it
(e.g. the Flutter SDK) could hang. Invoke the callback before returning.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@turnipdabeets turnipdabeets requested a review from a team as a code owner June 2, 2026 20:01
@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Jun 2, 2026

Comments Outside Diff (1)

  1. posthog/src/main/java/com/posthog/PostHog.kt, line 1252-1262 (link)

    P2 No tests covering the new callback-on-early-return behaviour

    The fix addresses a real contract violation, but both new code paths (callback fired when !isEnabled() and when distinctId.isBlank()) are untested. Adding a test that verifies onFeatureFlags.loaded() is called in each guard case would prevent silent regressions and fits the existing PostHogFeatureFlagsTest suite. The PR description offers to add one — it would be worth doing so before merge.

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: posthog/src/main/java/com/posthog/PostHog.kt
    Line: 1252-1262
    
    Comment:
    **No tests covering the new callback-on-early-return behaviour**
    
    The fix addresses a real contract violation, but both new code paths (callback fired when `!isEnabled()` and when `distinctId.isBlank()`) are untested. Adding a test that verifies `onFeatureFlags.loaded()` is called in each guard case would prevent silent regressions and fits the existing `PostHogFeatureFlagsTest` suite. The PR description offers to add one — it would be worth doing so before merge.
    
    How can I resolve this? If you propose a fix, please make it concise.

    Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
posthog/src/main/java/com/posthog/PostHog.kt:1278-1283
**Missing callback for `internalOnFeatureFlags` on blank distinctId**

When `distinctId.isBlank()`, only the external `onFeatureFlags?.loaded()` is invoked, but `internalOnFeatureFlags` (which starts session replay) is silently skipped. On the `!isEnabled()` path in `reloadFeatureFlags` this is fine — session replay should not start when the SDK is disabled — but for the blank-distinctId case the SDK is enabled and session replay is otherwise expected to start once flags resolve. A caller that triggers this path (e.g. calling `reloadFeatureFlags` before `identify`) would leave session replay permanently un-started for that SDK lifecycle, since `internalOnFeatureFlagsLoaded` is never invoked.

### Issue 2 of 2
posthog/src/main/java/com/posthog/PostHog.kt:1252-1262
**No tests covering the new callback-on-early-return behaviour**

The fix addresses a real contract violation, but both new code paths (callback fired when `!isEnabled()` and when `distinctId.isBlank()`) are untested. Adding a test that verifies `onFeatureFlags.loaded()` is called in each guard case would prevent silent regressions and fits the existing `PostHogFeatureFlagsTest` suite. The PR description offers to add one — it would be worth doing so before merge.

Reviews (1): Last reviewed commit: "fix: always invoke reloadFeatureFlags ca..." | Re-trigger Greptile

Comment thread posthog/src/main/java/com/posthog/PostHog.kt
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 2, 2026

posthog-android Compliance Report

Date: 2026-06-02 21:36:15 UTC
Duration: 2991ms

⚠️ Some Tests Failed

0/16 tests passed, 16 failed


Feature_Flags Tests

⚠️ 0/16 tests passed, 16 failed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 331ms
Request Payload.Flags Request Uses V2 Query Param 48ms
Request Payload.Flags Request Hits Flags Path Not Decide 37ms
Request Payload.Flags Request Omits Authorization Header 23ms
Request Payload.Token In Flags Body Matches Init 21ms
Request Payload.Groups Round Trip 19ms
Request Payload.Groups Default To Empty Object 20ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It 19ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 20ms
Request Payload.Disable Geoip Omitted Defaults To False 17ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 16ms
Request Lifecycle.No Flags Request On Init Alone 11ms
Request Lifecycle.No Flags Request On Normal Capture 2057ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 15ms
Request Lifecycle.Mock Response Value Is Returned To Caller 14ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 15ms

Failures

request_payload.request_with_person_properties_device_id

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.flags_request_uses_v2_query_param

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.flags_request_hits_flags_path_not_decide

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.flags_request_omits_authorization_header

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.token_in_flags_body_matches_init

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.groups_round_trip

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.groups_default_to_empty_object

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.person_properties_distinct_id_auto_populated_when_caller_omits_it

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.disable_geoip_false_propagates_as_geoip_disable_false

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.disable_geoip_omitted_defaults_to_false

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.flag_keys_to_evaluate_contains_only_requested_key

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_lifecycle.no_flags_request_on_init_alone

Expected 0 /flags requests, got 1

request_lifecycle.no_flags_request_on_normal_capture

Expected 0 /flags requests, got 1

request_lifecycle.two_flag_calls_produce_two_remote_requests

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_lifecycle.mock_response_value_is_returned_to_caller

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

side_effect_events.get_feature_flag_captures_feature_flag_called_event

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

Route the early-return callback invocations through a try/catch helper so a
throwing caller callback can't escape reloadFeatureFlags (matching
runOnFeatureFlagsCallbacks). Add a test asserting the callback fires when the
SDK is not enabled.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
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.

makes sense

@turnipdabeets
Copy link
Copy Markdown
Contributor Author

Re: @greptile-apps review on 302c9fd:

  • Issue 2 (no tests for early-return callback) — addressed in acf400c, which adds reloadFeatureFlags invokes callback when not enabled and routes the early-return callbacks through a notifyFeatureFlagsCallback try/catch helper. Note: only the !isEnabled() guard is covered; the blank-distinctId guard case is still untested.
  • Issue 1 (internalOnFeatureFlags/session replay skipped on blank distinctId) — not addressed; the blank-distinctId path still invokes only the external callback. This is pre-existing behaviour rather than a regression from this PR.

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Jun 3, 2026

Good progress — acf400c looks clean. The notifyFeatureFlagsCallback helper is the right approach (matches the exception-swallowing behavior of runOnFeatureFlagsCallbacks) and the !isEnabled() test is solid.

Two remaining notes:

Blank distinctId test (minor) — since the fix touches that guard too, the test coverage gap is worth closing. It'd be a near-identical test to the one you added (just trigger it via an unidentified/reset state rather than close()). Not blocking, but it'd round out the contract.

internalOnFeatureFlagsCallback on blank distinctId (pre-existing, tracking only) — agreed this is out of scope for this PR. Worth filing a follow-up issue so it doesn't get lost — the blank-distinctId path now fires the external callback but still silently skips session replay initialization, which could be surprising if that path ever becomes reachable in practice.

The core fix is correct and the changeset description is accurate. LGTM to merge once you decide whether to add the blank-distinctId test.

Tip: You can customize Greptile's behavior for this repo with .greptile/rules.md and .greptile/config.json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants