From 81109a0f72cdcef280ba11af29009b41f4c88edd Mon Sep 17 00:00:00 2001 From: ehhuang Date: Wed, 9 Jul 2025 20:59:37 -0700 Subject: [PATCH] test: terminate server process when finished (#2700) # What does this PR do? Terminate server process for real. ## Test Plan ```ENABLE_OPENAI=openai LLAMA_STACK_CONFIG=server:starter pytest -v tests/integration/agents/test_openai_responses.py --text-model "gpt-4o-mini" -vv -s -k 'test_list_response_input_items[' && lsof -ti:8321``` observe no process printed anymore --- tests/integration/fixtures/common.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tests/integration/fixtures/common.py b/tests/integration/fixtures/common.py index 27b2d6dc3..28a047ea5 100644 --- a/tests/integration/fixtures/common.py +++ b/tests/integration/fixtures/common.py @@ -6,6 +6,7 @@ import inspect import os +import signal import socket import subprocess import tempfile @@ -45,6 +46,8 @@ def start_llama_stack_server(config_name: str) -> subprocess.Popen: stderr=subprocess.PIPE, # keep stderr to see errors text=True, env={**os.environ, "LLAMA_STACK_LOG_FILE": "server.log"}, + # Create new process group so we can kill all child processes + preexec_fn=os.setsid, ) return process @@ -267,14 +270,17 @@ def cleanup_server_process(request): print(f"Server process already terminated with return code: {server_process.returncode}") return try: - server_process.terminate() + print(f"Terminating process {server_process.pid} and its group...") + # Kill the entire process group + os.killpg(os.getpgid(server_process.pid), signal.SIGTERM) server_process.wait(timeout=10) - print("Server process terminated gracefully") + print("Server process and children terminated gracefully") except subprocess.TimeoutExpired: print("Server process did not terminate gracefully, killing it") - server_process.kill() + # Force kill the entire process group + os.killpg(os.getpgid(server_process.pid), signal.SIGKILL) server_process.wait() - print("Server process killed") + print("Server process and children killed") except Exception as e: print(f"Error during server cleanup: {e}") else: