test: terminate server process when finished

# What does this PR do?


## Test Plan
This commit is contained in:
Eric Huang 2025-07-09 20:48:02 -07:00
parent 780b4c6eea
commit 982442d584

View file

@ -6,6 +6,7 @@
import inspect import inspect
import os import os
import signal
import socket import socket
import subprocess import subprocess
import tempfile import tempfile
@ -45,6 +46,8 @@ def start_llama_stack_server(config_name: str) -> subprocess.Popen:
stderr=subprocess.PIPE, # keep stderr to see errors stderr=subprocess.PIPE, # keep stderr to see errors
text=True, text=True,
env={**os.environ, "LLAMA_STACK_LOG_FILE": "server.log"}, 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 return process
@ -267,14 +270,17 @@ def cleanup_server_process(request):
print(f"Server process already terminated with return code: {server_process.returncode}") print(f"Server process already terminated with return code: {server_process.returncode}")
return return
try: 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) server_process.wait(timeout=10)
print("Server process terminated gracefully") print("Server process and children terminated gracefully")
except subprocess.TimeoutExpired: except subprocess.TimeoutExpired:
print("Server process did not terminate gracefully, killing it") 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() server_process.wait()
print("Server process killed") print("Server process and children killed")
except Exception as e: except Exception as e:
print(f"Error during server cleanup: {e}") print(f"Error during server cleanup: {e}")
else: else: