Repository navigation
fix: buffer session replay snapshots until first remote config resolves - #590
Conversation
posthog-android Compliance ReportDate: 2026-07-02 10:00:45 UTC ✅ All Tests Passed!45/45 tests passed Capture Tests✅ 29/29 tests passed View Details
Feature_Flags Tests✅ 16/16 tests passed View Details
|
|
|
Reviews (2): Last reviewed commit: "fix: keep session replay opening window ..." | Re-trigger Greptile |
…on on first config
…st session replay config
…ack to first config
…d on a linked feature flag
a03f9b9 to
b202c07
Compare
|
Reviews (3): Last reviewed commit: "fix: resolve session replay buffer on th..." | Re-trigger Greptile |
|
We have the same issue with error tracking, surveys, etc, everything that depends on remote config, right? |
Yeah I'll do a rundown later today on this and open some issues if needed |
💡 Motivation and Context
Session replay starts at cold start from the disk-cached
sessionRecordingconfig, set before any network call. Remote-config resolution is async, so when the cached decision disagrees with the freshly-fetched config (cached-on, fresh-off) there is a stale-cache window where the SDK records — and uploads — a session the fresh config disallows. Fresh installs are already safe, so this is a returning-user bug. There was also a latent gap where a mid-session fresh "off" only took effect at the next session rotation.This is the Android slice of a multi-target session-replay fix (iOS counterpart: PostHog/posthog-ios#679). Tracking issue: #575.
Recording still starts optimistically off the cached value, but snapshots are buffered (not persisted, not sent) until the first live remote config resolves; on resolve the buffered window migrates to the replay queue only if the current session is recordable under the fresh config, and is dropped otherwise. No stale-cache
$snapshotreaches the persisted queue or the network.What changed
Buffer-and-decide gate (
PostHogReplayIntegration)awaitingFirstRemoteConfiggate (guarded bybufferingLock) routes captured snapshots into the in-memory buffer until the first/configattempt resolves, then disarms exactly once. The gate is only armed when a remote-config fetch will actually happen — if neitherremoteConfignorpreloadFeatureFlagsis enabled, nothing would ever resolve it, so it stays disarmed and snapshots persist normally.minimumDurationcan't drain stale-cache frames to the persisted queue before the fresh decision lands. A snapshot that reaches the persist boundary while replay is inactive is dropped rather than enqueued.Resolve uses the full recordability decision, not just the flag
minimumDuration.add()racing the disarm finds the buffer cleared and replay inactive and drops its snapshot.linkedFlag handling — single-callback on Android
/flagscallback after/configand defers the resolve to it), Android'sloadRemoteConfigloads/flagsnested and synchronously (notifyRemoteConfigLoaded = false) and fires exactly oneonRemoteConfig. The linked flag is already merged and fresh at that single callback, so Android resolves the buffer there directly. There is no second callback to defer to, and deferring would leave the gate armed for the whole session (silently buffering, never sending). This is the one place the Android realization intentionally diverges from the iOS deferral.Mid-session re-evaluation
onRemoteConfigre-evaluates recording: a subsequent config that turns recording off stops it promptly instead of waiting for session rotation; one that turns it back on resumes recording for the current session.stopIfActiveflips the active flag synchronously before postingstop(), so a concurrentadd()can't persist a snapshot after the off decision.Concurrency / reset
bufferingGenerationcounter lets an in-flight resolve detect a concurrent session rotation (reset()/ identity change re-arming and clearing the buffer) and no-op instead of migrating or clearing the wrong session's window.reset()on the next/flagsreload, so the post-reset opening window isn't dropped.Failure / quota paths (
PostHogRemoteConfig)/config(offline launch) falls back to the disk-cached decision and emits a single fallback log line./flagsresponse resolves via the not-recordable path (a live response, not a fetch failure) and re-arms the session-recording / error-tracking / capture-performance decisions from the cached config instead of losing them.hasRemoteConfigFetched(), reset onclear().The in-memory buffer is intentionally not size-capped — replay needs the complete opening snapshot sequence, so a frame-dropping cap would migrate an unplayable partial window. A slow or absent
/configcan grow it until the config resolves; it is bounded by session activity and self-heals on the next resolution.💚 How did you test it?
Unit tests (
./gradlew :posthog:test :posthog-android:testDebugUnitTest :posthog-android:testReleaseUnitTest spotlessCheck apiCheck— all green), covering the gate lifecycle inPostHogReplayIntegrationTestand the remote-config paths inPostHogRemoteConfigTest/PostHogTest:add()routes snapshots to the buffer, not the persisted queue;onRemoteConfigdelivery resolves the gate — flag-on migrates the buffered window, and a snapshot added afterwards persists directly (no second callback needed);stop()runs is not persisted;remoteConfignorpreloadFeatureFlagswill fetch;hasRemoteConfigFetched()flips true on both the/configand/flagspaths and resets onclear(); a quota-limited flags reload resolves and notifies.Manual runs on the sample app against a local mock server that flips the configs. No manual on-device testing was performed.
📝 Checklist
If releasing new changes
pnpm changesetto generate a changeset file🤖 Agent context
DRI: @ioannisj
Autonomy: Human-driven (agent-assisted)