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: kill LSP child processes on shutdown to prevent memory leak (#8927)

Myles Scolnick353bef386343
diff --git a/marimo/_server/lsp.py b/marimo/_server/lsp.py
index aebc1cff68..221436ffdc 100644
--- a/marimo/_server/lsp.py
+++ b/marimo/_server/lsp.py
@@ -2,6 +2,8 @@
from __future__ import annotations
import asyncio
+import os
+import signal
import socket
import subprocess
import time
@@ -108,6 +110,11 @@ class BaseLspServer(LspServer):
stderr=subprocess.DEVNULL if is_windows() else subprocess.PIPE,
stdin=None,
text=True,
+ # Create a new process group so we can kill the entire
+ # tree (parent + children) on shutdown. Without this,
+ # child processes (e.g. copilot language-server.cjs)
+ # survive after the parent node process is terminated.
+ start_new_session=not is_windows(),
)
LOGGER.debug(
@@ -364,6 +371,34 @@ class BaseLspServer(LspServer):
except Exception as e:
LOGGER.error(f"Error monitoring {self.id} LSP health: {e}")
+ def _signal_process_tree(self, sig: signal.Signals) -> None:
+ """Send a signal to the process and all its children.
+
+ On Unix/macOS we started the subprocess in its own session
+ (start_new_session=True) so we can signal the whole process group
+ with os.killpg(). This ensures child processes like the copilot
+ language-server.cjs are cleaned up on shutdown.
+ """
+ if self.process is None:
+ LOGGER.warning(
+ "LSP server not running, cannot signal process tree"
+ )
+ return
+
+ try:
+ pgid = os.getpgid(self.process.pid)
+ os.killpg(pgid, sig)
+ except ProcessLookupError:
+ # Process or process group already exited; nothing to do.
+ return
+ except PermissionError:
+ # We can't signal the group; fall back to signalling
+ # the direct child only.
+ if sig == signal.SIGKILL:
+ self.process.kill()
+ else:
+ self.process.terminate()
+
def stop(self) -> None:
# Cancel health monitoring task
if (
@@ -374,7 +409,12 @@ class BaseLspServer(LspServer):
if self.process is not None:
LOGGER.debug("Stopping LSP server at port %s", self.port)
- self.process.terminate()
+
+ if is_windows():
+ self.process.terminate()
+ else:
+ self._signal_process_tree(signal.SIGTERM)
+
try:
# Wait for graceful shutdown with timeout
self.process.wait(timeout=5)
@@ -384,7 +424,10 @@ class BaseLspServer(LspServer):
LOGGER.warning(
"LSP server did not stop gracefully, forcing kill"
)
- self.process.kill()
+ if is_windows():
+ self.process.kill()
+ else:
+ self._signal_process_tree(signal.SIGKILL)
try:
self.process.wait(timeout=2)
except subprocess.TimeoutExpired:
diff --git a/packages/lsp/index.ts b/packages/lsp/index.ts
index f04ad99914..f9dfc8506d 100644
--- a/packages/lsp/index.ts
+++ b/packages/lsp/index.ts
@@ -194,6 +194,43 @@ function startWebSocketServer(
webSocket.close();
}
});
+
+ // When the parent process sends SIGTERM (e.g., marimo shutdown),
+ // close all WebSocket connections so that the onClose handlers fire
+ // and child language server processes (e.g. copilot) are killed via
+ // the jsonRpcConnection.dispose() → process.kill() chain.
+ let isShuttingDown = false;
+ const shutdown = () => {
+ if (isShuttingDown) {
+ return;
+ }
+ isShuttingDown = true;
+ logger.log("Received shutdown signal, closing all connections...");
+ for (const client of Array.from(webSocketServer.clients)) {
+ try {
+ client.close();
+ } catch (error) {
+ logger.error("Error while closing client WebSocket:", error);
+ }
+ }
+ // Wait for the server close callback so onClose handlers can fire
+ // and dispose child processes before we exit.
+ webSocketServer.close((error) => {
+ if (error) {
+ logger.error("Error while closing WebSocket server:", error);
+ }
+ });
+ // Force-exit if graceful shutdown takes too long. Use unref() so
+ // this timer doesn't keep the process alive if cleanup finishes
+ // earlier.
+ setTimeout(() => {
+ logger.error("Forced shutdown after timeout, exiting process.");
+ process.exit(0);
+ }, 5000).unref();
+ };
+
+ process.on("SIGTERM", shutdown);
+ process.on("SIGINT", shutdown);
}
async function main(): Promise<void> {
diff --git a/tests/_server/api/endpoints/test_terminal.py b/tests/_server/api/endpoints/test_terminal.py
index febcd3e586..7ab974f489 100644
--- a/tests/_server/api/endpoints/test_terminal.py
+++ b/tests/_server/api/endpoints/test_terminal.py
@@ -246,7 +246,7 @@ class TestCreateProcessCleanupHandler:
# Should try force kill
mock_kill.assert_any_call(mock_child_pid, signal.SIGKILL)
mock_waitpid.assert_called()
- mock_close.assert_called_with(mock_fd)
+ mock_close.assert_any_call(mock_fd)
def test_create_process_cleanup_handler_process_not_found(self) -> None:
"""Test cleanup handler when process doesn't exist."""
@@ -268,7 +268,7 @@ class TestCreateProcessCleanupHandler:
cleanup()
# Should still try to close the file descriptor
- mock_close.assert_called_with(mock_fd)
+ mock_close.assert_any_call(mock_fd)
def test_create_process_cleanup_handler_fd_error(self) -> None:
"""Test cleanup handler when file descriptor close fails."""
@@ -287,7 +287,7 @@ class TestCreateProcessCleanupHandler:
# Should not raise an exception
cleanup()
- mock_close.assert_called_with(mock_fd)
+ mock_close.assert_any_call(mock_fd)
class TestSetupChildProcess:
diff --git a/tests/_server/test_lsp.py b/tests/_server/test_lsp.py
index cd2b628289..07f4f43b75 100644
--- a/tests/_server/test_lsp.py
+++ b/tests/_server/test_lsp.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+import signal
from typing import TYPE_CHECKING, Literal, Union, cast
from unittest import mock
@@ -27,6 +28,7 @@ from marimo._server.lsp import (
TyServer,
any_lsp_server_running,
)
+from marimo._utils.platform import is_windows
if TYPE_CHECKING:
from collections.abc import Iterator
@@ -92,9 +94,19 @@ async def test_base_lsp_server_start_stop(
server.is_running() is True
) # Process exists and is running (returncode is None)
- # Test stop
- server.stop()
- mock_process.terminate.assert_called_once()
+ # Test stop - on Unix, should kill the process group
+ if is_windows():
+ server.stop()
+ mock_process.terminate.assert_called_once()
+ else:
+ mock_process.pid = 12345
+ with (
+ mock.patch("os.getpgid", return_value=12345) as mock_getpgid,
+ mock.patch("os.killpg") as mock_killpg,
+ ):
+ server.stop()
+ mock_getpgid.assert_called_once_with(12345)
+ mock_killpg.assert_called_once_with(12345, signal.SIGTERM)
# Verify wait was called with timeout for graceful shutdown
mock_process.wait.assert_called_once_with(timeout=5)
assert server.process is None
@@ -122,16 +134,50 @@ async def test_base_lsp_server_stop_force_kill_on_timeout(
None, # Second wait (after kill)
]
- # Test stop with force kill
- server.stop()
+ if is_windows():
+ server.stop()
+ mock_process.terminate.assert_called_once()
+ mock_process.kill.assert_called_once()
+ else:
+ mock_process.pid = 12345
+ with (
+ mock.patch("os.getpgid", return_value=12345),
+ mock.patch("os.killpg") as mock_killpg,
+ ):
+ server.stop()
+ # Should have been called twice: SIGTERM then SIGKILL
+ assert mock_killpg.call_count == 2
+ mock_killpg.assert_any_call(12345, signal.SIGTERM)
+ mock_killpg.assert_any_call(12345, signal.SIGKILL)
- mock_process.terminate.assert_called_once()
- mock_process.kill.assert_called_once()
# wait should be called twice: once after terminate, once after kill
assert mock_process.wait.call_count == 2
assert server.process is None
[email protected](is_windows(), reason="Unix-only process group test")
+async def test_base_lsp_server_stop_noop_on_process_lookup_error(
+ mock_popen: mock.MagicMock,
+ mock_process: mock.MagicMock,
+):
+ """Test that stop() treats ProcessLookupError as a no-op (already exited)."""
+ del mock_popen
+
+ server = MockLspServer(port=8000)
+ server.validate_requirements = mock.MagicMock(return_value=True)
+
+ with mock.patch.object(server, "_wait_until_ready", return_value=True):
+ await server.start()
+
+ mock_process.pid = 12345
+ with mock.patch("os.getpgid", side_effect=ProcessLookupError):
+ server.stop()
+ # ProcessLookupError means process already exited; should not
+ # fall back to terminate/kill.
+ mock_process.terminate.assert_not_called()
+ assert server.process is None
+
+
async def test_base_lsp_server_start_lock_prevents_concurrent_starts(
mock_popen: mock.MagicMock,
mock_process: mock.MagicMock,