Skip to content

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.

fix: re-initialize LSP on reconnect (#6967)

Myles Scolnick84a992be029e
diff --git a/frontend/src/components/charts/tooltip.ts b/frontend/src/components/charts/tooltip.ts
index ca1ea6621a..b13c157f44 100644
--- a/frontend/src/components/charts/tooltip.ts
+++ b/frontend/src/components/charts/tooltip.ts
@@ -1,3 +1,4 @@
+/* Copyright 2024 Marimo. All rights reserved. */
import { Handler } from "vega-tooltip";
// Create a tooltip handler that supports HTML content (including images)
diff --git a/frontend/src/core/codemirror/language/languages/python.ts b/frontend/src/core/codemirror/language/languages/python.ts
index 1d74cf7750..422993d9b3 100644
--- a/frontend/src/core/codemirror/language/languages/python.ts
+++ b/frontend/src/core/codemirror/language/languages/python.ts
@@ -44,8 +44,15 @@ import {
import type { LanguageAdapter } from "../types";
const pylspClient = once((lspConfig: LSPConfig) => {
+ // Create a mutable reference for the resync callback
+ let resyncCallback: (() => Promise<void>) | undefined;
+
+ const transport = createTransport("pylsp", async () => {
+ await resyncCallback?.();
+ });
+
const lspClientOpts = {
- transport: createTransport("pylsp"),
+ transport,
rootUri: getLSPDocumentRootUri(),
workspaceFolders: [],
};
@@ -126,47 +133,74 @@ const pylspClient = once((lspConfig: LSPConfig) => {
// We wrap the client in a NotebookLanguageServerClient to add some
// additional functionality to handle multiple cells
- return new NotebookLanguageServerClient(
+ const notebookClient = new NotebookLanguageServerClient(
new LanguageServerClient({
...lspClientOpts,
}),
settings,
);
+
+ // Set the resync callback now that the client exists
+ resyncCallback = () => notebookClient.resyncAllDocuments();
+
+ return notebookClient;
});
const tyLspClient = once((_: LSPConfig) => {
+ let resyncCallback: (() => Promise<void>) | undefined;
+
+ const transport = createTransport("ty", async () => {
+ await resyncCallback?.();
+ });
+
const lspClientOpts = {
- transport: createTransport("ty"),
+ transport,
rootUri: getLSPDocumentRootUri(),
workspaceFolders: [],
};
// We wrap the client in a NotebookLanguageServerClient to add some
// additional functionality to handle multiple cells
- return new NotebookLanguageServerClient(
+ const notebookClient = new NotebookLanguageServerClient(
new LanguageServerClient({
...lspClientOpts,
getWorkspaceConfiguration: (_) => [{ disableLanguageServices: true }],
}),
{},
);
+
+ // Set the resync callback now that the client exists
+ resyncCallback = () => notebookClient.resyncAllDocuments();
+
+ return notebookClient;
});
const pyrightClient = once((_: LSPConfig) => {
+ let resyncCallback: (() => Promise<void>) | undefined;
+
+ const transport = createTransport("basedpyright", async () => {
+ await resyncCallback?.();
+ });
+
const lspClientOpts = {
- transport: createTransport("basedpyright"),
+ transport,
rootUri: getLSPDocumentRootUri(),
workspaceFolders: [],
};
// We wrap the client in a NotebookLanguageServerClient to add some
// additional functionality to handle multiple cells
- return new NotebookLanguageServerClient(
+ const notebookClient = new NotebookLanguageServerClient(
new LanguageServerClient({
...lspClientOpts,
}),
{},
);
+
+ // Set the resync callback now that the client exists
+ resyncCallback = () => notebookClient.resyncAllDocuments();
+
+ return notebookClient;
});
/**
diff --git a/frontend/src/core/codemirror/lsp/notebook-lsp.ts b/frontend/src/core/codemirror/lsp/notebook-lsp.ts
index a24a1fcae9..4b7596768b 100644
--- a/frontend/src/core/codemirror/lsp/notebook-lsp.ts
+++ b/frontend/src/core/codemirror/lsp/notebook-lsp.ts
@@ -99,7 +99,7 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
CellId,
EditorView | null | undefined
>;
-
+ private readonly initialSettings: Record<string, unknown>;
private static readonly SEEN_CELL_DOCUMENT_URIS = new Set<CellDocumentUri>();
/**
@@ -120,7 +120,7 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
) {
this.documentUri = getLSPDocument();
this.getNotebookEditors = getNotebookEditors;
-
+ this.initialSettings = initialSettings;
this.client = client;
this.patchProcessNotification();
@@ -185,6 +185,37 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
this.client.close();
}
+ /**
+ * Re-synchronize all open documents with the LSP server.
+ * This is called after a WebSocket reconnection to restore document state.
+ */
+ public async resyncAllDocuments(): Promise<void> {
+ invariant(
+ isClientWithNotify(this.client),
+ "notify is not a method on the client",
+ );
+ await this.client.initialize();
+ this.client.notify("workspace/didChangeConfiguration", {
+ settings: this.initialSettings,
+ });
+
+ // Get the current document state
+ const { lens, version } = this.snapshotter.snapshot();
+
+ // Re-open the merged document with the LSP server
+ // This sends a textDocument/didOpen for the entire notebook
+ await this.client.textDocumentDidOpen({
+ textDocument: {
+ languageId: "python", // Default to Python for marimo notebooks
+ text: lens.mergedText,
+ uri: this.documentUri,
+ version: version,
+ },
+ });
+
+ Logger.log("[lsp] Document re-synchronization complete");
+ }
+
private getNotebookCode() {
return store.get(topologicalCodesAtom);
}
diff --git a/frontend/src/core/codemirror/lsp/transports.ts b/frontend/src/core/codemirror/lsp/transports.ts
index 65ec42ce9b..91a3a16dbf 100644
--- a/frontend/src/core/codemirror/lsp/transports.ts
+++ b/frontend/src/core/codemirror/lsp/transports.ts
@@ -10,10 +10,12 @@ import { getRuntimeManager } from "../../runtime/config";
* before connecting to the LSP server.
*
* @param serverName - The name of the LSP server.
+ * @param onReconnect - Optional callback to call after reconnection (e.g., to resync documents).
* @returns The transport.
*/
export function createTransport(
serverName: "pylsp" | "basedpyright" | "copilot" | "ty",
+ onReconnect?: () => Promise<void>,
) {
const runtimeManager = getRuntimeManager();
return new ReconnectingWebSocketTransport({
@@ -21,5 +23,6 @@ export function createTransport(
waitForConnection: async () => {
await waitForConnectionOpen();
},
+ onReconnect,
});
}
diff --git a/frontend/src/core/lsp/__tests__/transport.test.ts b/frontend/src/core/lsp/__tests__/transport.test.ts
index a59c286154..5dda138dda 100644
--- a/frontend/src/core/lsp/__tests__/transport.test.ts
+++ b/frontend/src/core/lsp/__tests__/transport.test.ts
@@ -230,4 +230,61 @@ describe("ReconnectingWebSocketTransport", () => {
expect(WebSocketTransport).toHaveBeenCalledTimes(1);
});
+
+ it("should call onReconnect callback after reconnection", async () => {
+ const getWsUrl = vi.fn(() => mockWsUrl);
+ const onReconnect = vi.fn().mockResolvedValue(undefined);
+ const transport = new ReconnectingWebSocketTransport({
+ getWsUrl,
+ onReconnect,
+ });
+
+ // First connection - callback should not be called
+ await transport.connect();
+ expect(onReconnect).not.toHaveBeenCalled();
+
+ // Simulate connection loss
+ mockConnection.readyState = WebSocket.CLOSED;
+
+ // Reconnect - callback should be called this time
+ const data: any = { method: "test", params: [] };
+ await transport.sendData(data, 5000);
+
+ expect(onReconnect).toHaveBeenCalledTimes(1);
+ });
+
+ it("should not call onReconnect callback on first connection", async () => {
+ const getWsUrl = vi.fn(() => mockWsUrl);
+ const onReconnect = vi.fn().mockResolvedValue(undefined);
+ const transport = new ReconnectingWebSocketTransport({
+ getWsUrl,
+ onReconnect,
+ });
+
+ await transport.connect();
+
+ expect(onReconnect).not.toHaveBeenCalled();
+ });
+
+ it("should handle onReconnect callback errors gracefully", async () => {
+ const getWsUrl = vi.fn(() => mockWsUrl);
+ const reconnectError = new Error("Reconnect callback failed");
+ const onReconnect = vi.fn().mockRejectedValue(reconnectError);
+ const transport = new ReconnectingWebSocketTransport({
+ getWsUrl,
+ onReconnect,
+ });
+
+ // First connection
+ await transport.connect();
+
+ // Simulate connection loss
+ mockConnection.readyState = WebSocket.CLOSED;
+
+ // Reconnect - should propagate the error from onReconnect
+ const data: any = { method: "test", params: [] };
+ await expect(transport.sendData(data, 5000)).rejects.toThrow(
+ "Reconnect callback failed",
+ );
+ });
});
diff --git a/frontend/src/core/lsp/transport.ts b/frontend/src/core/lsp/transport.ts
index d5d6a543f6..7be9fddd9c 100644
--- a/frontend/src/core/lsp/transport.ts
+++ b/frontend/src/core/lsp/transport.ts
@@ -15,6 +15,12 @@ export interface ReconnectingWebSocketTransportOptions {
* This is useful for ensuring dependencies (like the runtime) are ready.
*/
waitForConnection?: () => Promise<void>;
+
+ /**
+ * Optional callback that is called after a successful reconnection.
+ * This allows the LSP client to re-synchronize state (e.g., re-send document open notifications).
+ */
+ onReconnect?: () => Promise<void>;
}
/**
@@ -26,6 +32,7 @@ export class ReconnectingWebSocketTransport extends Transport {
private readonly options: ReconnectingWebSocketTransportOptions;
private connectionPromise: Promise<void> | undefined;
private isClosed = false;
+ private hasConnectedBefore = false;
constructor(options: ReconnectingWebSocketTransportOptions) {
super();
@@ -98,6 +105,15 @@ export class ReconnectingWebSocketTransport extends Transport {
// Connect the delegate
await delegate.connect();
Logger.log("WebSocket transport connected successfully");
+
+ // If this is a reconnection, call the onReconnect callback
+ const isReconnection = this.hasConnectedBefore;
+ this.hasConnectedBefore = true;
+
+ if (isReconnection && this.options.onReconnect) {
+ Logger.log("Calling onReconnect callback to re-synchronize state");
+ await this.options.onReconnect();
+ }
} catch (error) {
Logger.error("WebSocket transport connection failed", error);
// Clear the delegate on failure so we create a new one on retry
diff --git a/frontend/src/utils/arrays.ts b/frontend/src/utils/arrays.ts
index ecc1be1e26..ff19972117 100644
--- a/frontend/src/utils/arrays.ts
+++ b/frontend/src/utils/arrays.ts
@@ -100,7 +100,7 @@ export function uniqueByTakeLast<T>(arr: T[], key: (item: T) => string): T[] {
const k = key(item);
map.set(k, item);
}
- return Array.from(map.values());
+ return [...map.values()];
}
/**