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 Ty LSP startup and stale diagnostics in notebook editor (#8390)

axsseleeaffe6d28dc
diff --git a/frontend/src/components/app-config/user-config-form.tsx b/frontend/src/components/app-config/user-config-form.tsx
index b93bfd3cb0..1aad280e31 100644
--- a/frontend/src/components/app-config/user-config-form.tsx
+++ b/frontend/src/components/app-config/user-config-form.tsx
@@ -2,6 +2,7 @@
import { zodResolver } from "@hookform/resolvers/zod";
import { atom, useAtom, useAtomValue, useSetAtom } from "jotai";
+import { merge } from "lodash-es";
import {
AlertTriangleIcon,
BrainIcon,
@@ -287,10 +288,10 @@ export const UserConfigForm: React.FC = () => {
dirtyValues.ai = setAiModels(values.ai, dirtyValues.ai);
}
- await saveUserConfig({ config: dirtyValues }).then(() => {
- // Update local state with form values
- setConfig((prev) => ({ ...prev, ...values }));
- });
+ await saveUserConfig({ config: dirtyValues });
+ // Only apply the changed keys; this avoids stale request responses
+ // overwriting newer config changes.
+ setConfig((prev) => merge({}, prev, dirtyValues));
};
const onSubmit = useDebouncedCallback(onSubmitNotDebounced, FORM_DEBOUNCE);
diff --git a/frontend/src/core/codemirror/lsp/notebook-lsp.ts b/frontend/src/core/codemirror/lsp/notebook-lsp.ts
index bb5a536c60..6a3de1949e 100644
--- a/frontend/src/core/codemirror/lsp/notebook-lsp.ts
+++ b/frontend/src/core/codemirror/lsp/notebook-lsp.ts
@@ -178,6 +178,8 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
string,
Promise<LSP.CompletionItem>
>(10);
+ private latestDiagnosticsVersion: number | null = null;
+ private forwardedDiagnosticsVersion = 0;
constructor(
client: ILanguageServerClient,
@@ -270,6 +272,8 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
// Get the current document state
const { lens, version } = this.snapshotter.snapshot();
+ this.latestDiagnosticsVersion = null;
+ this.forwardedDiagnosticsVersion = 0;
// Re-open the merged document with the LSP server
// This sends a textDocument/didOpen for the entire notebook
@@ -768,13 +772,34 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
| { method: "other"; params: unknown },
) => {
if (notification.method === "textDocument/publishDiagnostics") {
+ const incomingVersion = notification.params.version;
+ if (incomingVersion != null) {
+ const latestVersion = this.latestDiagnosticsVersion;
+ if (
+ latestVersion !== null &&
+ Number.isFinite(incomingVersion) &&
+ incomingVersion < latestVersion
+ ) {
+ Logger.debug(
+ "[lsp] dropping stale diagnostics notification",
+ notification,
+ );
+ return;
+ }
+ this.latestDiagnosticsVersion = incomingVersion;
+ }
+
Logger.debug("[lsp] handling diagnostics", notification);
// Use the correct lens by version
const payload = this.snapshotter.getLatestSnapshot();
const diagnostics = notification.params.diagnostics;
- const { lens, version: cellVersion } = payload;
+ const { lens } = payload;
+ // Forward diagnostics with a strictly increasing version so downstream
+ // plugin updates/clears reliably, even when server repeats the same
+ // document version across multiple publishDiagnostics notifications.
+ const diagnosticsVersion = ++this.forwardedDiagnosticsVersion;
// Pre-partition diagnostics by cell
const diagnosticsByCellId = new Map<CellId, LSP.Diagnostic[]>();
@@ -817,7 +842,7 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
params: {
...notification.params,
uri: cellDocumentUri,
- version: cellVersion,
+ version: diagnosticsVersion,
diagnostics: cellDiagnostics,
},
});
@@ -832,6 +857,7 @@ export class NotebookLanguageServerClient implements ILanguageServerClient {
method: "textDocument/publishDiagnostics",
params: {
uri: cellDocumentUri,
+ version: diagnosticsVersion,
diagnostics: [],
},
});
diff --git a/marimo/_server/api/endpoints/config.py b/marimo/_server/api/endpoints/config.py
index ec846d1d25..ee5b2ba416 100644
--- a/marimo/_server/api/endpoints/config.py
+++ b/marimo/_server/api/endpoints/config.py
@@ -17,6 +17,7 @@ from marimo._runtime.packages.utils import is_python_isolated
from marimo._server.ai.mcp.config import is_mcp_config_empty
from marimo._server.api.deps import AppState
from marimo._server.api.utils import parse_request
+from marimo._server.lsp import any_lsp_server_running
from marimo._server.models.models import (
SaveUserConfigurationRequest,
SuccessResponse,
@@ -75,8 +76,8 @@ async def save_user_config(
async def handle_background_tasks() -> None:
# Update the server's view of the config
- if config["completion"]["copilot"]:
- LOGGER.debug("Starting copilot server")
+ if any_lsp_server_running(config):
+ LOGGER.debug("Starting language servers")
await app_state.session_manager.start_lsp_server()
# Reconfigure MCP servers if config changed
diff --git a/marimo/_server/api/middleware.py b/marimo/_server/api/middleware.py
index a5c5e9f9b8..dbe2158bca 100644
--- a/marimo/_server/api/middleware.py
+++ b/marimo/_server/api/middleware.py
@@ -391,6 +391,25 @@ class ProxyMiddleware:
return self.target_url
+ def _is_lsp_path(self, scope: Scope) -> bool:
+ return "/lsp/" in scope.get("path", "")
+
+ async def _try_start_lsp_server(self, scope: Scope) -> bool:
+ if not self._is_lsp_path(scope):
+ return False
+
+ app = scope.get("app")
+ if app is None:
+ return False
+
+ try:
+ app_state = AppStateBase(app.state)
+ await app_state.session_manager.start_lsp_server()
+ return True
+ except Exception as e:
+ LOGGER.warning("Failed to start LSP server from proxy: %s", e)
+ return False
+
async def __call__(
self, scope: Scope, receive: Receive, send: Send
) -> None:
@@ -467,6 +486,25 @@ class ProxyMiddleware:
self, scope: Scope, receive: Receive, send: Send, ws_url: str
) -> None:
websocket = WebSocket(scope, receive=receive, send=send)
+
+ async def safe_close(
+ *,
+ code: int = WebSocketCodes.UNEXPECTED_ERROR,
+ reason: str | None = None,
+ ) -> None:
+ if websocket.client_state == WebSocketState.DISCONNECTED:
+ return
+ try:
+ await websocket.close(code=code, reason=reason)
+ except RuntimeError as e:
+ # Starlette may raise if close was already sent.
+ if (
+ 'Cannot call "send" once a close message has been sent.'
+ in str(e)
+ ):
+ return
+ raise
+
try:
original_params = websocket.query_params
if original_params:
@@ -474,11 +512,11 @@ class ProxyMiddleware:
await websocket.accept()
# Try to connect to the upstream WebSocket with retries
- max_retries = 3
+ max_retries = 5
exponential_backoff = 1.5
async def get_client() -> ClientConnection:
- retry_delay = 0.5 # seconds
+ retry_delay = 0.25 # seconds
for attempt in range(max_retries):
try:
@@ -489,6 +527,17 @@ class ProxyMiddleware:
LOGGER.info(
f"WebSocket connection attempt {attempt + 1}/{max_retries} failed for {ws_url}: {e}"
)
+
+ # If this is an LSP proxy path, attempt to bring up the
+ # LSP server on connection failures. This is retried in
+ # case startup races with user-config persistence.
+ if self._is_lsp_path(scope):
+ started = await self._try_start_lsp_server(scope)
+ if started:
+ await asyncio.sleep(retry_delay)
+ if attempt < max_retries - 1:
+ continue
+
if attempt < max_retries - 1:
await asyncio.sleep(retry_delay)
retry_delay *= exponential_backoff
@@ -496,15 +545,6 @@ class ProxyMiddleware:
LOGGER.error(
f"Failed to connect to {ws_url} after {max_retries} attempts. Final error: {e}"
)
- # Close the client WebSocket with a meaningful error
- if (
- websocket.client_state
- != WebSocketState.DISCONNECTED
- ):
- await websocket.close(
- code=WebSocketCodes.UNEXPECTED_ERROR,
- reason="Failed to connect to LSP server",
- )
raise e
raise ValueError("Failed to connect to LSP server")
@@ -568,8 +608,7 @@ class ProxyMiddleware:
for task in relay_tasks:
if not task.done():
task.cancel()
- if websocket.client_state != WebSocketState.DISCONNECTED:
- await websocket.close()
+ await safe_close()
await ws_client.close()
except Exception as e:
LOGGER.error(f"WebSocket proxy error for {ws_url}: {e}")
@@ -580,8 +619,7 @@ class ProxyMiddleware:
LOGGER.error(
f"LSP server appears to be down at {ws_url}. Check if the LSP server started successfully."
)
- if websocket.client_state != WebSocketState.DISCONNECTED:
- await websocket.close(code=WebSocketCodes.UNEXPECTED_ERROR)
+ await safe_close(code=WebSocketCodes.UNEXPECTED_ERROR)
raise
diff --git a/marimo/_server/lsp.py b/marimo/_server/lsp.py
index 3807194590..aebc1cff68 100644
--- a/marimo/_server/lsp.py
+++ b/marimo/_server/lsp.py
@@ -120,10 +120,8 @@ class BaseLspServer(LspServer):
and self.process.returncode != 0
):
# Process failed immediately - read stderr to diagnose
- stderr_output = (
- self.process.stderr.read()
- if self.process.stderr
- else "No error output available"
+ stderr_output = await self._read_stderr_output(
+ fallback="No error output available"
)
LOGGER.error(
f"{self.id} LSP server failed to start with return code {self.process.returncode}"
@@ -140,6 +138,34 @@ class BaseLspServer(LspServer):
LOGGER.info(f"Started LSP {self.id} at port {self.port}")
self._started_at = time.time()
+ # Wait briefly for the server port to accept connections.
+ # This avoids races where the websocket proxy connects right
+ # after startup but before the process is listening.
+ is_ready = await self._wait_until_ready()
+ if not is_ready:
+ if not self.is_running():
+ stderr_output = await self._read_stderr_output(
+ fallback="No error output available"
+ )
+ LOGGER.error(
+ f"{self.id} LSP server exited before becoming ready."
+ )
+ LOGGER.error(f"Command: {' '.join(cmd)}")
+ LOGGER.error(f"Error output: {stderr_output}")
+ return AlertNotification(
+ title=f"{self.id} LSP server failed to start",
+ description=(
+ f"The {self.id} server exited before it was ready. "
+ f"Check {self.log_file} for details. "
+ f"Error: {stderr_output[:200]}"
+ ),
+ variant="danger",
+ )
+ LOGGER.warning(
+ "%s LSP server did not become ready within startup timeout.",
+ self.id,
+ )
+
# Start health monitoring in background
if (
self._health_check_task is None
@@ -158,6 +184,28 @@ class BaseLspServer(LspServer):
return None
+ async def _read_stderr_output(self, fallback: str = "") -> str:
+ process = self.process
+ if process is None or process.stderr is None:
+ return fallback
+ try:
+ return await asyncio.to_thread(process.stderr.read)
+ except Exception:
+ return fallback
+
+ async def _wait_until_ready(
+ self, timeout_seconds: float = 5.0, poll_seconds: float = 0.1
+ ) -> bool:
+ start_time = time.monotonic()
+ while (time.monotonic() - start_time) < timeout_seconds:
+ if not self.is_running():
+ return False
+ is_responsive, _ = await self.ping(timeout_ms=500)
+ if is_responsive:
+ return True
+ await asyncio.sleep(poll_seconds)
+ return False
+
def is_running(self) -> bool:
if self.process is None:
return False
@@ -297,14 +345,9 @@ class BaseLspServer(LspServer):
# Process has exited - check if it was expected
if self.process and self.process.returncode is not None:
- stderr_output = ""
- if self.process.stderr:
- try:
- # Try to read any remaining stderr
- # Note: stderr might be empty if already consumed during wait()
- stderr_output = self.process.stderr.read()
- except Exception:
- pass
+ # Try to read any remaining stderr.
+ # Note: stderr might be empty if already consumed during wait().
+ stderr_output = await self._read_stderr_output()
LOGGER.error(
f"{self.id} LSP server crashed unexpectedly with exit code {self.process.returncode}. Check {self.log_file} for details."
diff --git a/tests/_server/api/endpoints/test_config_endpoints.py b/tests/_server/api/endpoints/test_config_endpoints.py
index 69a53ea0a9..1e37071c1e 100644
--- a/tests/_server/api/endpoints/test_config_endpoints.py
+++ b/tests/_server/api/endpoints/test_config_endpoints.py
@@ -3,10 +3,13 @@ from __future__ import annotations
from typing import TYPE_CHECKING
-from tests._server.conftest import get_user_config_manager
-from tests._server.mocks import token_header, with_session
+from tests._server.conftest import (
+ get_user_config_manager,
+)
+from tests._server.mocks import get_session_manager, token_header, with_session
if TYPE_CHECKING:
+ import pytest
from starlette.testclient import TestClient
@@ -74,3 +77,32 @@ def test_save_user_config_with_partial_config(client: TestClient) -> None:
assert response.status_code == 200, response.text
assert response.headers["content-type"] == "application/json"
assert "success" in response.json()
+
+
+def test_save_user_config_starts_lsp_when_ty_enabled(
+ client: TestClient, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ called = False
+
+ async def mock_start_lsp_server() -> None:
+ nonlocal called
+ called = True
+
+ session_manager = get_session_manager(client)
+ monkeypatch.setattr(
+ session_manager, "start_lsp_server", mock_start_lsp_server
+ )
+
+ response = client.post(
+ "/api/kernel/save_user_config",
+ headers=HEADERS,
+ json={
+ "config": {
+ "completion": {"copilot": False},
+ "language_servers": {"ty": {"enabled": True}},
+ },
+ },
+ )
+
+ assert response.status_code == 200, response.text
+ assert called is True