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(experiments): stop exposures spinner hanging on stale cache (#65146)

Juraj Majerik10d938881676
diff --git a/frontend/src/queries/query.ts b/frontend/src/queries/query.ts
index 24a6a86969f8..146d46881f13 100644
--- a/frontend/src/queries/query.ts
+++ b/frontend/src/queries/query.ts
@@ -168,7 +168,13 @@ async function executeQuery<N extends DataNode>(
* This is important in shared contexts, where we cannot create arbitrary queries via POST – we can only GET.
*/
pollOnly = false,
- limitContext?: 'posthog_ai'
+ limitContext?: 'posthog_ai',
+ /**
+ * When the backend serves a cached result while kicking off a background recompute
+ * (stale-while-revalidate: `is_cached` is true *and* an incomplete `query_status` is
+ * attached), return the cached results immediately instead of blocking on the recompute.
+ */
+ acceptStaleCache = false
): Promise<NonNullable<N['response']>> {
if (!pollOnly) {
const refreshParam: RefreshType = refresh || 'blocking'
@@ -191,6 +197,12 @@ async function executeQuery<N extends DataNode>(
return response
}
+ if (acceptStaleCache && 'is_cached' in response && response.is_cached) {
+ // Cached results are already present alongside a background recompute, so use them
+ // now rather than discarding them to poll a job that may take a while (or be stuck).
+ return response
+ }
+
queryId = response.query_status.id
} else {
if (refresh !== 'async' && refresh !== 'force_async') {
@@ -215,7 +227,8 @@ export async function performQuery<N extends DataNode>(
filtersOverride?: DashboardFilter | null,
variablesOverride?: Record<string, HogQLVariable> | null,
pollOnly = false,
- limitContext?: 'posthog_ai'
+ limitContext?: 'posthog_ai',
+ acceptStaleCache = false
): Promise<NonNullable<N['response']>> {
let response: NonNullable<N['response']>
const logParams: Record<string, any> = {}
@@ -234,7 +247,8 @@ export async function performQuery<N extends DataNode>(
filtersOverride,
variablesOverride,
pollOnly,
- limitContext
+ limitContext,
+ acceptStaleCache
)
if (isHogQLQuery(queryNode) && response && typeof response === 'object') {
logParams.clickhouse_sql = (response as HogQLQueryResponse)?.clickhouse
diff --git a/frontend/src/scenes/experiments/experimentLogic.tsx b/frontend/src/scenes/experiments/experimentLogic.tsx
index 03fad3c2c41e..7e5ef0040efb 100644
--- a/frontend/src/scenes/experiments/experimentLogic.tsx
+++ b/frontend/src/scenes/experiments/experimentLogic.tsx
@@ -2631,7 +2631,20 @@ export const experimentLogic = kea<experimentLogicType>([
end_date: experiment.end_date,
holdout: experiment.holdout,
})
- return await performQuery(query, undefined, getExperimentRefreshMode(values.featureFlags, refresh))
+ // Show the cached exposures immediately when the backend is recomputing in the
+ // background, instead of hanging on the spinner until the recompute finishes.
+ return await performQuery(
+ query,
+ undefined,
+ getExperimentRefreshMode(values.featureFlags, refresh),
+ undefined, // queryId
+ undefined, // setPollResponse
+ undefined, // filtersOverride
+ undefined, // variablesOverride
+ false, // pollOnly
+ undefined, // limitContext
+ true // acceptStaleCache
+ )
},
},
],