From 0b2ff523ac98d4b332c4124590fa7b348bdb6672 Mon Sep 17 00:00:00 2001 From: Anik Bhattacharjee Date: Tue, 26 May 2026 15:26:06 -0400 Subject: [PATCH] LCORE-1869: Add graceful teardown and automatic cleanup for llama-stack container **Key Changes:** - Auto-stop container when exiting `make run` (Ctrl+C) via trap handler - Graceful shutdown with 10s timeout before force-kill - Preserve logs to `/tmp/llama-stack-*.log` on shutdown/removal **Developer Experience:** - No manual cleanup needed - just Ctrl+C - Logs preserved after container removal - Clearer feedback during shutdown Signed-off-by: Anik Bhattacharjee --- Makefile | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 13719e7fb..d47fcf07d 100644 --- a/Makefile +++ b/Makefile @@ -17,10 +17,11 @@ LLAMA_STACK_IMAGE ?= lightspeed-llama-stack:local LLAMA_STACK_PORT ?= 8321 CONTAINER_RUNTIME ?= $(shell command -v podman 2>/dev/null || command -v docker 2>/dev/null) -.PHONY: run build-llama-stack-image remove-llama-stack-container start-llama-stack-container wait-for-llama-stack-health clean-llama-stack +.PHONY: run build-llama-stack-image remove-llama-stack-container stop-llama-stack-container start-llama-stack-container wait-for-llama-stack-health clean-llama-stack run: start-llama-stack-container ## Run the service locally with llama-stack container @echo "Starting Lightspeed Core Stack..." + @trap 'echo ""; echo "Stopping services..."; $(MAKE) stop-llama-stack-container' EXIT INT TERM; \ uv run src/lightspeed_stack.py -c $(CONFIG) build-llama-stack-image: remove-llama-stack-container ## Build llama-stack container image @@ -31,10 +32,26 @@ build-llama-stack-image: remove-llama-stack-container ## Build llama-stack conta fi $(CONTAINER_RUNTIME) build -f deploy/llama-stack/test.containerfile -t $(LLAMA_STACK_IMAGE) . -remove-llama-stack-container: ## Remove existing llama-stack container +stop-llama-stack-container: ## Gracefully stop llama-stack container @if [ -n "$(CONTAINER_RUNTIME)" ] && $(CONTAINER_RUNTIME) inspect $(LLAMA_STACK_CONTAINER_NAME) >/dev/null 2>&1; then \ - echo "Removing existing llama-stack container..."; \ + echo "Stopping llama-stack container (timeout: 10s)..."; \ + if $(CONTAINER_RUNTIME) stop -t 10 $(LLAMA_STACK_CONTAINER_NAME) 2>/dev/null; then \ + echo "✓ Container stopped gracefully"; \ + else \ + echo "⚠ Container did not stop gracefully, capturing logs..."; \ + $(CONTAINER_RUNTIME) logs --tail 100 $(LLAMA_STACK_CONTAINER_NAME) > /tmp/llama-stack-failure.log 2>&1 || true; \ + echo "Logs saved to /tmp/llama-stack-failure.log"; \ + $(CONTAINER_RUNTIME) kill $(LLAMA_STACK_CONTAINER_NAME) 2>/dev/null || true; \ + fi; \ + fi + +remove-llama-stack-container: ## Remove llama-stack container (saves logs first) + @if [ -n "$(CONTAINER_RUNTIME)" ] && $(CONTAINER_RUNTIME) inspect $(LLAMA_STACK_CONTAINER_NAME) >/dev/null 2>&1; then \ + echo "Saving container logs before removal..."; \ + $(CONTAINER_RUNTIME) logs $(LLAMA_STACK_CONTAINER_NAME) > /tmp/llama-stack-last-run.log 2>&1 || true; \ + echo "Removing llama-stack container..."; \ $(CONTAINER_RUNTIME) rm -f $(LLAMA_STACK_CONTAINER_NAME); \ + echo "✓ Container removed (logs saved to /tmp/llama-stack-last-run.log)"; \ fi start-llama-stack-container: build-llama-stack-image ## Start llama-stack container