# -*- coding: utf-8 -*- """Stack-sample hung API + connection count; then SIGKILL restart and wait for listen.""" from __future__ import annotations import sys import time import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect("107.173.30.245", 4014, "eric", "4669805", timeout=40, allow_agent=False, look_for_keys=False) pw = "4669805" def sh(cmd, t=120): _, o, e = c.exec_command(f"echo '{pw}' | sudo -S -p '' bash -lc {repr(cmd)}", timeout=t) return (o.read() + e.read()).decode("utf-8", "replace") print("=== stop watch ===") print(sh("systemctl stop douyinweb-api-watch; true")) print("=== conn / recvq ===") print(sh("ss -ltn sport = :8014; ss -tn sport = :8014 | head -5; ss -tn sport = :8014 | wc -l")) print("=== try py-spy / pystack ===") print(sh("pid=$(systemctl show douyinweb-api -p MainPID --value); echo PID=$pid; which py-spy || true; ls /proc/$pid/task | wc -l; timeout 8 py-spy dump --pid $pid 2>/dev/null | head -120 || timeout 5 gdb -batch -p $pid -ex 'thread apply all bt' 2>/dev/null | head -80 || echo no_stack_tool; cat /proc/$pid/wchan; ls -l /proc/$pid/fd | wc -l")) print("=== who connects to 8014 top ===") print(sh("ss -tnp sport = :8014 | awk '{print $5}' | sed 's/:[0-9]*$//' | sort | uniq -c | sort -rn | head -20")) print("=== restart clean ===") print(sh( "systemctl kill -s SIGKILL douyinweb-api; pkill -9 -f 'uvicorn app.main:app' || true; " "sleep 2; systemctl reset-failed douyinweb-api; systemctl start douyinweb-api" )) for i in range(36): time.sleep(5) code = sh("curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health || echo 000").strip() rq = sh("ss -ltn sport = :8014 | awk 'NR==2{print $2}'").strip() print(f"[{i}] health={code} recvq={rq}", flush=True) if code.endswith("200"): break print("=== login after healthy ===") print(sh(r'''python3 - <<'PY' import json,urllib.request req=urllib.request.Request("http://127.0.0.1:8014/api/auth/login",data=json.dumps({"username":"hackrobot","password":"Xiao4669805@"}).encode(),headers={"Content-Type":"application/json"},method="POST") with urllib.request.urlopen(req,timeout=15) as r: print("login",r.status,r.read()[:80], (r.headers.get("Set-Cookie") or "")[:120]) PY''')) print("=== leave watch STOPPED ===") print(sh("systemctl is-active douyinweb-api douyinweb-api-watch; ss -ltn sport = :8014")) c.close()