feat(runtime): bound recovery and add safe release supervision
Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
1 parent
780ca71e17
commit
b1e458a0a5
16 files changed
+941
-66
No files matched your search
@@ -1,7 +1,7 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
umask 077
|
||||
[ "$#" -eq 0 ] || { echo 'error: target entrypoint accepts no arguments' >&2; exit 1; }
|
||||
[ "$#" -eq 0 ] || { echo 'error: target entrypoint accepts no arguments' >&2; exit 64; }
|
||||
|
||||
# Credentials never enter argv or logs. Docker supplies SS_PASSWORD via env;
|
||||
# clear the exported value before starting any child processes.
|
||||
@@ -9,12 +9,12 @@ ss_password=${SS_PASSWORD-}
|
||||
unset SS_PASSWORD
|
||||
case "$ss_password" in
|
||||
''|*[!0123456789abcdefABCDEF]*)
|
||||
echo 'error: target secret must contain only hexadecimal characters' >&2; exit 1 ;;
|
||||
echo 'error: target secret must contain only hexadecimal characters' >&2; exit 64 ;;
|
||||
esac
|
||||
[ "${#ss_password}" -ge 32 ] || { echo 'error: target secret must be at least 32 hex characters' >&2; exit 1; }
|
||||
[ "${#ss_password}" -ge 32 ] || { echo 'error: target secret must be at least 32 hex characters' >&2; exit 64; }
|
||||
config_dir=/run/zapret-autotune-ss
|
||||
awk '$2 == "/run/zapret-autotune-ss" && $3 == "tmpfs" { found=1 } END { exit !found }' /proc/mounts || {
|
||||
echo 'error: target Shadowsocks config directory must be a dedicated tmpfs' >&2; exit 1;
|
||||
echo 'error: target Shadowsocks config directory must be a dedicated tmpfs' >&2; exit 78;
|
||||
}
|
||||
chmod 0700 "$config_dir"
|
||||
cat > "$config_dir/server.json" <<EOF
|
||||
@@ -30,23 +30,49 @@ server_pid=
|
||||
local_pid=
|
||||
monitor_pid=
|
||||
zapret_started=0
|
||||
# Wait with a watchdog: never let an unresponsive child exceed Docker's grace.
|
||||
bounded_wait() {
|
||||
child=$1
|
||||
seconds=$2
|
||||
(
|
||||
sleep "$seconds" & sleeper=$!
|
||||
trap 'kill "$sleeper" 2>/dev/null || true; exit 0' TERM INT
|
||||
wait "$sleeper"
|
||||
kill -KILL "$child" 2>/dev/null || true
|
||||
) </dev/null >/dev/null 2>&1 &
|
||||
watchdog=$!
|
||||
waited_code=0
|
||||
wait "$child" 2>/dev/null || waited_code=$?
|
||||
kill -TERM "$watchdog" 2>/dev/null || true
|
||||
wait "$watchdog" 2>/dev/null || true
|
||||
}
|
||||
cleanup() {
|
||||
final_code=$?
|
||||
case "$final_code" in 0|64|75|78) ;; *) final_code=78 ;; esac
|
||||
trap - EXIT INT TERM
|
||||
# Let the monitor cancel/join scans and restore committed bytes BEFORE stop.
|
||||
if [ -n "$monitor_pid" ]; then
|
||||
kill -TERM "$monitor_pid" 2>/dev/null || true
|
||||
wait "$monitor_pid" 2>/dev/null || true
|
||||
bounded_wait "$monitor_pid" 60
|
||||
# Cancellation must restore safely even when SS failure requested retry.
|
||||
case "$waited_code" in 0|143) ;; *) final_code=78 ;; esac
|
||||
fi
|
||||
# Dedicated PID/network namespace: no host/other-target stop.
|
||||
if [ "$zapret_started" -eq 1 ]; then
|
||||
/opt/zapret2/init.d/sysv/zapret2 stop || true
|
||||
timeout -k 5 30 /opt/zapret2/init.d/sysv/zapret2 stop || final_code=78
|
||||
fi
|
||||
for pid in "$local_pid" "$server_pid"; do
|
||||
[ -z "$pid" ] || kill "$pid" 2>/dev/null || true
|
||||
done
|
||||
for pid in "$local_pid" "$server_pid"; do
|
||||
[ -z "$pid" ] || wait "$pid" 2>/dev/null || true
|
||||
if [ -n "$pid" ]; then
|
||||
bounded_wait "$pid" 5
|
||||
# Its original failure triggered supervision; only forced shutdown
|
||||
# is a cleanup failure, not the already-dead Shadowsocks status.
|
||||
[ "$waited_code" -ne 137 ] || final_code=78
|
||||
fi
|
||||
done
|
||||
exit "$final_code"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
trap 'exit 0' INT TERM
|
||||
@@ -60,9 +86,9 @@ local_pid=$!
|
||||
# Bounded readiness, no network probe. Init/status verifies socket ownership.
|
||||
attempt=0
|
||||
until awk '$2 == "00000000:0438" && $4 == "0A" { found=1 } END { exit !found }' /proc/net/tcp; do
|
||||
kill -0 "$server_pid" && kill -0 "$local_pid"
|
||||
{ kill -0 "$server_pid" && kill -0 "$local_pid"; } || exit 75
|
||||
attempt=$((attempt + 1))
|
||||
[ "$attempt" -lt 30 ] || { echo 'error: target SOCKS listener timeout' >&2; exit 1; }
|
||||
[ "$attempt" -lt 30 ] || { echo 'error: target SOCKS listener timeout' >&2; exit 75; }
|
||||
sleep 1
|
||||
done
|
||||
if [ ! -e "$ZAPRET_CONFIG" ]; then
|
||||
@@ -84,8 +110,12 @@ if ! kill -0 "$monitor_pid" 2>/dev/null; then
|
||||
wait "$monitor_pid" || code=$?
|
||||
monitor_pid=
|
||||
echo 'error: target managed monitor exited' >&2
|
||||
[ "$code" -ne 0 ] || code=1
|
||||
case "$code" in
|
||||
0|2|137|139) code=75 ;;
|
||||
64|75|78) ;;
|
||||
*) code=78 ;;
|
||||
esac
|
||||
exit "$code"
|
||||
fi
|
||||
echo 'error: target child process exited' >&2
|
||||
exit 1
|
||||
exit 75
|
||||
Reference in new issue
Block a user