From 28bffdef21d812595844eeea77b168eac44ba328 Mon Sep 17 00:00:00 2001 From: Hyu Date: Fri, 3 Jul 2026 15:26:18 +0800 Subject: [PATCH] fix(mcp): set _preserve_managed_process before finally in cold-start path (#2309) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _ColdStartRetry was caught in _lifecycle_loop_with_retry which set _preserve_managed_process = True — but by then the finally block inside _lifecycle_loop had already run and called _cleanup_box_stdio_session(), stopping the live managed process (return_code=143 SIGTERM). The cold-start retry then restarted a fresh process, eliminating the warm-up advantage. Fix: add an explicit except _ColdStartRetry in _lifecycle_loop that sets _preserve_managed_process = True before re-raising. The finally block then sees the flag and skips stop_managed_process, leaving the live process untouched for the next handshake attempt. Co-authored-by: dadachann <185672915+dadachann@users.noreply.github.com> --- src/langbot/pkg/provider/tools/loaders/mcp.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/langbot/pkg/provider/tools/loaders/mcp.py b/src/langbot/pkg/provider/tools/loaders/mcp.py index 499a00678..036dfe77d 100644 --- a/src/langbot/pkg/provider/tools/loaders/mcp.py +++ b/src/langbot/pkg/provider/tools/loaders/mcp.py @@ -439,6 +439,12 @@ class RuntimeMCPSession: else: await self._shutdown_event.wait() + except _ColdStartRetry: + # Cold-start in progress: set the preserve flag BEFORE the finally + # block runs so it does not stop the live managed process. The outer + # _lifecycle_loop_with_retry will reuse it on the next attempt. + self._preserve_managed_process = True + raise except Exception as e: self.status = MCPSessionStatus.ERROR self.error_message = str(e)