: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(replay): guard breakpoint cancellation in two kea loaders (#77226)
posthog[bot]a14e28eb95e0
diff --git a/frontend/src/queries/nodes/DataNode/dataNodeLogic.loadFilteredCount.test.ts b/frontend/src/queries/nodes/DataNode/dataNodeLogic.loadFilteredCount.test.tsnew file mode 100644index 000000000000..2518b210e77c--- /dev/null+++ b/frontend/src/queries/nodes/DataNode/dataNodeLogic.loadFilteredCount.test.ts@@ -0,0 +1,68 @@+import { expectLogic } from 'kea-test-utils'+import posthog from 'posthog-js'++import { featureFlagLogic } from 'lib/logic/featureFlagLogic'++import { dataNodeLogic } from '~/queries/nodes/DataNode/dataNodeLogic'+import * as queryModule from '~/queries/query'+import { ActorsQuery, NodeKind } from '~/queries/schema/schema-general'+import { initKeaTests } from '~/test/init'++const testUniqueKey = 'testFilteredCountKey'++describe('dataNodeLogic - loadFilteredCount', () => {+ let logic: ReturnType<typeof dataNodeLogic.build>++ beforeEach(() => {+ initKeaTests()+ featureFlagLogic.mount()+ jest.spyOn(posthog, 'captureException').mockImplementation(() => undefined)+ })++ afterEach(() => {+ logic?.unmount()+ jest.restoreAllMocks()+ })++ // Regression test: a second loadFilteredCount call supersedes the first via kea's+ // breakpoint mechanism, which throws 'kea-listeners breakpoint broke'. Without an+ // isBreakpoint guard, the superseded call's catch block reported it as a real error+ // and returned null, letting a stale or blank count land on the table.+ it('does not report a superseded loadFilteredCount call as an error', async () => {+ const query: ActorsQuery = {+ kind: NodeKind.ActorsQuery,+ select: ['id'],+ search: 'first search',+ }+ logic = dataNodeLogic({ key: testUniqueKey, query })+ logic.mount()++ let resolveFirstQuery: (value: any) => void = () => {}+ const firstQuery = new Promise((resolve) => {+ resolveFirstQuery = resolve+ })+ const performQuerySpy = jest+ .spyOn(queryModule, 'performQuery')+ .mockReturnValueOnce(firstQuery as any)+ .mockResolvedValueOnce({ results: [[5]] } as any)++ // Dispatch the first call and let its debounce clear, so it's awaiting performQuery+ // (the slow, unresolved firstQuery) when the second call comes in.+ logic.actions.loadFilteredCount()+ await new Promise((resolve) => setTimeout(resolve, 350))+ expect(performQuerySpy).toHaveBeenCalledTimes(1)++ // The second call supersedes the first via kea's breakpoint mechanism.+ logic.actions.loadFilteredCount()+ await expectLogic(logic).toDispatchActions(['loadFilteredCountSuccess'])+ expect(logic.values.filteredCount).toBe(5)++ // Now let the superseded first call's query resolve — its breakpoint() should throw+ // and be swallowed as a cancellation, not treated as a real error.+ resolveFirstQuery({ results: [[1]] })+ await new Promise((resolve) => setTimeout(resolve, 0))++ expect(posthog.captureException).not.toHaveBeenCalled()+ expect(logic.values.filteredCount).toBe(5)+ })+})diff --git a/frontend/src/queries/nodes/DataNode/dataNodeLogic.ts b/frontend/src/queries/nodes/DataNode/dataNodeLogic.tsindex cc2d8bfd8759..4fae5cc42e53 100644--- a/frontend/src/queries/nodes/DataNode/dataNodeLogic.ts+++ b/frontend/src/queries/nodes/DataNode/dataNodeLogic.ts@@ -5,6 +5,7 @@ import {afterMount,beforeUnmount,connect,+ isBreakpoint,kea,key,listeners,@@ -1383,7 +1384,10 @@ export const dataNodeLogic = kea<dataNodeLogicType>([const response = await performQuery(query)breakpoint()return response?.results?.[0]?.[0] || 0- } catch (error) {+ } catch (error: any) {+ if (isBreakpoint(error)) {+ throw error+ }posthog.captureException(error, { action: 'load filtered count in dataNodeLogic' })return null}diff --git a/frontend/src/scenes/session-recordings/player/sessionEventsDataLogic.test.ts b/frontend/src/scenes/session-recordings/player/sessionEventsDataLogic.test.tsnew file mode 100644index 000000000000..b447d53b9220--- /dev/null+++ b/frontend/src/scenes/session-recordings/player/sessionEventsDataLogic.test.ts@@ -0,0 +1,65 @@+import { expectLogic } from 'kea-test-utils'+import posthog from 'posthog-js'++import api from 'lib/api'++import { initKeaTests } from '~/test/init'+import { RecordingEventType } from '~/types'++import { sessionEventsDataLogic } from './sessionEventsDataLogic'++describe('sessionEventsDataLogic', () => {+ let logic: ReturnType<typeof sessionEventsDataLogic.build>++ const makeEvent = (id: string): RecordingEventType => ({+ id,+ event: '$pageview',+ timestamp: '2024-01-01T00:00:00Z',+ elements: [],+ properties: {},+ playerTime: 0,+ fullyLoaded: false,+ distinct_id: 'distinct-id',+ })++ beforeEach(() => {+ initKeaTests()+ logic = sessionEventsDataLogic({ sessionRecordingId: 'test-session' })+ logic.mount()+ jest.spyOn(posthog, 'captureException').mockImplementation(() => undefined)+ })++ afterEach(() => {+ logic.unmount()+ jest.restoreAllMocks()+ })++ // Regression test: a second loadFullEventData call supersedes the first via kea's+ // breakpoint mechanism, which throws 'kea-listeners breakpoint broke'. Without an+ // isBreakpoint guard, the superseded call's catch block ran anyway and marked its+ // events fullyLoaded, so their properties were never fetched again.+ it('does not mark a superseded loadFullEventData call as fully loaded, nor report it as an error', async () => {+ const eventOne = makeEvent('event-1')+ const eventTwo = makeEvent('event-2')+ logic.actions.loadEventsSuccess([eventOne, eventTwo])++ let resolveFirstQuery: (value: any) => void = () => {}+ const firstQuery = new Promise((resolve) => {+ resolveFirstQuery = resolve+ })+ jest.spyOn(api, 'queryHogQL')+ .mockReturnValueOnce(firstQuery as any)+ .mockResolvedValueOnce({ results: [[JSON.stringify({ some: 'value' }), 'event-2']] } as any)++ logic.actions.loadFullEventData(eventOne)+ logic.actions.loadFullEventData(eventTwo)++ resolveFirstQuery({ results: [[JSON.stringify({ some: 'value' }), 'event-1']] })++ await expectLogic(logic).toDispatchActions(['loadFullEventDataSuccess'])++ expect(posthog.captureException).not.toHaveBeenCalled()+ expect(logic.values.sessionEventsData?.find((e) => e.id === 'event-1')?.fullyLoaded).toBe(false)+ expect(logic.values.sessionEventsData?.find((e) => e.id === 'event-2')?.fullyLoaded).toBe(true)+ })+})diff --git a/frontend/src/scenes/session-recordings/player/sessionEventsDataLogic.ts b/frontend/src/scenes/session-recordings/player/sessionEventsDataLogic.tsindex 62ecb162cd11..a6481e769010 100644--- a/frontend/src/scenes/session-recordings/player/sessionEventsDataLogic.ts+++ b/frontend/src/scenes/session-recordings/player/sessionEventsDataLogic.ts@@ -4,6 +4,7 @@ import {actions,beforeUnmount,connect,+ isBreakpoint,kea,key,listeners,@@ -325,7 +326,10 @@ AND properties.$lib != 'web'`event.fullyLoaded = true}}- } catch (e) {+ } catch (e: any) {+ if (isBreakpoint(e)) {+ throw e+ }// NOTE: This is not ideal but should happen so rarely that it is tolerable.existingEvents.forEach((e) => (e.fullyLoaded = true))posthog.captureException(e, { feature: 'session-recording-load-full-event-data' })