From 03ce20156aa022520489717fc295928cdec41c53 Mon Sep 17 00:00:00 2001 From: Alexander Golovanov Date: Fri, 18 Sep 2026 14:05:23 +0300 Subject: [PATCH] [blockcheck2] Sweep leftover temp files from a killed/crashed run The pad file, PARALLEL_OUT, and HDRTEMP are all already removed on a normal exit - Ctrl+C, closing the terminal, a plain kill all clean up after themselves. kill -9 and crashes skip that entirely, same as for any script, and a later run has no way to know about a different PID's leftovers - so all three can pile up in /tmp indefinitely. Before each is used, the script now checks for existing files matching its pattern and removes any whose PID is confirmed no longer running. A file still owned by a live process - a second instance running at the same time - is left alone. --- blockcheck2.sh | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/blockcheck2.sh b/blockcheck2.sh index 6ddc21a..bd4f23f 100755 --- a/blockcheck2.sh +++ b/blockcheck2.sh @@ -65,6 +65,33 @@ QNUM=${QNUM:-$(($$ % 64536 + 1000))} PARALLEL_OUT=/tmp/zapret_parallel_$$ HDRTEMP=/tmp/zapret-hdr-$$ + +sweep_stale_pidfiles() +{ + # $1 - fixed filename prefix immediately preceding the pid + # + # A normal exit already removes this run's own PARALLEL_OUT/HDRTEMP + # files (unprepare_all(), further down), which covers Ctrl+C, + # a closed terminal, and a plain kill. kill -9 and crashes bypass + # that, as they would for any script's cleanup, and can leave + # stale files behind indefinitely, since nothing about a later run + # gives it any reason to know about a different PID's leftovers. + # Removes a matching file only once its PID (the run that created + # it) is confirmed no longer running, so a genuinely still-running + # concurrent instance - a different PID, its own files - is never + # touched. + local f pid prefix="$1" + for f in "$prefix"*; do + [ -e "$f" ] || continue + pid=${f#"$prefix"} + pid=${pid%%[!0-9]*} + [ -n "$pid" ] || continue + kill -0 "$pid" 2>/dev/null || rm -f "$f" + done +} +sweep_stale_pidfiles /tmp/zapret_parallel_ +sweep_stale_pidfiles /tmp/zapret-hdr- + NFT_TABLE=blockcheck$$ IPT_OUT_CHAIN=blockcheck_output_$$ IPT_IN_CHAIN=blockcheck_input_$$ @@ -85,8 +112,22 @@ unset ALL_PROXY apply_header_padfile() { - local n=1 left size + local n=1 left size f pid if [ "$CURL_PAD" -gt 0 ] 2>/dev/null; then + # A normal exit already removes this run's own pad file (see + # cleanup() below), which covers Ctrl+C, a closed terminal, and + # a plain kill. kill -9 and crashes bypass that, as they would + # for any script's cleanup, so sweep any such leftover from + # a previous run before making this run's own - only once its PID + # is confirmed no longer running, so a genuinely still-running + # concurrent instance (different PID, its own file) is never + # touched + for f in /tmp/zapret-curlpad-*; do + [ -e "$f" ] || continue + pid=${f##*-} + case "$pid" in ''|*[!0-9]*) continue ;; esac + kill -0 "$pid" 2>/dev/null || rm -f "$f" + done left=$CURL_PAD CURL_PAD_FILE=/tmp/zapret-curlpad-$$ rm -f "$CURL_PAD_FILE"