From 826ee9165d13b20851d491994b2cccedf6ca41e4 Mon Sep 17 00:00:00 2001 From: Josue Zamudio Date: Wed, 16 Sep 2026 00:56:48 +0000 Subject: [PATCH] run.sh: PID-file restart; pkill fallback scoped to :8321 so test servers on :8399 are never hit --- .gitignore | 1 + run.sh | 28 +++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 6ded754..b7d8fc2 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ __pycache__/ *.tar.gz server/data/ server.log +server.pid pi-session-*.html .pytest_cache/ diff --git a/run.sh b/run.sh index 9362c2f..d157f98 100755 --- a/run.sh +++ b/run.sh @@ -2,11 +2,29 @@ # launch (or restart) the ani-web server cd "$(dirname "$0")" || exit 1 [ -x .venv/bin/python ] || { echo "no .venv — run: uv venv .venv && uv pip install --python .venv/bin/python -r requirements.txt"; exit 1; } -pkill -f "uvicorn .*app:app" 2>/dev/null; sleep 1 -# graceful shutdown waits for in-flight /stream requests; force-kill anything -# still holding the port, or the old process outlives the restart -pkill -9 -f "uvicorn .*app:app" 2>/dev/null + +# stop the old instance via its PID file. Only when no pid file exists (first +# boot under this scheme) fall back to pkill — and then PORT-SCOPED: the old +# global "uvicorn .*app:app" pattern also matched the throwaway test server +# (server.app:app --host 127.0.0.1 --port 8399), so an ani-web restart killed +# running test sessions, and a stray test server kept port 8321's holder alive. +PIDF=server.pid +if [ -f "$PIDF" ]; then + oldpid=$(cat "$PIDF" 2>/dev/null) + if [ -n "$oldpid" ] && kill -0 "$oldpid" 2>/dev/null; then + kill "$oldpid" 2>/dev/null + i=0 + while kill -0 "$oldpid" 2>/dev/null && [ "$i" -lt 10 ]; do sleep 0.5; i=$((i + 1)); done + kill -9 "$oldpid" 2>/dev/null + fi + rm -f "$PIDF" +else + pkill -f "app:app --host 0.0.0.0 --port 8321" 2>/dev/null + sleep 1 +fi + # ANIWEB_SECURE=1: session cookie is Secure — only usable via the HTTPS # proxy (plain http://:8321 can no longer hold a session). ANIWEB_SECURE=1 nohup .venv/bin/python -m uvicorn server.app:app --host 0.0.0.0 --port 8321 --timeout-graceful-shutdown 3 > server.log 2>&1 & -echo "ani-web on http://localhost:8321 (pid $!) — use the HTTPS proxy to log in" +echo $! > "$PIDF" +echo "ani-web on http://localhost:8321 (pid $(cat "$PIDF")) — use the HTTPS proxy to log in"