Skip to content

fix: replay onFeatureFlags to a late listener without waiting for the flags request - #948

Merged
turnipdabeets merged 4 commits into
chore/v4-remove-deprecatedfrom
fix/v4-onfeatureflags-replay-prompt
Nov 9, 2026
Merged

turnipdabeets merged 4 commits into
chore/v4-remove-deprecatedfrom
fix/v4-onfeatureflags-replay-prompt

Conversation

@turnipdabeets

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

Fixes an unreleased 4.0 bug in onFeatureFlags (part of #871).

A listener registered after setup() gets a catch-up replay of the last delivered flags. That replay was queued on the remote config thread first, so it waited behind setup's in-flight /config + /flags request. With bootstrap flags and a slow network, the late listener got nothing until the request finished and then only the network result, so it never saw the bootstrapped values. The on-feature-flags spec says Android 4.0 calls the callback with the current values once flags are already loaded, and iOS 4.0 replays immediately on the main thread.

The replay now goes straight to the listener executor (the main thread on Android). Delivery still reads the latest value when it runs and skips a value it already delivered, so a load that lands right after is delivered once, after the replay, with no duplicate or stale value. Listener exceptions are still caught in deliverFeatureFlags.

On the plain JVM (no listener executor), listeners are documented to run on the remote config thread, so the replay still queues there and can still wait for an in-flight request. Fixing that needs a dedicated listener thread on the JVM, which is left out of this change.

No changeset: onFeatureFlags() is new in 4.0 and already covered by the 4.0 changeset.

💚 How did you test it?

  • New onFeatureFlags replays bootstrapped flags to a late listener while setup's load is in flight uses a mock server that holds /config and /flags. It fails on the base branch (no delivery within 2 s) and passes with the fix. It replaces the old test, which used an unreachable host that fails instantly and so hid the bug.
  • New onFeatureFlags delivers a load that lands before a late listener's replay runs once covers ordering: the listener gets only the network values, once.
  • ./gradlew spotlessCheck :posthog:apiCheck :posthog:test: all 1250 tests pass.

📝 Checklist

  • All commits are signed (see Commit signing)
  • 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.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Claude Code found the bug while reviewing 4.0's flag caching. A throwaway test confirmed it, and Claude Code then fixed it. I considered delivering the replay synchronously on the caller's thread for the JVM too, but turned it down: it would need a per-listener lock held across user callbacks (a deadlock risk) and would contradict the documented JVM thread.

🤖 Generated with Claude Code

… flags request

The catch-up replay for a listener registered after setup() hopped onto the
remote config thread first, so it queued behind an in-flight /config or /flags
request. With bootstrap flags and a slow network, the listener never saw the
bootstrapped values. Post the replay straight to the listener executor (the
main thread on Android) instead; delivery still reads the latest value at run
time and skips one already delivered, so a load landing right after is
delivered once, after the replay.
@turnipdabeets turnipdabeets self-assigned this Nov 6, 2026
@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

posthog-android Compliance Report

Date: 2026-11-08T20:11:09.284656+00:00
Duration: 477579ms

✅ All Tests Passed!

232/232 tests passed


Capture_V1 Tests

✅ 105/105 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint ✅ 425ms
Endpoint And Method.Does Not Use Legacy Endpoints ✅ 31ms
Required Headers.Has Authorization Bearer Header ✅ 30ms
Required Headers.Has Content Type Json ✅ 24ms
Required Headers.Has Posthog Sdk Info Format ✅ 30ms
Required Headers.Has Posthog Attempt Header ✅ 27ms
Required Headers.Has Posthog Request Id ✅ 24ms
Required Headers.Has Posthog Request Timestamp ✅ 30ms
Required Headers.Has User Agent ✅ 22ms
Body Format.Body Has Created At And Batch ✅ 26ms
Body Format.No Api Key In Body ✅ 24ms
Body Format.No Sent At In Body ✅ 23ms
Event Format.Event Has Required Root Fields ✅ 25ms
Event Format.Event Uuid Is Valid ✅ 22ms
Event Format.Event Timestamp Is Rfc3339 ✅ 23ms
Event Format.Non Utc Event Timestamp Is Converted To Utc ✅ 23ms
Event Format.Distinct Id Is String ✅ 21ms
Event Format.Distinct Id At Root Not Properties ✅ 25ms
Event Format.Custom Properties Preserved ✅ 24ms
Event Format.Set Properties Preserved ✅ 23ms
Event Format.Set Once Properties Preserved ✅ 26ms
Event Format.Groups Properties Preserved ✅ 20ms
Event Format.Sdk Generates Uuid If Not Provided ✅ 24ms
Event Format.Event Has Required Root Fields Batch ✅ 29ms
Event Format.Event Uuid Is Valid Batch ✅ 29ms
Event Format.Event Timestamp Is Rfc3339 Batch ✅ 29ms
Event Format.Distinct Id Is String Batch ✅ 25ms
Event Format.Distinct Id At Root Not Properties Batch ✅ 25ms
Event Format.Custom Properties Preserved Batch ✅ 29ms
Event Format.Set Properties Preserved Batch ✅ 38ms
Event Format.Set Once Properties Preserved Batch ✅ 28ms
Event Format.Groups Properties Preserved Batch ✅ 30ms
Event Format.Sdk Generates Uuid If Not Provided Batch ✅ 29ms
Batch Behavior.Multiple Events In Single Batch ✅ 32ms
Batch Behavior.Batch Envelope Smoke ✅ 30ms
Batch Behavior.Flush With No Events Sends Nothing ✅ 19ms
Batch Behavior.Flush At Triggers Batch ✅ 1028ms
Batch Behavior.Created At Reflects Batch Creation Time ✅ 23ms
Deduplication.Generates Unique Uuids ✅ 37ms
Deduplication.Different Events Same Content Different Uuids ✅ 26ms
Deduplication.Preserves Uuid On Retry ✅ 7016ms
Deduplication.Preserves Timestamp On Retry ✅ 7021ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry ✅ 7024ms
Deduplication.No Duplicate Events In Batch ✅ 36ms
Header Behavior On Retry.Attempt Header Starts At One ✅ 19ms
Header Behavior On Retry.Attempt Header Increments On Retry ✅ 12020ms
Header Behavior On Retry.Request Id Preserved On Retry ✅ 7016ms
Header Behavior On Retry.Different Requests Have Different Request Ids ✅ 2027ms
Header Behavior On Retry.Request Timestamp Changes On Retry ✅ 7015ms
Response Format Validation.Success Response Has Uuid Keyed Results ✅ 16ms
Response Format Validation.Success Response Has Ok For Each Event ✅ 21ms
Response Format Validation.Success No Retry After When All Ok ✅ 20ms
Response Format Validation.Success Retry After Present When Retry Events ✅ 2012ms
Response Format Validation.Success No Retry After When Drop Only ✅ 20ms
Response Format Validation.Response Echoes Request Id ✅ 17ms
Retry Behavior.Retries On 408 ✅ 7014ms
Retry Behavior.Retries On 500 ✅ 7017ms
Retry Behavior.Retries On 503 ✅ 7021ms
Retry Behavior.Retries On 504 ✅ 7017ms
Retry Behavior.Retries On 404 ✅ 7013ms
Retry Behavior.Retries On 429 ✅ 6017ms
Retry Behavior.Retries On 502 ✅ 7016ms
Retry Behavior.Retryable Errors Have Retry After ✅ 4014ms
Retry Behavior.Respects Retry After On Retryable Error ✅ 10018ms
Retry Behavior.Does Not Retry On 400 ✅ 4020ms
Retry Behavior.Does Not Retry On 401 ✅ 4019ms
Retry Behavior.Does Not Retry On 402 ✅ 4017ms
Retry Behavior.Does Not Retry On 413 ✅ 4018ms
Retry Behavior.Does Not Retry On 415 ✅ 4025ms
Retry Behavior.Non Retryable Errors Have No Retry After ✅ 4018ms
Retry Behavior.Implements Backoff ✅ 14029ms
Partial Batch Handling.Handles 200 Full Success ✅ 2020ms
Partial Batch Handling.Handles 200 With All Ok ✅ 3023ms
Partial Batch Handling.Does Not Retry Dropped Events ✅ 3020ms
Partial Batch Handling.Does Not Retry Warning Events ✅ 3023ms
Partial Batch Handling.Prunes Ok Events On Partial Retry ✅ 7015ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry ✅ 7016ms
Partial Batch Handling.Retries Only Retry Events From Partial ✅ 7015ms
Partial Batch Handling.Partial Retry Preserves Uuids ✅ 7016ms
Partial Batch Handling.Partial Retry Attempt Header Increments ✅ 7015ms
Partial Batch Handling.Partial Retry Request Id Preserved ✅ 7017ms
Partial Batch Handling.Respects Retry After On Partial ✅ 7023ms
Partial Batch Handling.Unknown Result Treated As Terminal ✅ 3021ms
Partial Batch Handling.Mixed Ok Drop Warning No Retry ✅ 3026ms
Compression.Sends Gzip Content Encoding ✅ 16ms
Compression.No Content Encoding When Disabled ✅ 18ms
Compression.Compressed Body Is Decompressible ✅ 17ms
Error Handling.Does Not Retry On Unknown 4Xx ✅ 4018ms
Event Options.Cookieless Mode Override ✅ 15ms
Event Options.Disable Skew Correction Override ✅ 15ms
Event Options.Process Person Profile Override ✅ 15ms
Event Options.Product Tour Id Override ✅ 17ms
Event Options.Unset Options Omitted ✅ 16ms
Event Options.Options Override In Batch ✅ 23ms
Event Options.Unknown Option Passes Through ✅ 17ms
Event Options.Option Value Is Not Converted ✅ 17ms
Event Options.Lenient Bool Option Values Are Not Converted ✅ 19ms
Event Options.Option Wins Over Legacy Property ✅ 17ms
Event Options.Legacy Property Fills Unset Option ✅ 19ms
Event Options.Null Option Falls Back To Legacy Property ✅ 21ms
Property Layers.Caller Property Beats Registered Property ✅ 17ms
Property Layers.Before Send Runs Last ✅ 17ms
Fail Open.Empty Api Key Sends Nothing ✅ 3011ms
Fail Open.Personal Api Key Sends Nothing ✅ 3011ms
Geoip And Historical Migration.Historical Migration Absent By Default ✅ 15ms

Capture_Ai_V1 Tests

✅ 110/110 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint ✅ 18ms
Endpoint And Method.Does Not Use Legacy Endpoints ✅ 15ms
Endpoint And Method.Capture Does Not Reroute Ai Named Events ✅ 17ms
Endpoint And Method.Capture Ai Does Not Reroute Non Ai Events ✅ 16ms
Lane Isolation.Failing Ai Lane Does Not Delay Analytics ✅ 7026ms
Required Headers.Has Authorization Bearer Header ✅ 16ms
Required Headers.Has Content Type Json ✅ 16ms
Required Headers.Has Posthog Sdk Info Format ✅ 13ms
Required Headers.Has Posthog Attempt Header ✅ 16ms
Required Headers.Has Posthog Request Id ✅ 14ms
Required Headers.Has Posthog Request Timestamp ✅ 12ms
Required Headers.Has User Agent ✅ 12ms
Body Format.Body Has Created At And Batch ✅ 14ms
Body Format.No Api Key In Body ✅ 13ms
Body Format.No Sent At In Body ✅ 13ms
Event Format.Event Has Required Root Fields ✅ 15ms
Event Format.Event Uuid Is Valid ✅ 15ms
Event Format.Event Timestamp Is Rfc3339 ✅ 15ms
Event Format.Non Utc Event Timestamp Is Converted To Utc ✅ 15ms
Event Format.Distinct Id Is String ✅ 15ms
Event Format.Distinct Id At Root Not Properties ✅ 14ms
Event Format.Custom Properties Preserved ✅ 14ms
Event Format.Set Properties Preserved ✅ 20ms
Event Format.Set Once Properties Preserved ✅ 15ms
Event Format.Groups Properties Preserved ✅ 16ms
Event Format.Sdk Generates Uuid If Not Provided ✅ 18ms
Event Format.Event Has Required Root Fields Batch ✅ 20ms
Event Format.Event Uuid Is Valid Batch ✅ 20ms
Event Format.Event Timestamp Is Rfc3339 Batch ✅ 16ms
Event Format.Distinct Id Is String Batch ✅ 19ms
Event Format.Distinct Id At Root Not Properties Batch ✅ 19ms
Event Format.Custom Properties Preserved Batch ✅ 20ms
Event Format.Set Properties Preserved Batch ✅ 21ms
Event Format.Set Once Properties Preserved Batch ✅ 21ms
Event Format.Groups Properties Preserved Batch ✅ 26ms
Event Format.Sdk Generates Uuid If Not Provided Batch ✅ 18ms
Batch Behavior.Multiple Events In Single Batch ✅ 27ms
Batch Behavior.Batch Envelope Smoke ✅ 28ms
Batch Behavior.Flush At Triggers Batch ✅ 1018ms
Batch Behavior.Created At Reflects Batch Creation Time ✅ 17ms
Deduplication.Generates Unique Uuids ✅ 35ms
Deduplication.Different Events Same Content Different Uuids ✅ 19ms
Deduplication.Preserves Uuid On Retry ✅ 7013ms
Deduplication.Preserves Timestamp On Retry ✅ 7014ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry ✅ 7017ms
Deduplication.No Duplicate Events In Batch ✅ 27ms
Header Behavior On Retry.Attempt Header Starts At One ✅ 16ms
Header Behavior On Retry.Attempt Header Increments On Retry ✅ 12022ms
Header Behavior On Retry.Request Id Preserved On Retry ✅ 7014ms
Header Behavior On Retry.Different Requests Have Different Request Ids ✅ 2024ms
Header Behavior On Retry.Request Timestamp Changes On Retry ✅ 7015ms
Response Format Validation.Success Response Has Uuid Keyed Results ✅ 15ms
Response Format Validation.Success Response Has Ok For Each Event ✅ 20ms
Response Format Validation.Success No Retry After When All Ok ✅ 18ms
Response Format Validation.Success Retry After Present When Retry Events ✅ 2009ms
Response Format Validation.Success No Retry After When Drop Only ✅ 17ms
Response Format Validation.Response Echoes Request Id ✅ 12ms
Retry Behavior.Retries On 408 ✅ 7013ms
Retry Behavior.Retries On 500 ✅ 7015ms
Retry Behavior.Retries On 503 ✅ 7021ms
Retry Behavior.Retries On 504 ✅ 7016ms
Retry Behavior.Retries On 404 ✅ 7015ms
Retry Behavior.Retries On 429 ✅ 7014ms
Retry Behavior.Retries On 502 ✅ 7015ms
Retry Behavior.Retryable Errors Have Retry After ✅ 4012ms
Retry Behavior.Respects Retry After On Retryable Error ✅ 10022ms
Retry Behavior.Does Not Retry On 400 ✅ 4019ms
Retry Behavior.Does Not Retry On 401 ✅ 4017ms
Retry Behavior.Does Not Retry On 402 ✅ 4017ms
Retry Behavior.Does Not Retry On 413 ✅ 4015ms
Retry Behavior.Does Not Retry On 415 ✅ 4018ms
Retry Behavior.Non Retryable Errors Have No Retry After ✅ 4016ms
Retry Behavior.Implements Backoff ✅ 14026ms
Partial Batch Handling.Handles 200 Full Success ✅ 2013ms
Partial Batch Handling.Handles 200 With All Ok ✅ 3022ms
Partial Batch Handling.Does Not Retry Dropped Events ✅ 3020ms
Partial Batch Handling.Does Not Retry Warning Events ✅ 3020ms
Partial Batch Handling.Prunes Ok Events On Partial Retry ✅ 7015ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry ✅ 7015ms
Partial Batch Handling.Retries Only Retry Events From Partial ✅ 7015ms
Partial Batch Handling.Partial Retry Preserves Uuids ✅ 7015ms
Partial Batch Handling.Partial Retry Attempt Header Increments ✅ 7015ms
Partial Batch Handling.Partial Retry Request Id Preserved ✅ 7015ms
Partial Batch Handling.Respects Retry After On Partial ✅ 7017ms
Partial Batch Handling.Unknown Result Treated As Terminal ✅ 3020ms
Partial Batch Handling.Mixed Ok Drop Warning No Retry ✅ 3023ms
Compression.Sends Gzip Content Encoding ✅ 14ms
Compression.No Content Encoding When Disabled ✅ 14ms
Compression.Compressed Body Is Decompressible ✅ 15ms
Error Handling.Does Not Retry On Unknown 4Xx ✅ 4013ms
Event Options.Cookieless Mode Override ✅ 13ms
Event Options.Disable Skew Correction Override ✅ 12ms
Event Options.Process Person Profile Override ✅ 13ms
Event Options.Product Tour Id Override ✅ 13ms
Event Options.Unset Options Omitted ✅ 13ms
Event Options.Options Override In Batch ✅ 17ms
Event Options.Unknown Option Passes Through ✅ 13ms
Event Options.Option Value Is Not Converted ✅ 14ms
Event Options.Lenient Bool Option Values Are Not Converted ✅ 13ms
Event Options.Option Wins Over Legacy Property ✅ 13ms
Event Options.Legacy Property Fills Unset Option ✅ 14ms
Event Options.Null Option Falls Back To Legacy Property ✅ 13ms
Event Size.Event Over Ceiling Is Not Sent ✅ 3371ms
Event Size.Over Ceiling Drop Is Reported Or Logged Without Content ✅ 2054ms
Event Size.Batches Close Before Byte Target ✅ 6094ms
Geoip And Historical Migration.Historical Migration Absent By Default ✅ 15ms
Property Layers.Caller Property Beats Registered Property ✅ 14ms
Property Layers.Before Send Runs Last ✅ 16ms
Fail Open.Empty Api Key Sends Nothing ✅ 1011ms
Fail Open.Personal Api Key Sends Nothing ✅ 1009ms

Feature_Flags Tests

✅ 17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id ✅ 32ms
Request Payload.Flags Request Uses V2 Query Param ✅ 21ms
Request Payload.Flags Request Hits Flags Path Not Decide ✅ 19ms
Request Payload.Flags Request Omits Authorization Header ✅ 19ms
Request Payload.Token In Flags Body Matches Init ✅ 17ms
Request Payload.Groups Round Trip ✅ 18ms
Request Payload.Groups Default To Empty Object ✅ 16ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False ✅ 19ms
Request Payload.Disable Geoip Omitted Defaults To False ✅ 13ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key ✅ 14ms
Request Lifecycle.No Flags Request On Init Alone ✅ 7ms
Request Lifecycle.No Flags Request On Normal Capture ✅ 13ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests ✅ 24ms
Request Lifecycle.Mock Response Value Is Returned To Caller ✅ 19ms
Retry Behavior.Retries Flags On 502 ✅ 320ms
Retry Behavior.Retries Flags On 504 ✅ 318ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event ✅ 16ms

@turnipdabeets
turnipdabeets marked this pull request as ready for review November 6, 2026 23:19
@turnipdabeets
turnipdabeets requested a review from a team as a code owner November 6, 2026 23:19

@marandaneto marandaneto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The code looks good; no qualifying findings. Verdict: correct.

@turnipdabeets
turnipdabeets merged commit d6ce5b3 into chore/v4-remove-deprecated Nov 9, 2026
12 checks passed
@turnipdabeets
turnipdabeets deleted the fix/v4-onfeatureflags-replay-prompt branch November 9, 2026 14:29
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