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: LSP servers not stopping gracefully on shutdown (#9084)
Myles Scolnick21cfe3eae678
diff --git a/marimo/_server/lsp.py b/marimo/_server/lsp.pyindex 221436ffdc..e30813627a 100644--- a/marimo/_server/lsp.py+++ b/marimo/_server/lsp.py@@ -399,7 +399,30 @@ class BaseLspServer(LspServer):else:self.process.terminate()- def stop(self) -> None:+ def _close_pipes(self) -> None:+ """Close stdout/stderr pipes to unblock the child process.++ If the child is writing to a pipe whose buffer is full, it will+ block forever. Closing the parent's end of the pipe lets the+ child receive EPIPE/SIGPIPE so it can exit, and prevents+ process.wait() from deadlocking.+ """+ if self.process is None:+ return+ for stream in (self.process.stdout, self.process.stderr):+ if stream is not None:+ try:+ stream.close()+ except OSError:+ pass++ def _begin_stop(self) -> None:+ """Send termination signal and close pipes.++ Call this on every server *before* calling _finish_stop() so that+ all servers receive SIGTERM concurrently and the wait timeouts+ don't stack up.+ """# Cancel health monitoring taskif (self._health_check_task is not None@@ -415,26 +438,39 @@ class BaseLspServer(LspServer):else:self._signal_process_tree(signal.SIGTERM)+ # Close pipes so the child doesn't block writing to a full+ # pipe buffer, which would prevent it from exiting and cause+ # wait() to deadlock (see Python subprocess docs).+ self._close_pipes()++ def _finish_stop(self) -> None:+ """Wait for the process to exit, force-killing if needed.++ Must be called after _begin_stop().+ """+ if self.process is None:+ LOGGER.debug("LSP server not running")+ return++ try:+ self.process.wait(timeout=2)+ LOGGER.debug("LSP server stopped gracefully")+ except subprocess.TimeoutExpired:+ # Force kill if process doesn't respond to terminate+ LOGGER.warning("LSP server did not stop gracefully, forcing kill")+ if is_windows():+ self.process.kill()+ else:+ self._signal_process_tree(signal.SIGKILL)try:- # Wait for graceful shutdown with timeout- self.process.wait(timeout=5)- LOGGER.debug("LSP server stopped gracefully")+ self.process.wait(timeout=2)except subprocess.TimeoutExpired:- # Force kill if process doesn't respond to terminate- LOGGER.warning(- "LSP server did not stop gracefully, forcing kill"- )- if is_windows():- self.process.kill()- else:- self._signal_process_tree(signal.SIGKILL)- try:- self.process.wait(timeout=2)- except subprocess.TimeoutExpired:- LOGGER.error("Failed to kill LSP server process")- self.process = None- else:- LOGGER.debug("LSP server not running")+ LOGGER.error("Failed to kill LSP server process")+ self.process = None++ def stop(self) -> None:+ self._begin_stop()+ self._finish_stop()def validate_requirements(self) -> Union[str, Literal[True]]:raise NotImplementedError()@@ -823,8 +859,18 @@ class CompositeLspServer(LspServer):return alerts[0] if alerts else Nonedef stop(self) -> None:+ # Signal all servers to stop first (send SIGTERM + close pipes)+ # so they shut down concurrently, then wait for each one.+ # This avoids stacking the per-server wait timeouts.+ for server in self.servers.values():+ if isinstance(server, BaseLspServer):+ server._begin_stop()+ else:+ server.stop()+ continuefor server in self.servers.values():- server.stop()+ if isinstance(server, BaseLspServer):+ server._finish_stop()def is_running(self) -> bool:return any(server.is_running() for server in self.servers.values())diff --git a/packages/lsp/index.test.ts b/packages/lsp/index.test.tsindex b5e87eddf6..4334f4a201 100644--- a/packages/lsp/index.test.ts+++ b/packages/lsp/index.test.ts@@ -70,7 +70,7 @@ describe("parseTypedCommand", () => {describe("LSP Server Integration", () => {it("should start and be killable", async () => {- const process = childProcess.spawn("node", [+ const proc = childProcess.spawn("node", ["./dist/index.cjs","--lsp","echo hello",@@ -78,22 +78,27 @@ describe("LSP Server Integration", () => {path.join(os.tmpdir(), "lsp-server-test.log"),]);+ // Register the exit listener BEFORE sending the signal to avoid+ // a race where the process exits before the listener is attached.+ const exitCode = new Promise<number | null>((resolve) => {+ proc.on("exit", (code) => resolve(code));+ });+// Give it a moment to startawait new Promise((resolve) => setTimeout(resolve, 1000));- // Kill the process- process.kill();-- // Wait for process to die- await new Promise<void>((resolve) => {- process.on("exit", () => resolve());- });+ proc.kill();+ const code = await exitCode;- expect(process.killed).toBe(true);+ // Process should have exited (0 = graceful shutdown, null = signal)+ expect(code === 0 || code === null).toBe(true);+ // Verify the process actually terminated: exitCode is non-null for+ // normal exit, signalCode is non-null for signal-based termination.+ expect(proc.exitCode !== null || proc.signalCode !== null).toBe(true);});it("should start with typed copilot command", async () => {- const process = childProcess.spawn("node", [+ const proc = childProcess.spawn("node", ["./dist/index.cjs","--lsp","copilot:echo copilot-test",@@ -101,17 +106,17 @@ describe("LSP Server Integration", () => {path.join(os.tmpdir(), "lsp-server-copilot-test.log"),]);+ const exitCode = new Promise<number | null>((resolve) => {+ proc.on("exit", (code) => resolve(code));+ });+// Give it a moment to startawait new Promise((resolve) => setTimeout(resolve, 1000));- // Kill the process- process.kill();-- // Wait for process to die- await new Promise<void>((resolve) => {- process.on("exit", () => resolve());- });+ proc.kill();+ const code = await exitCode;- expect(process.killed).toBe(true);+ expect(code === 0 || code === null).toBe(true);+ expect(proc.exitCode !== null || proc.signalCode !== null).toBe(true);});});diff --git a/packages/lsp/index.ts b/packages/lsp/index.tsindex 8c7779254e..6538a4d3fa 100644--- a/packages/lsp/index.ts+++ b/packages/lsp/index.ts@@ -217,13 +217,16 @@ function startWebSocketServer(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.+ // Force-exit if graceful shutdown takes too long. On Unix/non-Windows+ // deployments, the parent process may send SIGTERM to the whole process+ // group, so child LSP processes may already have received the signal —+ // we still exit promptly here after attempting graceful cleanup. 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();+ }, 500).unref();};process.on("SIGTERM", shutdown);diff --git a/tests/_server/test_lsp.py b/tests/_server/test_lsp.pyindex 07f4f43b75..8711b32b6c 100644--- a/tests/_server/test_lsp.py+++ b/tests/_server/test_lsp.py@@ -108,7 +108,7 @@ async def test_base_lsp_server_start_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)+ mock_process.wait.assert_called_once_with(timeout=2)assert server.process is None@@ -130,7 +130,7 @@ async def test_base_lsp_server_stop_force_kill_on_timeout(# Make wait() raise TimeoutExpired to simulate hung processmock_process.wait.side_effect = [- subprocess.TimeoutExpired("cmd", 5), # First wait (terminate)+ subprocess.TimeoutExpired("cmd", 2), # First wait (terminate)None, # Second wait (after kill)]