: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(notebooks): stop the kernel poll on canvas notebooks (#88325)
Sinan Kucukkoseler2d6139c5103c
diff --git a/frontend/src/scenes/notebooks/Notebook/notebookKernelInfoLogic.test.ts b/frontend/src/scenes/notebooks/Notebook/notebookKernelInfoLogic.test.tsnew file mode 100644index 000000000000..89c4b0ce280a--- /dev/null+++ b/frontend/src/scenes/notebooks/Notebook/notebookKernelInfoLogic.test.ts@@ -0,0 +1,52 @@+import api from 'lib/api'++import { initKeaTests } from '~/test/init'++import { notebookKernelInfoLogic } from './notebookKernelInfoLogic'+import type { NotebookLogicMode } from './notebookLogic'++describe('notebookKernelInfoLogic', () => {+ let kernelStatusSpy: jest.SpyInstance+ let logic: ReturnType<typeof notebookKernelInfoLogic.build> | undefined++ beforeEach(() => {+ initKeaTests()+ kernelStatusSpy = jest+ .spyOn(api.notebooks, 'kernelStatus')+ .mockResolvedValue({ backend: null, status: 'stopped' })+ jest.useFakeTimers()+ })++ afterEach(() => {+ logic?.unmount()+ logic = undefined+ jest.useRealTimers()+ kernelStatusSpy.mockRestore()+ })++ test.each([+ { mode: 'notebook' as NotebookLogicMode, shortId: 'abc123', shouldPoll: true },+ { mode: undefined, shortId: 'def456', shouldPoll: true },+ { mode: 'canvas' as NotebookLogicMode, shortId: 'canvas-01890abc', shouldPoll: false },+ ])('mode $mode polls kernel status: $shouldPoll', async ({ mode, shortId, shouldPoll }) => {+ logic = notebookKernelInfoLogic({ shortId, mode })+ logic.mount()++ // afterMount fetches once on its own, so pin that count before any timer runs. Asserting+ // only that the spy was called would pass just as well with the refresh loop deleted.+ expect(kernelStatusSpy).toHaveBeenCalledTimes(shouldPoll ? 1 : 0)++ // A canvas has no server row, so a poll would 404 every 10 seconds until the page closes.+ // Advance asynchronously: each tick skips its fetch while the previous one is still in+ // flight, so the loop only advances if the mocked request settles between ticks.+ await jest.advanceTimersByTimeAsync(30_000)++ if (shouldPoll) {+ // Strictly more than the mount fetch, which is what shows the loop re-arms.+ expect(kernelStatusSpy.mock.calls.length).toBeGreaterThan(1)+ expect(kernelStatusSpy).toHaveBeenCalledWith(shortId)+ } else {+ expect(kernelStatusSpy).not.toHaveBeenCalled()+ }+ })+})diff --git a/frontend/src/scenes/notebooks/Notebook/notebookKernelInfoLogic.ts b/frontend/src/scenes/notebooks/Notebook/notebookKernelInfoLogic.tsindex 4c63aed88ab1..2eb24c197442 100644--- a/frontend/src/scenes/notebooks/Notebook/notebookKernelInfoLogic.ts+++ b/frontend/src/scenes/notebooks/Notebook/notebookKernelInfoLogic.ts@@ -16,6 +16,7 @@ import { loaders } from 'kea-loaders'import api from 'lib/api'import { PythonKernelExecuteResponse } from '../Nodes/pythonExecution'+import type { NotebookLogicMode } from './notebookLogic'/** A DuckDB object a SQL node can currently SELECT from, as reported by the kernel's last run. */export type NotebookKernelFrame = {@@ -49,6 +50,13 @@ export type NotebookKernelInfo = {export type NotebookKernelInfoLogicProps = {shortId: string+ /**+ * Anything other than `'notebook'` disables the kernel poll. A canvas notebook is never+ * persisted, so `/kernel/status/` always 404s for its client-made short ID, and a canvas+ * holds no Python or SQL node that could use a kernel. Person and group profile pages+ * render one, so polling them costs a 404 every 10 seconds for as long as the page is open.+ */+ mode?: NotebookLogicMode}export const cpuCoreOptions = [0.125, 0.25, 0.5, 1, 2, 4, 6, 8, 16, 32, 64]@@ -546,7 +554,10 @@ export const notebookKernelInfoLogic = kea<notebookKernelInfoLogicType>([actions.executeKernelSuccess(null)},})),- afterMount(({ actions, cache, values }) => {+ afterMount(({ actions, cache, props, values }) => {+ if (props.mode && props.mode !== 'notebook') {+ return+ }const scheduleRefresh = (): void => {const delayMs = values.isStarting ? 2000 : 10000cache.kernelInfoRefresh = window.setTimeout(() => {diff --git a/frontend/src/scenes/notebooks/Notebook/notebookLogic.ts b/frontend/src/scenes/notebooks/Notebook/notebookLogic.tsindex e4ae1cab3dff..8d9e312ae262 100644--- a/frontend/src/scenes/notebooks/Notebook/notebookLogic.ts+++ b/frontend/src/scenes/notebooks/Notebook/notebookLogic.ts@@ -703,7 +703,7 @@ export const notebookLogic = kea<notebookLogicType>([item_id: props.shortId,}),['comments', 'selectedCommentId'],- notebookKernelInfoLogic({ shortId: props.shortId }),+ notebookKernelInfoLogic({ shortId: props.shortId, mode: props.mode }),['kernelInfo'],notebookSettingsLogic,['showKernelInfo'],