: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(logs): stop live-tail poller crashing on unmount (#81084)
Frank Hamand3a29cc9d7194
diff --git a/products/logs/frontend/components/LogsViewer/data/logsViewerDataLogic.test.ts b/products/logs/frontend/components/LogsViewer/data/logsViewerDataLogic.test.tsindex 058cac02886e..a95fe99926b4 100644--- a/products/logs/frontend/components/LogsViewer/data/logsViewerDataLogic.test.ts+++ b/products/logs/frontend/components/LogsViewer/data/logsViewerDataLogic.test.ts@@ -164,6 +164,32 @@ describe('logsViewerDataLogic', () => {expect(logic.values.newLogUuids.size).toBe(0)})++ it('unmounting mid-poll does not throw ([KEA] Can not find path ...)', async () => {+ // beforeUnmount aborts the in-flight live-tail request. pollForNewLogs' finally+ // block used to run unconditionally after that abort, dispatching actions against+ // a logic that had already been torn down and crashing with a Kea "path not found"+ // error. Guarding the finally block on signal.aborted (mirroring the existing catch+ // block) fixes it.+ let resolveQuery: (() => void) | undefined+ useMocks({+ post: {+ '/api/environments/:team_id/logs/query/': () =>+ new Promise((resolve) => {+ resolveQuery = () => resolve([200, { results: [], maxExportableLogs: 5000 }])+ }),+ },+ })++ logic.actions.setLiveTailRunning(true)+ await expectLogic(logic).toDispatchActions(['setLiveTailAbortController'])++ expect(() => logic.unmount()).not.toThrow()++ // Let the aborted request's rejection propagate through pollForNewLogs' catch/finally.+ resolveQuery?.()+ await new Promise((resolve) => setTimeout(resolve, 0))+ })})describe('sparklineData selector', () => {diff --git a/products/logs/frontend/components/LogsViewer/data/logsViewerDataLogic.ts b/products/logs/frontend/components/LogsViewer/data/logsViewerDataLogic.tsindex d8a39b4718b0..461b98a34d7a 100644--- a/products/logs/frontend/components/LogsViewer/data/logsViewerDataLogic.ts+++ b/products/logs/frontend/components/LogsViewer/data/logsViewerDataLogic.ts@@ -994,7 +994,7 @@ export const logsViewerDataLogic = kea<logsViewerDataLogicType>([},})),- listeners(({ actions, values, cache }) => ({+ listeners(({ actions, values, cache, props }) => ({handleQueryChange: ({ filterType, extraProps }) => {if (values.hasRunQuery) {posthog.capture('logs filter changed', { filter_type: filterType, ...extraProps })@@ -1201,7 +1201,7 @@ export const logsViewerDataLogic = kea<logsViewerDataLogicType>([actions.setNewLogUuids([])}} catch (error) {- if (signal.aborted) {+ if (signal.aborted || !logsViewerDataLogic.isMounted(props.id)) {return}console.error('Live tail polling error:', error)@@ -1214,17 +1214,25 @@ export const logsViewerDataLogic = kea<logsViewerDataLogicType>([})actions.setLiveTailRunning(false)} finally {- actions.setLiveTailAbortController(null)- if (values.liveTailRunning) {- cache.disposables.add(() => {- const timerId = setTimeout(- () => {- actions.pollForNewLogs()- },- Math.max(duration, values.liveTailPollInterval)- )- return () => clearTimeout(timerId)- }, 'liveTailTimer')+ // beforeUnmount aborts the in-flight controller and marks liveTailRunning false,+ // but those are plain dispatches during teardown, not guaranteed to run their+ // listeners before the logic's keyed path is torn down. So an unmount that lands+ // while this request is in flight can resolve normally afterwards. Re-check both+ // signals before touching actions/values, or this can dispatch against an+ // already-unmounted keyed logic instance and throw "[KEA] Can not find path ...".+ if (!signal.aborted && logsViewerDataLogic.isMounted(props.id)) {+ actions.setLiveTailAbortController(null)+ if (values.liveTailRunning) {+ cache.disposables.add(() => {+ const timerId = setTimeout(+ () => {+ actions.pollForNewLogs()+ },+ Math.max(duration, values.liveTailPollInterval)+ )+ return () => clearTimeout(timerId)+ }, 'liveTailTimer')+ }}}},