From 8e3c00ee7edd5db70e87120846a0dbc0fe69d2fb Mon Sep 17 00:00:00 2001 From: David Montero Date: Thu, 16 Apr 2026 05:52:52 +0200 Subject: [PATCH] fix: exit container when backend or nginx dies so Docker can restart it Previously uvicorn was launched with `&` while nginx ran with `exec`, so if the backend crashed (e.g. the `AssertionError` in websockets' keepalive_ping that took prod down for ~31h), nginx kept the container "up" and every /api/* request returned 502. Now both processes are monitored with `wait -n`; whichever dies first triggers the entrypoint to exit, letting `restart: unless-stopped` recover the container cleanly. Co-Authored-By: Claude Opus 4.6 (1M context) --- deploy/entrypoint.sh | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/deploy/entrypoint.sh b/deploy/entrypoint.sh index db82836e..ca155342 100644 --- a/deploy/entrypoint.sh +++ b/deploy/entrypoint.sh @@ -39,10 +39,21 @@ fi # Start FastAPI backend in the background on port 8001 echo "🚀 Starting Velxio Backend..." uvicorn app.main:app --host 127.0.0.1 --port 8001 & +UVICORN_PID=$! # Wait for backend to be healthy (optional but good practice) sleep 2 -# Start Nginx in the foreground to keep the container running +# Start Nginx in the background so we can monitor both processes echo "🌐 Starting Nginx Web Server on port 80..." -exec nginx -g "daemon off;" +nginx -g "daemon off;" & +NGINX_PID=$! + +# If either process dies, exit so Docker's restart policy can recover the +# container. Previously uvicorn could crash silently while nginx kept the +# container "up", leaving every /api/* request returning 502 Bad Gateway. +wait -n "$UVICORN_PID" "$NGINX_PID" +EXIT_CODE=$? +echo "⚠️ A core process exited (code=$EXIT_CODE). Shutting down container so Docker can restart it." +kill "$UVICORN_PID" "$NGINX_PID" 2>/dev/null || true +exit "$EXIT_CODE"