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(flags): avoid store access after unmount in applyTemplate (#67926)

posthog[bot]3a593f922910
diff --git a/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts b/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts
index 9f39fda6e17d..72b85385a42f 100644
--- a/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts
+++ b/frontend/src/scenes/feature-flags/featureFlagLogic.test.ts
@@ -22,7 +22,12 @@ import { FeatureFlagFilters } from '~/types'
import { TemplateKey } from 'products/feature_flags/frontend/featureFlagTemplateConstants'
-import { defaultReleaseConditionsLogic, resolveDefaultReleaseConditions } from './defaultReleaseConditionsLogic'
+import * as defaultReleaseConditionsModule from './defaultReleaseConditionsLogic'
+import {
+ DefaultReleaseConditionsResponse,
+ defaultReleaseConditionsLogic,
+ resolveDefaultReleaseConditions,
+} from './defaultReleaseConditionsLogic'
import { detectFeatureFlagChanges } from './featureFlagConfirmationLogic'
import {
NEW_FLAG,
@@ -397,6 +402,42 @@ describe('featureFlagLogic', () => {
}),
})
})
+
+ it('does not access the store after unmounting mid-apply', async () => {
+ // applyTemplate awaits release conditions before reading values again. Unmounting during
+ // that await (navigating away, or the auto-apply from a ?template= param racing a fast
+ // unmount) must not touch a store path that no longer exists — that used to throw
+ // "Can not find path ... in the store" and surface as an unhandled rejection.
+ let resolveRelease: (value: DefaultReleaseConditionsResponse | null) => void = () => {}
+ const pendingRelease = new Promise<DefaultReleaseConditionsResponse | null>((resolve) => {
+ resolveRelease = resolve
+ })
+ const spy = jest
+ .spyOn(defaultReleaseConditionsModule, 'resolveDefaultReleaseConditions')
+ .mockReturnValue(pendingRelease)
+
+ const rejections: unknown[] = []
+ const onRejection = (error: unknown): void => {
+ rejections.push(error)
+ }
+ process.on('unhandledRejection', onRejection)
+
+ try {
+ // Listener parks on the pending release conditions, then we navigate away.
+ logic.actions.applyTemplate('targeted')
+ logic.unmount()
+
+ // Resuming after unmount must bail instead of reading values.featureFlag.
+ resolveRelease(null)
+ await pendingRelease
+ await new Promise((resolve) => setTimeout(resolve, 0))
+ } finally {
+ process.off('unhandledRejection', onRejection)
+ spy.mockRestore()
+ }
+
+ expect(rejections).toEqual([])
+ })
})
describe('setRemoteConfigEnabled', () => {
diff --git a/frontend/src/scenes/feature-flags/featureFlagLogic.ts b/frontend/src/scenes/feature-flags/featureFlagLogic.ts
index db089391a190..db5b5efd36d6 100644
--- a/frontend/src/scenes/feature-flags/featureFlagLogic.ts
+++ b/frontend/src/scenes/feature-flags/featureFlagLogic.ts
@@ -2029,7 +2029,7 @@ export const featureFlagLogic = kea<featureFlagLogicType>([
maybeApplyUrlIntent(values, actions)
}
},
- applyTemplate: async ({ templateId }) => {
+ applyTemplate: async ({ templateId }, breakpoint) => {
const template = values.templates.find((t) => t.id === templateId)
if (!template || !values.featureFlag) {
return
@@ -2040,6 +2040,9 @@ export const featureFlagLogic = kea<featureFlagLogicType>([
values.defaultReleaseConditions,
values.currentTeam?.id
)
+ // Bail if the logic unmounted or applyTemplate was re-invoked while release conditions
+ // were loading — otherwise the reads below hit a store path that no longer exists and kea throws.
+ breakpoint()
const defaultGroups =
defaultConfig?.enabled && defaultConfig.default_groups?.length > 0 ? defaultConfig.default_groups : []
const templateGroups = templateValues.filters?.groups ?? []