: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(tabs): reverse tab mount/unmount order to prevent logic deallocation (#57469)
Thomas Obermüllerfd4f40af7dd7
diff --git a/frontend/src/scenes/sceneLogic.tsx b/frontend/src/scenes/sceneLogic.tsxindex 18ed498bc5b9..a79b1fc3fe3f 100644--- a/frontend/src/scenes/sceneLogic.tsx+++ b/frontend/src/scenes/sceneLogic.tsx@@ -1135,30 +1135,45 @@ export const sceneLogic = kea<sceneLogicType>([}}- const unmount = cache.mountedTabLogic[tabId]- if (unmount) {- try {- unmount()- } catch (error) {- console.error('Error unmounting previous tab logic:', error)- }- delete cache.mountedTabLogic[tabId]- }+ // Mount the new scene logic *before* unmounting the previous one. When the new+ // mount uses the same logic key (tab switch back to the same scene with the same+ // params), kea's reference count goes 1 → 2 → 1 and reducer state is preserved.+ // Unmount-then-mount would briefly drop the count to 0 and destroy the instance.+ let newUnmount: (() => void) | undefined+ let newLogicErrored = falseif (exportedScene?.logic) {try {const builtLogicProps = { tabId, ...exportedScene?.paramsToProps?.(params) }const builtLogic = exportedScene?.logic(builtLogicProps)- cache.mountedTabLogic[tabId] = builtLogic.mount()+ newUnmount = builtLogic.mount()} catch (error) {// Scene logic builders (e.g. dashboardLogic.key()) can throw on malformed// route params like `/dashboard/abc`. Capture so regressions surface, then// route to Error404 so the user sees a proper 404 instead of a blank crash.posthog.captureException(error, { extra: { sceneId, sceneKey, tabId } })- actions.loadScene(Scene.Error404, undefined, tabId, emptySceneParams, 'REPLACE')- return+ newLogicErrored = true}}+ const previousUnmount = cache.mountedTabLogic[tabId]+ if (newUnmount) {+ cache.mountedTabLogic[tabId] = newUnmount+ } else {+ delete cache.mountedTabLogic[tabId]+ }+ if (previousUnmount) {+ try {+ previousUnmount()+ } catch (error) {+ console.error('Error unmounting previous tab logic:', error)+ }+ }++ if (newLogicErrored) {+ actions.loadScene(Scene.Error404, undefined, tabId, emptySceneParams, 'REPLACE')+ return+ }+const trackingKey = tabId || '__default__'const lastTracked = cache.lastTrackedSceneByTab?.[trackingKey]if (!lastTracked || lastTracked.sceneId !== sceneId || lastTracked.sceneKey !== sceneKey) {