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.

perf: start LSP and MCP in the background, avoid repetitive io reads (#6142)

Myles Scolnickdbe347eaea24
diff --git a/marimo/_config/manager.py b/marimo/_config/manager.py
index 2d700b1c44..87fa9bd115 100644
--- a/marimo/_config/manager.py
+++ b/marimo/_config/manager.py
@@ -3,7 +3,7 @@ from __future__ import annotations
import os
from abc import abstractmethod
-from functools import cached_property
+from functools import cached_property, lru_cache
from pathlib import Path
from typing import Optional, Union, cast
@@ -180,6 +180,10 @@ class ProjectConfigManager(PartialMarimoConfigReader):
def __init__(self, start_path: str) -> None:
self.pyproject_path = find_nearest_pyproject_toml(start_path)
+ # It is safe to cache this config, as we only read from the pyproject.toml
+ # and never update it. If the user updates the pyproject.toml,
+ # it is ok to expect updates to be reflected after a server restart.
+ @lru_cache(maxsize=2) # noqa: B019
def get_config(self, *, hide_secrets: bool = True) -> PartialMarimoConfig:
try:
if self.pyproject_path is None:
@@ -302,6 +306,10 @@ class ScriptConfigManager(PartialMarimoConfigReader):
def __init__(self, filename: Optional[str]) -> None:
self.filename = filename
+ # It is safe to cache this config, as we only read from the script
+ # and never update it. If the user updates the script,
+ # it is ok to expect updates to be reflected after a server restart.
+ @lru_cache(maxsize=2) # noqa: B019
def get_config(self, *, hide_secrets: bool = True) -> PartialMarimoConfig:
if self.filename is None:
return {}
diff --git a/marimo/_config/utils.py b/marimo/_config/utils.py
index 0d904e2f4a..a2abadb6dd 100644
--- a/marimo/_config/utils.py
+++ b/marimo/_config/utils.py
@@ -2,6 +2,7 @@
from __future__ import annotations
import os
+from functools import lru_cache
from typing import Any, Optional
from marimo import _loggers
@@ -47,6 +48,11 @@ def get_or_create_user_config_path() -> str:
return str(config_path)
+# This operation may be expensive due to searching for a config file up to
+# the home directory.
+# We cache the result to avoid re-searching. It is ok to expect new
+# config files to only be picked up after a restart.
+@lru_cache(maxsize=1)
def get_user_config_path() -> Optional[str]:
"""Find path of config file (.marimo.toml).
diff --git a/marimo/_runtime/context/script_context.py b/marimo/_runtime/context/script_context.py
index 359ed026d9..a433832ae9 100644
--- a/marimo/_runtime/context/script_context.py
+++ b/marimo/_runtime/context/script_context.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import sys
from contextlib import contextmanager
from dataclasses import dataclass
+from functools import cached_property
from typing import TYPE_CHECKING, Any, Optional
from marimo._ast.app import AppKernelRunnerRegistry
@@ -65,12 +66,16 @@ class ScriptRuntimeContext(RuntimeContext):
def execution_context(self) -> ExecutionContext | None:
return self._app.execution_context
- @property
- def marimo_config(self) -> MarimoConfig:
+ @cached_property
+ def _cached_config(self) -> MarimoConfig:
return get_default_config_manager(
current_path=self.filename
).get_config()
+ @property
+ def marimo_config(self) -> MarimoConfig:
+ return self._cached_config
+
@property
def cell_id(self) -> Optional[CellId_t]:
"""Get the cell id of the currently executing cell, if any."""
diff --git a/marimo/_server/api/endpoints/config.py b/marimo/_server/api/endpoints/config.py
index 973f54316c..b4e90d993c 100644
--- a/marimo/_server/api/endpoints/config.py
+++ b/marimo/_server/api/endpoints/config.py
@@ -1,16 +1,17 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
-from typing import TYPE_CHECKING
+from typing import TYPE_CHECKING, Optional
from starlette.authentication import requires
+from starlette.background import BackgroundTask
+from starlette.responses import JSONResponse
from marimo import _loggers
from marimo._runtime.requests import SetUserConfigRequest
from marimo._server.api.deps import AppState
from marimo._server.api.utils import parse_request
from marimo._server.models.models import (
- BaseResponse,
SaveUserConfigurationRequest,
SuccessResponse,
)
@@ -31,7 +32,7 @@ router = APIRouter()
async def save_user_config(
*,
request: Request,
-) -> BaseResponse:
+) -> JSONResponse:
"""
requestBody:
content:
@@ -53,10 +54,13 @@ async def save_user_config(
)
config = app_state.config_manager.save_config(body.config)
+ background_task: Optional[BackgroundTask] = None
# Update the server's view of the config
if config["completion"]["copilot"]:
LOGGER.debug("Starting copilot server")
- await app_state.session_manager.start_lsp_server()
+ background_task = BackgroundTask(
+ app_state.session_manager.start_lsp_server
+ )
# Update the kernel's view of the config
# Session could be None if the user is on the home page
@@ -68,4 +72,8 @@ async def save_user_config(
app_state.require_current_session_id()
),
)
- return SuccessResponse()
+
+ return JSONResponse(
+ content=SuccessResponse().as_camel_case(),
+ background=background_task,
+ )
diff --git a/marimo/_server/api/lifespans.py b/marimo/_server/api/lifespans.py
index f9cf2e225a..62d642465f 100644
--- a/marimo/_server/api/lifespans.py
+++ b/marimo/_server/api/lifespans.py
@@ -4,7 +4,7 @@ from __future__ import annotations
import asyncio
import contextlib
import socket
-from typing import TYPE_CHECKING
+from typing import TYPE_CHECKING, Any, Optional
from marimo import _loggers
from marimo._server.api.deps import AppState, AppStateBase
@@ -30,6 +30,8 @@ if TYPE_CHECKING:
LOGGER = _loggers.marimo_logger()
+background_tasks: set[asyncio.Task[Any]] = set()
+
@contextlib.asynccontextmanager
async def lsp(app: Starlette) -> AsyncIterator[None]:
@@ -38,16 +40,36 @@ async def lsp(app: Starlette) -> AsyncIterator[None]:
session_mgr = state.session_manager
# Only start the LSP server in Edit mode
- if session_mgr.mode == SessionMode.EDIT:
- if any_lsp_server_running(user_config):
- LOGGER.debug("Language Servers are enabled")
- await session_mgr.start_lsp_server()
+ if session_mgr.mode != SessionMode.EDIT:
+ yield
+ return
+
+ # Only start the LSP server if any LSP servers are configured
+ if not any_lsp_server_running(user_config):
+ yield
+ return
+
+ LOGGER.debug("Language Servers are enabled")
+ # Start LSP server in background to avoid blocking server startup
+ task = asyncio.create_task(session_mgr.start_lsp_server())
+ background_tasks.add(task) # Keep a reference to prevent GC
+ task.add_done_callback(background_tasks.discard) # Clean up when done
yield
+ # Shutdown
+ task.cancel()
+ try:
+ await task
+ except asyncio.CancelledError:
+ pass
+
@contextlib.asynccontextmanager
async def mcp(app: Starlette) -> AsyncIterator[None]:
+ if TYPE_CHECKING:
+ from marimo._server.ai.mcp import MCPClient
+
state = AppState.from_app(app)
session_mgr = state.session_manager
user_config = state.config_manager.get_config()
@@ -55,42 +77,45 @@ async def mcp(app: Starlette) -> AsyncIterator[None]:
"mcp_docs", False
)
- mcp_client = None # Track MCP client for cleanup
-
# Only start MCP servers in Edit mode
- if session_mgr.mode == SessionMode.EDIT and mcp_docs_enabled:
+ if session_mgr.mode != SessionMode.EDIT or not mcp_docs_enabled:
+ yield
+ return
+
+ LOGGER.warning("MCP servers are experimental and may not work as expected")
+
+ async def background_connect_mcp_servers() -> Optional[MCPClient]:
try:
from marimo._server.ai.mcp import get_mcp_client
mcp_client = get_mcp_client()
- LOGGER.warning(
- "MCP servers are experimental and may not work as expected"
+ await mcp_client.connect_to_all_servers()
+ LOGGER.info(
+ f"MCP servers connected: {list(mcp_client.servers.keys())}"
)
- if mcp_client and mcp_client.servers:
- LOGGER.debug(
- f"Starting MCP servers: {list(mcp_client.servers.keys())}"
- )
- await mcp_client.connect_to_all_servers()
- LOGGER.info(
- f"MCP servers connected: {len(mcp_client.servers)}"
- )
- else:
- LOGGER.debug("No MCP servers configured")
+ return mcp_client
except Exception as e:
- LOGGER.warning(f"Failed to connect MCP servers: {e}")
+ LOGGER.warning(f"Failed to connect MCP servers in background: {e}")
+ return None
+
+ task = asyncio.create_task(background_connect_mcp_servers())
+ background_tasks.add(task) # Keep a reference to prevent GC
+ task.add_done_callback(background_tasks.discard) # Clean up when done
yield
- # Clean up MCP connections on shutdown
- if mcp_client:
- try:
- LOGGER.info(
- f"About to disconnect from all MCP servers in task: {id(asyncio.current_task())}"
- )
+ # Shutdown
+ task.cancel()
+ try:
+ mcp_client = await task
+ if mcp_client:
+ LOGGER.info("Disconnecting from all MCP servers")
await mcp_client.disconnect_from_all_servers()
LOGGER.info("Successfully disconnected from all MCP servers")
- except Exception as e:
- LOGGER.error(f"Error during MCP cleanup: {e}")
+ except asyncio.CancelledError:
+ pass
+ except Exception as e:
+ LOGGER.error(f"Error during MCP cleanup: {e}")
@contextlib.asynccontextmanager
diff --git a/marimo/_server/lsp.py b/marimo/_server/lsp.py
index 9d866b3cf9..3caa6f202b 100644
--- a/marimo/_server/lsp.py
+++ b/marimo/_server/lsp.py
@@ -1,6 +1,7 @@
# Copyright 2025 Marimo. All rights reserved.
from __future__ import annotations
+import asyncio
import subprocess
from abc import ABC, abstractmethod
from pathlib import Path
@@ -24,7 +25,7 @@ class LspServer(ABC):
id: str
@abstractmethod
- def start(self) -> Optional[Alert]:
+ async def start(self) -> Optional[Alert]:
pass
@abstractmethod
@@ -42,12 +43,13 @@ class BaseLspServer(LspServer):
self.process: Optional[subprocess.Popen[str]] = None
@server_tracer.start_as_current_span("lsp_server.start")
- def start(self) -> Optional[Alert]:
+ async def start(self) -> Optional[Alert]:
if self.process is not None:
LOGGER.debug("LSP server already started")
return None
- validation_msg = self.validate_requirements()
+ # Validation could be expensive, so we run it in a thread
+ validation_msg = await asyncio.to_thread(self.validate_requirements)
if validation_msg is not True:
LOGGER.error(
f"Cannot start {self.id} LSP server: {validation_msg}"
@@ -64,7 +66,7 @@ class BaseLspServer(LspServer):
return None
LOGGER.debug("... running command: %s", cmd)
- self.process = subprocess.Popen(
+ self.process = subprocess.Popen( # noqa: ASYNC220
cmd,
# only show stdout when in development
stdout=subprocess.PIPE
@@ -219,12 +221,12 @@ class CopilotLspServer(BaseLspServer):
class PyLspServer(BaseLspServer):
id = "pylsp"
- def start(self) -> Optional[Alert]:
+ async def start(self) -> Optional[Alert]:
# pylsp is not required, so we don't want to alert or fail if it is not installed
if not DependencyManager.pylsp.has():
LOGGER.debug("pylsp is not installed. Skipping LSP server.")
return None
- return super().start()
+ return await super().start()
def validate_requirements(self) -> Union[str, Literal[True]]:
if DependencyManager.pylsp.has():
@@ -260,12 +262,12 @@ class PyLspServer(BaseLspServer):
class BasedpyrightServer(BaseLspServer):
id = "basedpyright"
- def start(self) -> Optional[Alert]:
+ async def start(self) -> Optional[Alert]:
# basedpyright is not required, so we don't want to alert or fail if it is not installed
if not DependencyManager.basedpyright.has():
LOGGER.debug("basedpyright is not installed. Skipping LSP server.")
return None
- return super().start()
+ return await super().start()
def validate_requirements(self) -> Union[str, Literal[True]]:
if not DependencyManager.basedpyright.has():
@@ -302,12 +304,12 @@ class BasedpyrightServer(BaseLspServer):
class TyServer(BaseLspServer):
id = "ty"
- def start(self) -> Optional[Alert]:
+ async def start(self) -> Optional[Alert]:
# ty is not required, so we don't want to alert or fail if it is not installed
if not DependencyManager.ty.has():
LOGGER.debug("ty is not installed. Skipping LSP server.")
return None
- return super().start()
+ return await super().start()
def validate_requirements(self) -> Union[str, Literal[True]]:
if not DependencyManager.ty.has():
@@ -344,7 +346,7 @@ class TyServer(BaseLspServer):
class NoopLspServer(LspServer):
- def start(self) -> None:
+ async def start(self) -> None:
pass
def stop(self) -> None:
@@ -383,9 +385,7 @@ class CompositeLspServer(LspServer):
last_free_port = find_free_port(last_free_port + 1)
self.servers[server_name] = server_constructor(last_free_port)
- def _is_enabled(self, server_name: str) -> bool:
- # .get_config() is not cached
- config = self.config_reader.get_config()
+ def _is_enabled(self, config: MarimoConfig, server_name: str) -> bool:
if server_name == "copilot":
copilot = config["completion"]["copilot"]
return copilot is True or copilot == "github"
@@ -397,19 +397,23 @@ class CompositeLspServer(LspServer):
.get("enabled", False),
)
- def start(self) -> Optional[Alert]:
- alerts: list[Alert] = []
+ async def start(self) -> Optional[Alert]:
+ # .get_config() should not be cached, as it may be updated by the user
+ config = self.config_reader.get_config()
+ tasks: list[asyncio.Task[Optional[Alert]]] = []
+
for server_name, server in self.servers.items():
- if not self._is_enabled(server_name):
+ if not self._is_enabled(config, server_name):
# We don't shut down the server if it is already running
# in case the user wants to re-enable it
continue
# We call start again even for existing servers in case it failed
# to start the first time (e.g. got new dependencies)
- alert = server.start()
- if alert is not None:
- alerts.append(alert)
+ tasks.append(asyncio.create_task(server.start()))
+ # Start all servers in parallel
+ results = await asyncio.gather(*tasks)
+ alerts = [alert for alert in results if alert is not None]
return alerts[0] if alerts else None
def stop(self) -> None:
diff --git a/marimo/_server/sessions.py b/marimo/_server/sessions.py
index 55133791cf..52ecf71f77 100644
--- a/marimo/_server/sessions.py
+++ b/marimo/_server/sessions.py
@@ -1097,7 +1097,7 @@ class SessionManager:
return
LOGGER.debug("Starting LSP server...")
- alert = self.lsp_server.start()
+ alert = await self.lsp_server.start()
if alert is not None:
LOGGER.warning(
diff --git a/marimo/_utils/timer.py b/marimo/_utils/timer.py
new file mode 100644
index 0000000000..162d136298
--- /dev/null
+++ b/marimo/_utils/timer.py
@@ -0,0 +1,22 @@
+import functools
+import time
+from typing import Any, Callable
+
+
+def timer(func: Callable[..., Any]) -> Callable[..., Any]:
+ """
+ A decorator that measures and prints the execution time of a function.
+
+ This should only be used for manual debugging.
+ """
+
+ @functools.wraps(func)
+ def wrapper(*args: Any, **kwargs: Any) -> Any:
+ start_time = time.perf_counter()
+ result = func(*args, **kwargs)
+ end_time = time.perf_counter()
+ execution_time = end_time - start_time
+ print(f"{func.__name__} took {execution_time:.4f} seconds to execute") # noqa: T201
+ return result
+
+ return wrapper
diff --git a/tests/_config/test_config_utils.py b/tests/_config/test_config_utils.py
index 28db64ca8a..15235de6a5 100644
--- a/tests/_config/test_config_utils.py
+++ b/tests/_config/test_config_utils.py
@@ -50,11 +50,15 @@ def test_get_config_path():
xdg_config_path = str(Path("~/.config/marimo/marimo.toml").expanduser())
home_config_path = str(Path("~/.marimo.toml").expanduser())
+ get_user_config_path.cache_clear()
+
# If neither config exists, return None
with _mock_file_exists(doesnt_exist=[xdg_config_path, home_config_path]):
found_config_path = get_user_config_path()
assert found_config_path is None
+ get_user_config_path.cache_clear()
+
# If only XDG path exists, use XDG path
with _mock_file_exists(
exists=xdg_config_path, doesnt_exist=home_config_path
@@ -62,6 +66,8 @@ def test_get_config_path():
found_config_path = get_user_config_path()
assert found_config_path == xdg_config_path
+ get_user_config_path.cache_clear()
+
# If both config paths exist, home config takes precedence
with _mock_file_exists(exists=[xdg_config_path, home_config_path]):
found_config_path = get_user_config_path()
@@ -76,6 +82,8 @@ def test_get_or_create_config_path():
xdg_config_path = str(Path(temp_dir) / "marimo/marimo.toml")
home_config_path = str(Path("~/.marimo.toml").expanduser())
+ get_user_config_path.cache_clear()
+
# If neither config exists, XDG config should be created and used
with _mock_file_exists(
doesnt_exist=[xdg_config_path, home_config_path]
@@ -83,6 +91,8 @@ def test_get_or_create_config_path():
found_config_path = get_or_create_user_config_path()
assert found_config_path == xdg_config_path
+ get_user_config_path.cache_clear()
+
# If only XDG path exists, use XDG path
with _mock_file_exists(
exists=xdg_config_path, doesnt_exist=home_config_path
@@ -90,6 +100,8 @@ def test_get_or_create_config_path():
found_config_path = get_or_create_user_config_path()
assert found_config_path == xdg_config_path
+ get_user_config_path.cache_clear()
+
# If both config paths exist, home config takes precedence
with _mock_file_exists(exists=[xdg_config_path, home_config_path]):
found_config_path = get_or_create_user_config_path()
diff --git a/tests/_server/test_lsp.py b/tests/_server/test_lsp.py
index e9504b8700..4125502526 100644
--- a/tests/_server/test_lsp.py
+++ b/tests/_server/test_lsp.py
@@ -61,7 +61,7 @@ class MockLspServer(BaseLspServer):
return Alert(title="Mock Alert", description="Mock missing binary")
-def test_base_lsp_server_start_stop(
+async def test_base_lsp_server_start_stop(
mock_popen: mock.MagicMock,
mock_process: mock.MagicMock,
):
@@ -69,7 +69,7 @@ def test_base_lsp_server_start_stop(
server.validate_requirements = mock.MagicMock(return_value=False)
# Without binary install
- alert = server.start()
+ alert = await server.start()
assert alert is not None
assert alert.title == "Mock Alert"
assert alert.description == "Mock missing binary"
@@ -77,7 +77,7 @@ def test_base_lsp_server_start_stop(
server.validate_requirements = mock.MagicMock(return_value=True)
# Test start
- alert = server.start()
+ alert = await server.start()
assert alert is None
mock_popen.assert_called_once()
assert server.is_running() is False # Process exists but not running
@@ -88,16 +88,16 @@ def test_base_lsp_server_start_stop(
assert server.process is None
-def test_base_lsp_server_missing_binary(mock_which: mock.MagicMock):
+async def test_base_lsp_server_missing_binary(mock_which: mock.MagicMock):
server = MockLspServer(port=8000)
mock_which.return_value = None
server.validate_requirements = mock.MagicMock(return_value=False)
- alert = server.start()
+ alert = await server.start()
assert alert is not None
assert alert.title == "Mock Alert"
-def test_pylsp_server():
+async def test_pylsp_server():
import sys
server = PyLspServer(port=8000)
@@ -229,14 +229,15 @@ def test_composite_server():
{"copilot": True, "activate_on_typing": True}
)
config_reader = as_reader(completion_config, config)
+ config = config_reader.get_config()
server = CompositeLspServer(config_reader, min_port=8000)
assert (
len(server.servers) == total_lsp_servers
) # Both pylsp and copilot enabled
- assert server._is_enabled("pylsp") is True
- assert server._is_enabled("copilot") is True
- assert server._is_enabled("ty") is True
- assert server._is_enabled("basedpyright") is True
+ assert server._is_enabled(config, "pylsp") is True
+ assert server._is_enabled(config, "copilot") is True
+ assert server._is_enabled(config, "ty") is True
+ assert server._is_enabled(config, "basedpyright") is True
# Test with only pylsp
config = LanguageServersConfig({"pylsp": {"enabled": True}})
@@ -244,11 +245,12 @@ def test_composite_server():
{"copilot": False, "activate_on_typing": True}
)
config_reader = as_reader(completion_config, config)
+ config = config_reader.get_config()
server = CompositeLspServer(config_reader, min_port=8000)
assert len(server.servers) == total_lsp_servers
- assert server._is_enabled("pylsp") is True
- assert server._is_enabled("copilot") is False
- assert server._is_enabled("ty") is False
+ assert server._is_enabled(config, "pylsp") is True
+ assert server._is_enabled(config, "copilot") is False
+ assert server._is_enabled(config, "ty") is False
# Test with only ty enabled
config = LanguageServersConfig(
@@ -262,12 +264,13 @@ def test_composite_server():
{"copilot": False, "activate_on_typing": True}
)
config_reader = as_reader(completion_config, config)
+ config = config_reader.get_config()
server = CompositeLspServer(config_reader, min_port=8000)
assert len(server.servers) == total_lsp_servers
- assert server._is_enabled("pylsp") is False
- assert server._is_enabled("basedpyright") is False
- assert server._is_enabled("copilot") is False
- assert server._is_enabled("ty") is True
+ assert server._is_enabled(config, "pylsp") is False
+ assert server._is_enabled(config, "basedpyright") is False
+ assert server._is_enabled(config, "copilot") is False
+ assert server._is_enabled(config, "ty") is True
# Test with only basedpyright enabled
config = LanguageServersConfig(
@@ -281,12 +284,13 @@ def test_composite_server():
{"copilot": False, "activate_on_typing": True}
)
config_reader = as_reader(completion_config, config)
+ config = config_reader.get_config()
server = CompositeLspServer(config_reader, min_port=8000)
assert len(server.servers) == total_lsp_servers
- assert server._is_enabled("pylsp") is False
- assert server._is_enabled("basedpyright") is True
- assert server._is_enabled("copilot") is False
- assert server._is_enabled("ty") is False
+ assert server._is_enabled(config, "pylsp") is False
+ assert server._is_enabled(config, "basedpyright") is True
+ assert server._is_enabled(config, "copilot") is False
+ assert server._is_enabled(config, "ty") is False
# Test with nothing enabled
config = LanguageServersConfig({"pylsp": {"enabled": False}})
@@ -294,11 +298,12 @@ def test_composite_server():
{"copilot": False, "activate_on_typing": True}
)
config_reader = as_reader(completion_config, config)
+ config = config_reader.get_config()
server = CompositeLspServer(config_reader, min_port=8000)
assert len(server.servers) == total_lsp_servers
- assert server._is_enabled("pylsp") is False
- assert server._is_enabled("copilot") is False
- assert server._is_enabled("ty") is False
+ assert server._is_enabled(config, "pylsp") is False
+ assert server._is_enabled(config, "copilot") is False
+ assert server._is_enabled(config, "ty") is False
def test_any_lsp_server_running():