A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: reset table loading state on failure and dispose jotai effect subscriptions (#11442)
Kiran Gadhave105ecbbc3da5
diff --git a/frontend/src/components/datasources/__tests__/table-list.test.tsx b/frontend/src/components/datasources/__tests__/table-list.test.tsxnew file mode 100644index 0000000000..3bb595eb3d--- /dev/null+++ b/frontend/src/components/datasources/__tests__/table-list.test.tsx@@ -0,0 +1,118 @@+/* Copyright 2026 Marimo. All rights reserved. */++import { render, screen, waitFor } from "@testing-library/react";+import { Provider } from "jotai";+import type React from "react";+import { describe, expect, it } from "vitest";+import { MockRequestClient } from "@/__mocks__/requests";+import type { SQLTableContext } from "@/core/datasets/data-source-connections";+import { PreviewSQLTableList } from "@/core/datasets/request-registry";+import type { SQLTableListPreview } from "@/core/kernel/messages";+import type { RequestId } from "@/core/network/DeferredRequestRegistry";+import { requestClientAtom } from "@/core/network/requests";+import { store } from "@/core/state/jotai";+import { TableList } from "../datasources";++const wrapper = ({ children }: { children: React.ReactNode }) => (+ <Provider store={store}>{children}</Provider>+);++const sqlTableContext: SQLTableContext = {+ engine: "my_engine",+ database: "my_db",+ schema: "public",+ dialect: "postgres",+ schemaPath: ["public"],+};++// The kernel answers table-list requests over the websocket, so the mock+// settles the deferred request instead of returning the response directly.+function renderDeferredTableList(settle?: (requestId: RequestId) => void) {+ const client = MockRequestClient.create();+ client.previewSQLTableList.mockImplementation(async ({ requestId }) => {+ settle?.(requestId);+ return null;+ });+ store.set(requestClientAtom, client);+ render(+ <TableList+ tables={[]}+ tablesResolved={false}+ sqlTableContext={sqlTableContext}+ />,+ { wrapper },+ );+ return client;+}++describe("TableList deferred discovery", () => {+ it("shows the error when the request rejects", async () => {+ renderDeferredTableList((requestId) =>+ PreviewSQLTableList.reject(requestId, new Error("engine offline")),+ );++ expect(screen.getByText("Loading tables...")).toBeInTheDocument();++ await waitFor(() => {+ expect(screen.getByText("engine offline")).toBeInTheDocument();+ });+ expect(screen.queryByText("Loading tables...")).toBeNull();+ });++ it("shows the error when the response has no tables field", async () => {+ renderDeferredTableList((requestId) =>+ PreviewSQLTableList.resolve(requestId, {} as SQLTableListPreview),+ );++ await waitFor(() => {+ expect(screen.getByText("No tables available")).toBeInTheDocument();+ });+ expect(screen.queryByText("Loading tables...")).toBeNull();+ });++ it("shows the empty state for an empty table list", async () => {+ renderDeferredTableList((requestId) =>+ PreviewSQLTableList.resolve(requestId, {+ tables: [],+ } as unknown as SQLTableListPreview),+ );++ await waitFor(() => {+ expect(screen.getByText("No tables found")).toBeInTheDocument();+ });+ expect(screen.queryByText("Loading tables...")).toBeNull();+ });++ it("requests the table list with the table context and waits for the kernel", async () => {+ let pendingRequestId: RequestId | undefined;+ const client = renderDeferredTableList((requestId) => {+ pendingRequestId = requestId;+ });++ await waitFor(() => {+ expect(client.previewSQLTableList).toHaveBeenCalledTimes(1);+ });+ expect(client.previewSQLTableList).toHaveBeenCalledWith(+ expect.objectContaining({+ engine: "my_engine",+ database: "my_db",+ schema: "public",+ schemaPath: ["public"],+ }),+ );+ expect(screen.getByText("Loading tables...")).toBeInTheDocument();++ // Settle the request so the process-wide registry does not keep the entry.+ expect(pendingRequestId).toBeDefined();+ PreviewSQLTableList.resolve(+ pendingRequestId as RequestId,+ {+ tables: [],+ } as unknown as SQLTableListPreview,+ );+ await waitFor(() => {+ expect(screen.queryByText("Loading tables...")).toBeNull();+ });+ expect(PreviewSQLTableList.requests.size).toBe(0);+ });+});diff --git a/frontend/src/components/datasources/datasources.tsx b/frontend/src/components/datasources/datasources.tsxindex 75ec16726c..dc6312a6e9 100644--- a/frontend/src/components/datasources/datasources.tsx+++ b/frontend/src/components/datasources/datasources.tsx@@ -702,7 +702,40 @@ const SchemaNode: React.FC<SchemaNodeProps> = (props) => {);};-const TableList: React.FC<{+// Kept outside the component because the React Compiler does not yet+// support try/finally inside component bodies.+async function loadTableList(+ sqlTableContext: SQLTableContext,+ addTableList: ReturnType<typeof useDataSourceActions>["addTableList"],+ setLoading: (loading: boolean) => void,+) {+ setLoading(true);+ try {+ const { engine, database, schema, schemaPath } = sqlTableContext;+ const previewTableList = await PreviewSQLTableList.request({+ engine: engine,+ database: database,+ schema: schema,+ schemaPath: schemaPath ?? [],+ });++ if (!previewTableList?.tables) {+ throw new Error("No tables available");+ }++ addTableList({+ tables: previewTableList.tables,+ sqlTableContext: sqlTableContext,+ });+ } finally {+ setLoading(false);+ }+}++/**+ * This is exported for testing purposes only.+ */+export const TableList: React.FC<{tables: DataTable[];sqlTableContext?: SQLTableContext;searchValue?: string;@@ -731,26 +764,7 @@ const TableList: React.FC<{// resets tablesResolved to false).const { isPending, error } = useAsyncData(async () => {if (!tablesResolved && sqlTableContext) {- setTablesLoading(true);-- const { engine, database, schema, schemaPath } = sqlTableContext;- const previewTableList = await PreviewSQLTableList.request({- engine: engine,- database: database,- schema: schema,- schemaPath: schemaPath ?? [],- });-- if (!previewTableList?.tables) {- setTablesLoading(false);- throw new Error("No tables available");- }-- addTableList({- tables: previewTableList.tables,- sqlTableContext: sqlTableContext,- });- setTablesLoading(false);+ await loadTableList(sqlTableContext, addTableList, setTablesLoading);}}, [tablesResolved, sqlTableContext]);diff --git a/frontend/src/core/state/__tests__/jotai.test.ts b/frontend/src/core/state/__tests__/jotai.test.tsdeleted file mode 100644index 2a925bcd76..0000000000--- a/frontend/src/core/state/__tests__/jotai.test.ts+++ /dev/null@@ -1,25 +0,0 @@-/* Copyright 2026 Marimo. All rights reserved. */--import { atom } from "jotai";-import { describe, expect, it } from "vitest";-import { store, waitFor } from "../jotai";--describe("waitFor function", () => {- it("should resolve when the atom satisfies the predicate", async () => {- const testAtom = atom(0);- store.set(testAtom, 10);-- const result = await waitFor(testAtom, (value) => value === 10);- expect(result).toBe(10);- });-- it("should resolve when the atom changes to satisfy the predicate", async () => {- const testAtom = atom(0);-- setTimeout(() => store.set(testAtom, 15), 5);- setTimeout(() => store.set(testAtom, 20), 10);-- const result = await waitFor(testAtom, (value) => value === 20);- expect(result).toBe(20);- });-});diff --git a/frontend/src/core/state/__tests__/jotai.test.tsx b/frontend/src/core/state/__tests__/jotai.test.tsxnew file mode 100644index 0000000000..90c81d700f--- /dev/null+++ b/frontend/src/core/state/__tests__/jotai.test.tsx@@ -0,0 +1,108 @@+/* Copyright 2026 Marimo. All rights reserved. */++import { act, renderHook } from "@testing-library/react";+import { atom, Provider } from "jotai";+import type React from "react";+import { describe, expect, it, vi } from "vitest";+import { store, useJotaiEffect, waitFor } from "../jotai";++const wrapper = ({ children }: { children: React.ReactNode }) => (+ <Provider store={store}>{children}</Provider>+);++describe("waitFor function", () => {+ it("should resolve when the atom satisfies the predicate", async () => {+ const testAtom = atom(0);+ store.set(testAtom, 10);++ const result = await waitFor(testAtom, (value) => value === 10);+ expect(result).toBe(10);+ });++ it("should resolve when the atom changes to satisfy the predicate", async () => {+ const testAtom = atom(0);++ setTimeout(() => store.set(testAtom, 15), 5);+ setTimeout(() => store.set(testAtom, 20), 10);++ const result = await waitFor(testAtom, (value) => value === 20);+ expect(result).toBe(20);+ });+});++describe("useJotaiEffect", () => {+ it("calls the effect with the new and previous values", () => {+ const testAtom = atom(0);+ const effect = vi.fn();+ renderHook(() => useJotaiEffect(testAtom, effect), { wrapper });++ act(() => store.set(testAtom, 1));+ act(() => store.set(testAtom, 2));++ expect(effect.mock.calls).toEqual([+ [1, 0],+ [2, 1],+ ]);+ });++ it("calls only the latest callback after a rerender", () => {+ const testAtom = atom(0);+ const first = vi.fn();+ const second = vi.fn();+ const { rerender } = renderHook(+ ({ effect }) => useJotaiEffect(testAtom, effect),+ { initialProps: { effect: first }, wrapper },+ );++ rerender({ effect: second });+ act(() => store.set(testAtom, 1));++ expect(first).not.toHaveBeenCalled();+ expect(second).toHaveBeenCalledTimes(1);+ });++ it("subscribes once per rerender with an inline callback", () => {+ const testAtom = atom(0);+ const calls: number[] = [];+ const { rerender } = renderHook(+ () => useJotaiEffect(testAtom, (value) => calls.push(value)),+ { wrapper },+ );++ rerender();+ rerender();+ act(() => store.set(testAtom, 1));++ expect(calls).toEqual([1]);+ });++ it("stops listening to the previous atom when the atom changes", () => {+ const firstAtom = atom(0);+ const secondAtom = atom(0);+ const effect = vi.fn();+ const { rerender } = renderHook(+ ({ target }) => useJotaiEffect(target, effect),+ { initialProps: { target: firstAtom }, wrapper },+ );++ rerender({ target: secondAtom });+ act(() => store.set(firstAtom, 1));+ expect(effect).not.toHaveBeenCalled();++ act(() => store.set(secondAtom, 1));+ expect(effect).toHaveBeenCalledTimes(1);+ });++ it("unsubscribes on unmount", () => {+ const testAtom = atom(0);+ const effect = vi.fn();+ const { unmount } = renderHook(() => useJotaiEffect(testAtom, effect), {+ wrapper,+ });++ unmount();+ act(() => store.set(testAtom, 1));++ expect(effect).not.toHaveBeenCalled();+ });+});diff --git a/frontend/src/core/state/jotai.ts b/frontend/src/core/state/jotai.tsindex 0eae3a1166..81a04594ed 100644--- a/frontend/src/core/state/jotai.ts+++ b/frontend/src/core/state/jotai.ts@@ -42,7 +42,7 @@ export function useJotaiEffect<T>(const store = useStore();useEffect(() => {let prevValue = store.get(atom);- store.sub(atom, () => {+ return store.sub(atom, () => {const value = store.get(atom);effect(value, prevValue);prevValue = value;