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(max): tolerate mid-unmount scene logic in context collection (#70287)

posthog[bot]c2c7c5dbc135
diff --git a/frontend/src/scenes/max/maxContextLogic.test.ts b/frontend/src/scenes/max/maxContextLogic.test.ts
index ff4acaa67d2f..17c0b6665391 100644
--- a/frontend/src/scenes/max/maxContextLogic.test.ts
+++ b/frontend/src/scenes/max/maxContextLogic.test.ts
@@ -702,6 +702,7 @@ describe('maxContextLogic', () => {
// Simulate the dashboard scene being active, mirroring how dashboardLogic.maxContext
// builds its context from the (not yet fully loaded) dashboard value.
jest.spyOn(sceneLogic.selectors, 'activeSceneLogic').mockReturnValue({
+ isMounted: () => true,
selectors: {
maxContext: () => [createMaxContextHelpers.dashboard(dashboardWithoutTiles)],
},
@@ -729,6 +730,7 @@ describe('maxContextLogic', () => {
jest.spyOn(console, 'error').mockImplementation(() => undefined)
jest.spyOn(sceneLogic.selectors, 'activeSceneLogic').mockReturnValue({
+ isMounted: () => true,
selectors: {
maxContext: () => {
throw new Error('boom from scene maxContext')
@@ -748,5 +750,46 @@ describe('maxContextLogic', () => {
expect.objectContaining({ feature: 'max_scene_context' })
)
})
+
+ it('does not read (or report) context for a scene logic caught mid-unmount', async () => {
+ const captureException = jest.spyOn(posthog, 'captureException').mockImplementation(() => undefined as any)
+ const maxContext = jest.fn(() => [createMaxContextHelpers.dashboard(mockDashboard)])
+
+ jest.spyOn(sceneLogic.selectors, 'activeSceneLogic').mockReturnValue({
+ isMounted: () => false,
+ selectors: { maxContext },
+ } as any)
+ jest.spyOn(sceneLogic.selectors, 'activeLoadedScene').mockReturnValue({
+ paramsToProps: () => ({}),
+ sceneParams: {},
+ } as any)
+
+ await expectLogic(logic).toMatchValues({ sceneContext: [] })
+
+ expect(maxContext).not.toHaveBeenCalled()
+ expect(captureException).not.toHaveBeenCalled()
+ })
+
+ it('treats a "Can not find path" throw as a benign mid-unmount race, not an exception', async () => {
+ const captureException = jest.spyOn(posthog, 'captureException').mockImplementation(() => undefined as any)
+ jest.spyOn(console, 'error').mockImplementation(() => undefined)
+
+ jest.spyOn(sceneLogic.selectors, 'activeSceneLogic').mockReturnValue({
+ isMounted: () => true,
+ selectors: {
+ maxContext: () => {
+ throw new Error('[KEA] Can not find path scenes.dashboard.dashboardLogic in the store')
+ },
+ },
+ } as any)
+ jest.spyOn(sceneLogic.selectors, 'activeLoadedScene').mockReturnValue({
+ paramsToProps: () => ({}),
+ sceneParams: {},
+ } as any)
+
+ await expectLogic(logic).toMatchValues({ sceneContext: [] })
+
+ expect(captureException).not.toHaveBeenCalled()
+ })
})
})
diff --git a/frontend/src/scenes/max/maxContextLogic.ts b/frontend/src/scenes/max/maxContextLogic.ts
index 5fa3be331777..ad9de0832c59 100644
--- a/frontend/src/scenes/max/maxContextLogic.ts
+++ b/frontend/src/scenes/max/maxContextLogic.ts
@@ -525,7 +525,16 @@ export const maxContextLogic = kea<maxContextLogicType>([
(state): MaxContextInput[] => {
const activeSceneLogic = sceneLogic.selectors.activeSceneLogic(state, {})
- if (activeSceneLogic && 'maxContext' in activeSceneLogic.selectors) {
+ // During a scene transition, sceneLogic can still resolve the outgoing scene's
+ // (keyed) logic reference after kea has already unmounted it. Reading its
+ // maxContext then walks into a store path that no longer exists and throws
+ // "Can not find path". Skip the read while unmounted — the next recompute after
+ // the new scene mounts returns fresh context.
+ if (
+ activeSceneLogic &&
+ activeSceneLogic.isMounted() &&
+ 'maxContext' in activeSceneLogic.selectors
+ ) {
try {
const activeLoadedScene = sceneLogic.selectors.activeLoadedScene(state, {})
return activeSceneLogic.selectors.maxContext(
@@ -533,8 +542,12 @@ export const maxContextLogic = kea<maxContextLogicType>([
activeLoadedScene?.paramsToProps?.(activeLoadedScene?.sceneParams) || {}
)
} catch (error) {
- // Surface the failure instead of silently returning empty context.
- reportSceneContextError(error)
+ // A logic caught mid-unmount throws a benign "Can not find path" — treat
+ // that as a transient race and skip it quietly. Surface everything else
+ // so genuine context-build bugs don't get swallowed.
+ if (!(error instanceof Error && error.message.includes('Can not find path'))) {
+ reportSceneContextError(error)
+ }
}
}
return []
diff --git a/frontend/src/scenes/max/maxThreadLogic.test.ts b/frontend/src/scenes/max/maxThreadLogic.test.ts
index 0eee6612c29b..4e1ce2e6b0ce 100644
--- a/frontend/src/scenes/max/maxThreadLogic.test.ts
+++ b/frontend/src/scenes/max/maxThreadLogic.test.ts
@@ -476,6 +476,7 @@ describe('maxThreadLogic', () => {
// it ships with no dashboard context and Max can't see the open dashboard.
const mockLoadingDashboardScene = (): { values: { dashboard: any } } => {
const fakeDashboardLogic: any = {
+ isMounted: () => true,
selectors: {
maxContext: () =>
fakeDashboardLogic.values.dashboard