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(replay): keep waiting while snapshots load during json export (#125517)

Tue Haulund3114d0e333f7
diff --git a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.test.ts b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.test.ts
index 9a7509bbe1d3..340687003095 100644
--- a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.test.ts
+++ b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.test.ts
@@ -1,9 +1,15 @@
+import { MOCK_DEFAULT_ORGANIZATION, MOCK_DEFAULT_USER } from 'lib/api.mock'
+
import { router } from 'kea-router'
import { expectLogic } from 'kea-test-utils'
+import { HttpResponse } from 'msw'
import posthog from 'posthog-js'
import { EventType, IncrementalSource, eventWithTime } from 'posthog-js/rrweb-types'
+import { lemonToast } from '@posthog/lemon-ui'
+
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
+import * as domUtils from 'lib/utils/dom'
import { playerSettingsLogic } from 'scenes/session-recordings/player/playerSettingsLogic'
import { sessionRecordingDataCoordinatorLogic } from 'scenes/session-recordings/player/sessionRecordingDataCoordinatorLogic'
import * as sessionRecordingDataCoordinatorLogicModule from 'scenes/session-recordings/player/sessionRecordingDataCoordinatorLogic'
@@ -13,9 +19,10 @@ import {
} from 'scenes/session-recordings/player/sessionRecordingPlayerLogic'
import { makeLogger } from 'scenes/session-recordings/player/utils/player-logging'
import { urls } from 'scenes/urls'
+import { userLogic } from 'scenes/userLogic'
import { resumeKeaLoadersErrors, silenceKeaLoadersErrors } from '~/initKea'
-import { ExporterFormat, RecordingSegment, RecordingSnapshot, SessionPlayerState } from '~/types'
+import { AvailableFeature, ExporterFormat, RecordingSegment, RecordingSnapshot, SessionPlayerState } from '~/types'
import { isUsableHeatmapUrl } from 'products/web_analytics/frontend/heatmaps/replayIframeData'
@@ -26,6 +33,7 @@ import {
overrideSessionRecordingMocks,
recordingMetaJson,
setupSessionRecordingTest,
+ snapshotsAsJSONLines,
} from './__mocks__/test-setup'
import {
findNewEvents,
@@ -1804,6 +1812,80 @@ describe('sessionRecordingPlayerLogic', () => {
})
})
+ describe('exportRecordingToFile', () => {
+ it.each([
+ {
+ description: 'exports the recording after a snapshot request that takes 20 s',
+ requestMs: 20000,
+ expected: 'exported',
+ },
+ {
+ description: 'fails when a snapshot request never answers',
+ requestMs: null,
+ expected: 'Timeout waiting for recording to load',
+ },
+ ])('$description', async ({ requestMs, expected }) => {
+ let releaseSnapshots = (): void => {}
+ const snapshotsReleased = new Promise<void>((resolve) => {
+ releaseSnapshots = resolve
+ })
+ jest.useFakeTimers()
+ logic.unmount()
+ setupSessionRecordingTest({
+ getMocks: {
+ '/api/environments/:team_id/session_recordings/:id/snapshots': async ({ request }) => {
+ if (new URL(request.url).searchParams.get('source') === 'blob_v2') {
+ await snapshotsReleased
+ return new HttpResponse(snapshotsAsJSONLines())
+ }
+ return [200, { sources: [BLOB_SOURCE_V2] }]
+ },
+ },
+ })
+ featureFlagLogic.mount()
+ logic = sessionRecordingPlayerLogic({
+ sessionRecordingId: '2',
+ playerKey: 'test',
+ blobV2PollingDisabled: true,
+ })
+ logic.mount()
+ userLogic.actions.loadUserSuccess({
+ ...MOCK_DEFAULT_USER,
+ organization: {
+ ...MOCK_DEFAULT_ORGANIZATION,
+ available_product_features: [
+ { key: AvailableFeature.RECORDINGS_FILE_EXPORT, name: 'Recordings file export' },
+ ],
+ },
+ })
+ await jest.advanceTimersByTimeAsync(1000)
+
+ const downloadFileSpy = jest.spyOn(domUtils, 'downloadFile').mockImplementation(() => {})
+ const toastSpy = jest.spyOn(lemonToast, 'promise').mockImplementation((promise) => promise.catch(() => {}))
+ try {
+ // The mounted player component marks the player active, which allows snapshot loading.
+ logic.actions.setPlayerActive(true)
+ logic.actions.exportRecordingToFile()
+ const exportOutcome = (toastSpy.mock.calls[0][0] as Promise<void>).then(
+ () => 'exported',
+ (error: Error) => error.message
+ )
+
+ await jest.advanceTimersByTimeAsync(requestMs ?? 125000)
+ if (requestMs !== null) {
+ releaseSnapshots()
+ }
+ await jest.advanceTimersByTimeAsync(5000)
+
+ expect(await exportOutcome).toBe(expected)
+ } finally {
+ toastSpy.mockRestore()
+ downloadFileSpy.mockRestore()
+ jest.useRealTimers()
+ }
+ })
+ })
+
describe('exportRecording', () => {
it('uses the player skip-inactivity setting', () => {
// setRootFrame clears innerHTML, so append the iframe after it runs
diff --git a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts
index 73b0705cdb59..74a042012d15 100644
--- a/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts
+++ b/frontend/src/scenes/session-recordings/player/sessionRecordingPlayerLogic.ts
@@ -772,6 +772,7 @@ export interface sessionRecordingPlayerLogicValues {
url: string
}[] // sessionRecordingDataCoordinatorLogic
allSourcesLoaded: boolean // snapshotDataLogic
+ isLoadingSnapshots: boolean // snapshotDataLogic
isSnapshotUnauthorized: boolean // snapshotDataLogic
replayProxyToken: string | null // snapshotDataLogic
snapshotSources: SessionRecordingSnapshotSource[] | null // snapshotDataLogic
@@ -1469,14 +1470,18 @@ function encodeRecording(recording: ExportedSessionRecordingFileV2): string[] {
}
async function loadAllSnapshots(
- values: Pick<sessionRecordingPlayerLogicValues, 'sessionPlayerData'>,
+ values: Pick<sessionRecordingPlayerLogicValues, 'sessionPlayerData' | 'isLoadingSnapshots'>,
actions: { loadAllSources: () => void; loadNextSnapshotSource: () => void }
): Promise<void> {
actions.loadAllSources()
const delayTime = 1000
const maxStallIterations = 15
+ // One request fetches a whole batch of sources, so a slow request adds no snapshots for a long time.
+ // A request in flight gets this higher limit, which still stops the export when a request never answers.
+ const maxLoadingStallIterations = 120
let stallCount = 0
+ let loadingStallCount = 0
let lastSnapshotCount = 0
while (!values.sessionPlayerData.fullyLoaded) {
const currentCount = values.sessionPlayerData.snapshotsByWindowId
@@ -1484,11 +1489,14 @@ async function loadAllSnapshots(
: 0
if (currentCount > lastSnapshotCount) {
stallCount = 0
+ loadingStallCount = 0
lastSnapshotCount = currentCount
+ } else if (values.isLoadingSnapshots) {
+ loadingStallCount++
} else {
stallCount++
}
- if (stallCount >= maxStallIterations) {
+ if (stallCount >= maxStallIterations || loadingStallCount >= maxLoadingStallIterations) {
throw new Error('Timeout waiting for recording to load')
}
actions.loadNextSnapshotSource()
@@ -1516,6 +1524,7 @@ export const sessionRecordingPlayerLogic = kea<sessionRecordingPlayerLogicType>(
'allSourcesLoaded',
'storeVersion',
'isSnapshotUnauthorized',
+ 'isLoadingSnapshots',
],
sessionRecordingDataCoordinatorLogic(props),
[