From 0451d7e2a9265325ffb0cbab8a8200505b1e2ef9 Mon Sep 17 00:00:00 2001 From: Rishi Balakrishnan Date: Wed, 5 Aug 2026 12:07:07 -0400 Subject: [PATCH] fix(fallback): serialize controller evaluations and await feature init A stale in-flight evaluation could resolve after a newer one and overwrite its decision; evaluations could also read the flag's in-code default before GrowthBook finished loading. --- src/state/appview-fallback-controller.tsx | 25 ++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/state/appview-fallback-controller.tsx b/src/state/appview-fallback-controller.tsx index 2c5ec73c7..30a928642 100644 --- a/src/state/appview-fallback-controller.tsx +++ b/src/state/appview-fallback-controller.tsx @@ -16,7 +16,7 @@ import { INITIAL_DECIDER_STATE, } from '#/state/appview-status' import {useAgent, useSession} from '#/state/session' -import {Features, features} from '#/analytics/features' +import {Features, features, init as featuresInit} from '#/analytics/features' const RECHECK_INTERVAL = 3 * 60e3 @@ -31,16 +31,31 @@ export function AppviewFallbackController() { const queryClient = useQueryClient() const {hasSession} = useSession() const deciderRef = useRef(INITIAL_DECIDER_STATE) + const epochRef = useRef(0) const evaluate = useCallback(async () => { + // Evaluations await network (status poll, eligibility probe) and can fire + // concurrently from the interval, foregrounding, and outage transitions. + // Only the newest run may apply a result: a stale run resolving late would + // otherwise overwrite a fresher decision with an outdated one. + const epoch = ++epochRef.current + const apply = (next: boolean, trigger?: FallbackMode) => { + if (epoch !== epochRef.current) return + setFallbackActive(next, agent, queryClient, trigger) + } + + // The flag defaults to 'auto' until GrowthBook has loaded; acting on that + // default would override an operator-set force-* mode during launch. + await featuresInit + if (!hasSession) { - setFallbackActive(false, agent, queryClient) + apply(false) return } const mode = getMode() if (mode === 'force-primary') { - setFallbackActive(false, agent, queryClient) + apply(false) return } @@ -56,12 +71,12 @@ export function AppviewFallbackController() { } if (!wantsFallback) { - setFallbackActive(false, agent, queryClient) + apply(false) return } const eligibility = await probeFallbackEligibility(agent) - setFallbackActive(eligibility === 'eligible', agent, queryClient, mode) + apply(eligibility === 'eligible', mode) }, [agent, queryClient, hasSession]) useEffect(() => { -- knotmirror 0.0.1