From 176583a329caba5277a28c20adcab6522d5019b1 Mon Sep 17 00:00:00 2001 From: Chen Frydman Date: Tue, 10 Mar 2026 09:55:36 +0000 Subject: [PATCH] fix: terminate MLflow server when Caldera exits The MLflow server was started with subprocess.Popen() but the process reference was discarded, leaving it running as an orphan after Caldera shuts down. Store the Popen reference in _mlflow_proc and register an atexit handler that terminates it gracefully (with a 5-second timeout before force-kill) when the Caldera process exits. --- hook.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/hook.py b/hook.py index 822b11e..d1e6fa0 100644 --- a/hook.py +++ b/hook.py @@ -5,6 +5,7 @@ import traceback import logging import psutil +import atexit from app.utility.base_world import BaseWorld @@ -53,6 +54,18 @@ def is_port_open(port): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: return sock.connect_ex(('127.0.0.1', port)) == 0 +_mlflow_proc = None + +def _shutdown_mlflow(): + """Terminate the MLflow server process started by this plugin.""" + if _mlflow_proc is not None and _mlflow_proc.poll() is None: + log.info("[MCP] Shutting down MLflow server...") + _mlflow_proc.terminate() + try: + _mlflow_proc.wait(timeout=5) + except subprocess.TimeoutExpired: + _mlflow_proc.kill() + # ๐Ÿ” Start MLflow server if it's not already running if not is_port_open(5000): # ๐Ÿงผ Kill old MLflow server if it exists @@ -61,13 +74,14 @@ def is_port_open(port): plugin_dir = os.path.dirname(os.path.abspath(__file__)) mlruns_path = os.path.join(plugin_dir, 'mlruns') db_path = os.path.join(plugin_dir, 'mlruns.db') - subprocess.Popen([ + _mlflow_proc = subprocess.Popen([ "mlflow", "server", "--backend-store-uri", f"sqlite:///{db_path}", "--default-artifact-root", mlruns_path, "--host", "127.0.0.1", "--port", "5000" ], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + atexit.register(_shutdown_mlflow) log.debug("[MCP] Starting MLflow server at http://localhost:5000") except Exception as e: log.error(f"[MCP] Failed to start MLflow server: {e}")