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(notebooks): wait for the server before calling a cell run timed out (#88340)

Sinan Kucukkoseler0517969de1e3
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 42ce980841a1..9b84383c6491 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -5764,6 +5764,10 @@ snapshots:
hash: v1.k794b7964.4b8d5226ee154debdf333ff93d65a90ca766bcb25951e1baf36affb8b84b548d.dWZSHssPc83RkHVE-JPJA562bJR7MUim2yfqhjfNE50
scenes-app-customer-analytics-accounts--feature-gate-off--light:
hash: v1.k794b7964.e8b91bf6a0dd1b5d31064f4cbb94e9791e999c210831c455a498d0c7cc7e6bbb.IkZPJrJz33sKS8zzKwoy8V3s0GvmKpe5EGYTCIljhLs
+ scenes-app-customer-analytics-accounts--row-expanded-conversations--dark:
+ hash: v1.k794b7964.c83d3e0351279053117a4376654ece30522ea04e0d17a21d084e58be220395e9.coxsJkBmbXdsAZSUNvss_RtW7LQ6MVRxvqbiW61JfQI
+ scenes-app-customer-analytics-accounts--row-expanded-conversations--light:
+ hash: v1.k794b7964.9ffbe7d6153dbbd3812945e1f0e70e48bf941371a6d8b30027d0b0eab61f98e4.86uwFxkcB9kgOSyT445fxXt_x_ckpJp3GRukqn62BGY
scenes-app-customer-analytics-accounts--row-expanded-email-threads--dark:
hash: v1.k794b7964.0b52a6a4b48bc77bd2f0cf6d4550c4ec9d5262aaf9ac1a1aee9e2e57f311bbf1.moW1nKPfmZWFyn0i-N8hewBU7i250wMB4P8xCmMt9w8
scenes-app-customer-analytics-accounts--row-expanded-email-threads--light:
diff --git a/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.test.ts b/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.test.ts
index 2a69c3f7f6ca..7041b6a055b6 100644
--- a/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.test.ts
+++ b/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.test.ts
@@ -9,7 +9,7 @@ import { initKeaTests } from '~/test/init'
import { buildMarkdownNotebookContent, serializeMarkdownNotebookComponent } from '../Notebook/markdownNotebookV2'
import { notebookSettingsLogic } from '../Notebook/notebookSettingsLogic'
import { NotebookNodeType } from '../types'
-import { collectSqlV2Refs, notebookNodeSQLV2Logic } from './notebookNodeSQLV2Logic'
+import { collectSqlV2Refs, notebookNodeSQLV2Logic, pollIntervalMs } from './notebookNodeSQLV2Logic'
describe('notebookNodeSQLV2Logic', () => {
let logic: ReturnType<typeof notebookNodeSQLV2Logic.build>
@@ -138,6 +138,22 @@ describe('notebookNodeSQLV2Logic', () => {
})
})
+ describe('pollIntervalMs', () => {
+ // The steps are ordered slowest first and the lookup takes the first match, so
+ // reordering them silently returns one cadence for every wait. That changes how many
+ // requests a long-running cell makes by several times over, and nothing else catches it.
+ it.each([
+ [0, 1_000],
+ [29_999, 1_000],
+ [30_000, 2_000],
+ [119_999, 2_000],
+ [120_000, 5_000],
+ [20 * 60 * 1_000, 5_000],
+ ])('waits %ims into a run, so it polls every %ims', (waitedMs, expected) => {
+ expect(pollIntervalMs(waitedMs)).toEqual(expected)
+ })
+ })
+
describe('execution lanes', () => {
it('pages a direct run client-side from the rows the result poll returned', async () => {
// The server page endpoint refuses hogql runs; losing the local slice would
@@ -447,6 +463,35 @@ describe('notebookNodeSQLV2Logic', () => {
other.unmount()
})
+ it('gives up the poller at the wait budget without leaving a stray timer', async () => {
+ // Reaching the 21-minute client budget stops polling synchronously, which disposes the
+ // poll timer. The self-rescheduling callback must not arm a new one afterwards: an
+ // untracked timer would survive unmount and re-fire the failure every interval, aborting
+ // any run-all chain waiting on this cell until a reload.
+ jest.useFakeTimers()
+ try {
+ mount({ runId: 'r1', hasResult: false })
+ // Let the first (still-running) poll settle so its scheduled follow-up is what trips
+ // the budget next.
+ await jest.advanceTimersByTimeAsync(0)
+
+ // Jump the accumulated wait to the budget edge; the next scheduled poll trips it.
+ logic.cache.pollWaitedMs = 21 * 60 * 1000
+ await jest.advanceTimersByTimeAsync(1000)
+
+ expect(logic.values.runError).toContain('Stopped checking')
+ // The poller is disposed and, crucially, not re-armed. A stray timer would re-enter
+ // the budget branch every interval, which each time accumulates the wait again — so an
+ // unchanged wait after advancing past several intervals proves nothing rescheduled.
+ expect(logic.cache.disposables.registry.has('pollResult')).toBe(false)
+ const waitedAfterGivingUp = logic.cache.pollWaitedMs
+ await jest.advanceTimersByTimeAsync(15_000)
+ expect(logic.cache.pollWaitedMs).toBe(waitedAfterGivingUp)
+ } finally {
+ jest.useRealTimers()
+ }
+ })
+
it('unmounting a busy node releases the notebook', async () => {
mount()
logic.actions.runQuery('select 1')
diff --git a/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.ts b/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.ts
index c958432d1c0b..b010c3a77c76 100644
--- a/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.ts
+++ b/frontend/src/scenes/notebooks/Nodes/notebookNodeSQLV2Logic.ts
@@ -63,10 +63,25 @@ export function collectSqlV2Refs(doc: JSONContent | null | undefined, selfNodeId
return refs
}
-const POLL_INTERVAL_MS = 1000
-// Must outlast the backend's own run budgets (180s data-plane poll deadline, 300s kernel
-// execute timeout) plus slack, or a slow-but-successful run gets reported as timed out.
-const MAX_POLL_ATTEMPTS = 330 // ~5.5 minutes at 1s
+// How often the poller re-checks a run, by how long it already waited. A cell that
+// materializes a large frame can legitimately run for many minutes, and a flat one-second
+// cadence across that window costs hundreds of requests for a single cell, so the interval
+// widens once nobody is plausibly still watching the first result land.
+const POLL_INTERVAL_STEPS_MS = [
+ { afterMs: 120_000, intervalMs: 5_000 },
+ { afterMs: 30_000, intervalMs: 2_000 },
+ { afterMs: 0, intervalMs: 1_000 },
+]
+
+export function pollIntervalMs(waitedMs: number): number {
+ return POLL_INTERVAL_STEPS_MS.find(({ afterMs }) => waitedMs >= afterMs)?.intervalMs ?? 1_000
+}
+
+// Must outlast every backend run budget, so the client reports the run's real outcome rather
+// than inventing one. The backend expires a stalled run itself: the direct lane at 600s, the
+// kernel lane at 1200s. Under those, a run the backend goes on to finish still reads here as
+// a client timeout, and the cell renders as errored while the server is still working on it.
+const MAX_POLL_WAIT_MS = 21 * 60 * 1000
export const SQL_V2_DEFAULT_PAGE_SIZE = 50
@@ -580,21 +595,44 @@ export const notebookNodeSQLV2Logic = kea<notebookNodeSQLV2LogicType>([
// Idempotent re-register: also covers a remount resuming a persisted in-flight run.
actions.startOperation(runOperation)
cache.activeRunId = runId
- cache.pollAttempts = 0
+ cache.pollWaitedMs = 0
actions.pollResult(runId)
// Same key auto-disposes any previous poller; disposables clean up on unmount and pause on hidden tab.
cache.disposables.add(() => {
- const intervalId = window.setInterval(() => actions.pollResult(runId), POLL_INTERVAL_MS)
- return () => clearInterval(intervalId)
+ // Reschedules itself rather than using setInterval, because the cadence
+ // widens as the wait grows.
+ let timeoutId = 0
+ const scheduleNext = (): void => {
+ timeoutId = window.setTimeout(
+ () => {
+ actions.pollResult(runId)
+ // pollResult can stop the poller synchronously when it reaches the
+ // budget, which disposes this entry. Re-arm only while it still
+ // owns a live poller, or the new timer would outlive the disposable
+ // and loop the failure forever.
+ if (cache.disposables.registry.has('pollResult')) {
+ scheduleNext()
+ }
+ },
+ pollIntervalMs(cache.pollWaitedMs ?? 0)
+ )
+ }
+ scheduleNext()
+ return () => clearTimeout(timeoutId)
}, 'pollResult')
},
pollResult: async ({ runId }) => {
if (cache.pollInFlight) {
return
}
- cache.pollAttempts = (cache.pollAttempts ?? 0) + 1
- if (cache.pollAttempts > MAX_POLL_ATTEMPTS) {
- actions.setRunError('Timed out waiting for result')
+ // Accumulated from the intervals the poller actually used, not from the clock:
+ // it pauses while the tab is hidden, so a clock-based budget would burn down
+ // during a pause the user never spent waiting.
+ cache.pollWaitedMs = (cache.pollWaitedMs ?? 0) + pollIntervalMs(cache.pollWaitedMs ?? 0)
+ if (cache.pollWaitedMs > MAX_POLL_WAIT_MS) {
+ // Past every backend budget, so the server is unreachable rather than slow.
+ // The run keeps whatever outcome the server gave it; only this client gave up.
+ actions.setRunError('Stopped checking for a result. Reload the page to see if the run finished.')
actions.stopPolling()
actions.nodeRunFinished(props.nodeId, 'failed', null)
return