Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

Container: SIGKILL a server that is still alive KYOJIN_STOP_TIMEOUT seconds after SIGTERM

YanissAmza1ac3cbf6d26
diff --git a/doc/container.md b/doc/container.md
index 9fbc580..8f88eee 100644
--- a/doc/container.md
+++ b/doc/container.md
@@ -139,6 +139,7 @@ The container port must be the port llama-swap picked (`-p ${PORT}:${PORT}`), so
| `KYOJIN_DRAFT_TOKENS`, `KYOJIN_DRAFT_POLICY` | server default | Speculative decoding settings |
| `KYOJIN_ENABLE_VISION`, `_THINKING`, `_UNCENSOR` | on | `0` switches the feature off (Qwen; uncensor also GLM) |
| `KYOJIN_CACHE_DIR` | see "Keep the cache" | Folder for the kernel cache |
+| `KYOJIN_STOP_TIMEOUT` | `20` | Seconds after SIGTERM before a server that is still running gets SIGKILL (keep it below `docker stop -t`) |
| `KYOJIN_DOWNLOAD`, `KYOJIN_HF_REPO` | on, the model's repo | Pack download control |
| `KYOJIN_EXTRA_ARGS` | empty | Extra server arguments, shell-quoted |
| `EXL3_*` | unset | Engine variables pass through unchanged |
diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh
index bbb42fb..34625e2 100755
--- a/docker/entrypoint.sh
+++ b/docker/entrypoint.sh
@@ -44,7 +44,18 @@ export TRITON_CACHE_DIR=${TRITON_CACHE_DIR:-$CACHE_HOME/triton}
# so python never sees it (measured: docker stop took the full timeout, exit 137). The trap is set before the
# configuration step because that step can be a download of tens of GB.
CHILD=""
-stop() { echo "[kyojin] signal received, stopping" >&2; [ -n "$CHILD" ] && kill -TERM "$CHILD" 2>/dev/null; STOPPED=1; }
+# A server that is still alive KYOJIN_STOP_TIMEOUT seconds after SIGTERM (default 20; seen once: a server that held
+# its GPU memory for 42 minutes) gets SIGKILL, so that a stop always frees the GPU.
+stop() {
+ echo "[kyojin] signal received, stopping" >&2
+ [ "$STOPPED" = 1 ] && return
+ STOPPED=1
+ [ -n "$CHILD" ] || return
+ kill -TERM "$CHILD" 2>/dev/null
+ ( pid=$CHILD; n=${KYOJIN_STOP_TIMEOUT:-20}
+ while [ "$n" -gt 0 ] && kill -0 "$pid" 2>/dev/null; do sleep 1; n=$((n - 1)); done
+ kill -0 "$pid" 2>/dev/null && { echo "[kyojin] server still running after SIGTERM: SIGKILL" >&2; kill -KILL "$pid" 2>/dev/null; } ) &
+}
STOPPED=0
trap stop TERM INT
wait_child() { while kill -0 "$CHILD" 2>/dev/null; do wait "$CHILD" 2>/dev/null; done; wait "$CHILD" 2>/dev/null; return $?; }
diff --git a/docker/test_entrypoint.sh b/docker/test_entrypoint.sh
index a289e43..6ca0c2d 100644
--- a/docker/test_entrypoint.sh
+++ b/docker/test_entrypoint.sh
@@ -23,7 +23,10 @@ print("STUB args:", " ".join(a), flush=True)
print("STUB env: sourced=%s triton=%s" % (os.environ.get("KYOJIN_TEST_ENV_SOURCED"), os.environ.get("TRITON_CACHE_DIR")), flush=True)
if "--rc" in a:
sys.exit(int(a[a.index("--rc") + 1]))
-if "--hang" in a:
+if "--deaf" in a:
+ signal.signal(signal.SIGTERM, signal.SIG_IGN)
+ print("STUB deaf", flush=True)
+if "--hang" in a or "--deaf" in a:
time.sleep(60)
EOF
mkdir -p "$T/bin"; ln -s "$(command -v python3)" "$T/bin/python"
@@ -69,6 +72,14 @@ S=$(date +%s.%N); kill -TERM "$EP"; wait "$EP"; RC=$?; E=$(python3 -c "import ti
check "SIGTERM stops the server (exit 143) in under 3 s (took ${E}s)" "[ $RC = 143 ] && python3 -c 'import sys;sys.exit(0 if $E<3 else 1)'"
check "stop is logged" "grep -q 'signal received' $T/out.term"
+# 4b a server that ignores SIGTERM is killed after KYOJIN_STOP_TIMEOUT
+mkdir -p "$T/home.deaf"
+env -i PATH="$PATH" HOME="$T/home.deaf" KYOJIN_ROOT="$R" KYOJIN_MODEL=qwen MODEL_DIR="$T/models" KYOJIN_STOP_TIMEOUT=2 setsid bash "$R/docker/entrypoint.sh" --deaf >"$T/out.deaf" 2>&1 &
+EP=$!
+for i in $(seq 1 50); do grep -q 'STUB deaf' "$T/out.deaf" 2>/dev/null && break; sleep 0.2; done
+S=$(date +%s.%N); kill -TERM "$EP"; wait "$EP"; RC=$?; E=$(python3 -c "import time;print(round(time.time()-$S,1))")
+check "a server deaf to SIGTERM is killed after the timeout (exit 137, took ${E}s)" "[ $RC = 137 ] && python3 -c 'import sys;sys.exit(0 if 1.5<$E<6 else 1)' && grep -q 'SIGKILL' $T/out.deaf"
+
# 5 SIGTERM during the configuration step (a long download): stub the argument script with a sleeper
R2=$T/root2; cp -r "$R" "$R2"; printf 'import time\ntime.sleep(60)\n' >"$R2/docker/kyojin_args.py"
mkdir -p "$T/home.dl"