Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix session replay storage race conditions

Blue Mousee72e8366b991
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index 76c190170..0dcc94ba3 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -2088,7 +2088,13 @@ export class AnalyticsService {
switch (Number(result)) {
case 1:
- return { ...keys, chunkIndex, eventCount, uncompressedBytes }
+ return {
+ ...keys,
+ chunkIndex,
+ eventCount,
+ uncompressedBytes,
+ duplicate: false,
+ }
case -1:
throw new PayloadTooLargeException('Session replay has too many chunks')
case -2:
@@ -2096,7 +2102,13 @@ export class AnalyticsService {
case -3:
throw new PayloadTooLargeException('Session replay is too large')
case -4:
- return { ...keys, chunkIndex, eventCount, uncompressedBytes }
+ return {
+ ...keys,
+ chunkIndex,
+ eventCount,
+ uncompressedBytes,
+ duplicate: true,
+ }
case -5:
throw new PayloadTooLargeException('Session replay is too long')
default:
@@ -2184,6 +2196,18 @@ export class AnalyticsService {
retention,
timestamps,
)
+
+ if (chunkReservation.duplicate) {
+ return {
+ replayId,
+ psid,
+ chunkIndex,
+ eventCount: events.length,
+ ...retention,
+ countedUsage: false,
+ }
+ }
+
const objectKey = this.getSessionReplayObjectKey(
pid,
psid,
@@ -2414,10 +2438,6 @@ export class AnalyticsService {
const objectKeys = chunks.map((chunk) => chunk.objectKey).filter(Boolean)
- await mapLimit(objectKeys, SESSION_REPLAY_CHUNK_FETCH_CONCURRENCY, (key) =>
- this.sessionReplayStorage.deleteObject(key),
- )
-
await clickhouse.command({
query: `
ALTER TABLE session_replay_chunks
@@ -2426,8 +2446,13 @@ export class AnalyticsService {
AND replayId = {replayId:String}
`,
query_params: { pid, psid, replayId: selectedReplayId },
+ clickhouse_settings: { mutations_sync: '2' },
})
+ await mapLimit(objectKeys, SESSION_REPLAY_CHUNK_FETCH_CONCURRENCY, (key) =>
+ this.sessionReplayStorage.deleteObject(key),
+ )
+
return { deleted: true, deletedChunks: objectKeys.length }
}
diff --git a/packages/tracker-js/jest.config.js b/packages/tracker-js/jest.config.js
index b785309f4..3e34bec33 100644
--- a/packages/tracker-js/jest.config.js
+++ b/packages/tracker-js/jest.config.js
@@ -2,6 +2,7 @@ export default {
preset: 'ts-jest',
testEnvironment: '<rootDir>/tests/jsdomEnvironment.ts',
extensionsToTreatAsEsm: ['.ts'],
+ watchman: false,
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
@@ -10,6 +11,7 @@ export default {
'ts-jest',
{
useESM: true,
+ tsconfig: 'tsconfig.test.json',
},
],
},
diff --git a/packages/tracker-js/src/Lib.ts b/packages/tracker-js/src/Lib.ts
index 0b8cbb593..a42bb85a6 100644
--- a/packages/tracker-js/src/Lib.ts
+++ b/packages/tracker-js/src/Lib.ts
@@ -314,6 +314,7 @@ export class Lib {
private cachedData: CachedData | null = null
private rrwebLoader: Promise<void> | null = null
private sessionReplayActions: SessionReplayActions | null = null
+ private sessionReplayInitPromise: Promise<SessionReplayActions> | null = null
constructor(private projectID: string, private options?: LibOptions) {
this.trackPathChange = this.trackPathChange.bind(this)
@@ -790,6 +791,29 @@ export class Lib {
return this.sessionReplayActions
}
+ if (this.sessionReplayInitPromise) {
+ return this.sessionReplayInitPromise
+ }
+
+ const initPromise = this.initialiseSessionReplay(options)
+ this.sessionReplayInitPromise = initPromise
+
+ try {
+ return await initPromise
+ } finally {
+ if (this.sessionReplayInitPromise === initPromise) {
+ this.sessionReplayInitPromise = null
+ }
+ }
+ }
+
+ private async initialiseSessionReplay(
+ options: SessionReplayOptions,
+ ): Promise<SessionReplayActions> {
+ if (this.sessionReplayActions) {
+ return this.sessionReplayActions
+ }
+
if (!this.canTrack()) {
return defaultSessionReplayActions
}
@@ -910,6 +934,7 @@ export class Lib {
stopRecording?.()
await flush()
this.sessionReplayActions = null
+ this.sessionReplayInitPromise = null
}
const resetIdleTimer = () => {
if (!idleTimeoutMs || stopped) return
diff --git a/packages/tracker-js/tests/sessionReplay.test.ts b/packages/tracker-js/tests/sessionReplay.test.ts
index b0c884da5..124b052dc 100644
--- a/packages/tracker-js/tests/sessionReplay.test.ts
+++ b/packages/tracker-js/tests/sessionReplay.test.ts
@@ -145,6 +145,28 @@ describe('Session replay tracking', () => {
await actions.stop()
})
+ test('concurrent startSessionReplay calls share one recorder', async () => {
+ const { stopRecording } = usePackageRrweb()
+ const { init, startSessionReplay } = await loadTracker()
+
+ init(PROJECT_ID, { devMode: true })
+ const [firstActions, secondActions] = await Promise.all([
+ startSessionReplay({ flushIntervalMs: 60_000 }),
+ startSessionReplay({ flushIntervalMs: 10_000 }),
+ ])
+
+ expect(firstActions).toBe(secondActions)
+ expect(mockRrwebRecord).toHaveBeenCalledTimes(1)
+ expect(
+ fetchMock.mock.calls.filter(([url]) =>
+ String(url).includes('/session-replay/start'),
+ ),
+ ).toHaveLength(1)
+
+ await firstActions.stop()
+ expect(stopRecording).toHaveBeenCalledTimes(1)
+ })
+
test('sampleRate can skip recording before loading rrweb', async () => {
const { init, startSessionReplay } = await loadTracker()
diff --git a/packages/tracker-js/tsconfig.test.json b/packages/tracker-js/tsconfig.test.json
new file mode 100644
index 000000000..4e5d5e168
--- /dev/null
+++ b/packages/tracker-js/tsconfig.test.json
@@ -0,0 +1,7 @@
+{
+ "extends": "./tsconfig.json",
+ "compilerOptions": {
+ "types": ["jest", "node"]
+ },
+ "include": ["src", "tests"]
+}
diff --git a/web/app/pages/Project/tabs/Replays/ReplaysView.tsx b/web/app/pages/Project/tabs/Replays/ReplaysView.tsx
index 93598c23e..5fac94875 100644
--- a/web/app/pages/Project/tabs/Replays/ReplaysView.tsx
+++ b/web/app/pages/Project/tabs/Replays/ReplaysView.tsx
@@ -303,6 +303,9 @@ const ReplaysViewInner = ({
const removeReplayFromList = (psid: string, replayId: string) => {
const removedReplayKey = `${psid}:${replayId}`
+ const removed = replays.some(
+ (replay) => `${replay.psid}:${replay.replayId}` === removedReplayKey,
+ )
setReplays((prev) => {
const next = prev.filter(
@@ -311,7 +314,9 @@ const ReplaysViewInner = ({
hasShownContentRef.current = !_isEmpty(next)
return next
})
- setReplaysSkip((prev) => Math.max(0, prev - 1))
+ if (removed) {
+ setReplaysSkip((prev) => Math.max(0, prev - 1))
+ }
}
const deleteReplay = async () => {