From 19709c2c75f536fb9edf5b0a8e214609567ad887 Mon Sep 17 00:00:00 2001 From: Joshua Oosthuizen Date: Tue, 17 Nov 2026 15:48:28 +0200 Subject: [PATCH] frpc: restart it the first time the admin lines are added (a reload can't work yet) Found in the VM test: the new install's tunnels never opened, because frpc only reads admin_addr when it starts, so the first reload failed. That first time (and whenever a reload fails), frpc is restarted under systemd and the report says so; later changes reload as before. Co-Authored-By: Claude Opus 5.5 --- scripts/lib/frpc.sh | 22 +++++++++++++++++++++- scripts/tests/frpc.bats | 24 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/scripts/lib/frpc.sh b/scripts/lib/frpc.sh index 526dd65..beee8c6 100644 --- a/scripts/lib/frpc.sh +++ b/scripts/lib/frpc.sh @@ -94,6 +94,8 @@ frpc_add() { fi new="$(mktemp "$(dirname "$FRPC_INI")/.frpc.ini.XXXXXX")" with_admin_lines "$FRPC_INI" > "$new" + # The admin lines were added just now: the running frpc only reads them when it starts. + grep -q "^admin_addr" "$FRPC_INI" || FRPC_NEEDS_RESTART=1 # Keep the file's last line intact: only add a newline if it had none. [ -z "$(tail -c 1 "$new")" ] || echo >> "$new" { echo; frpc_block "$church" "$product" "$web" "$api"; } >> "$new" @@ -122,6 +124,24 @@ frpc_remove() { info "frpc: removed $church-$product." } +FRPC_NEEDS_RESTART=0 + +# frpc_reload: picks up the changed frpc.ini. Normally a reload (no other tunnel drops). The first +# time the admin lines were added, or if the reload fails, frpc is restarted instead: its other +# tunnels drop for a few seconds (found in the VM test: a reload can't work before the restart). frpc_reload() { - "$FRPC_BIN" reload -c "$FRPC_INI" || warn "frpc reload failed: restart frpc (systemctl restart frpc) to pick up the change." + if [ "$FRPC_NEEDS_RESTART" != 1 ] && "$FRPC_BIN" reload -c "$FRPC_INI" >/dev/null 2>&1; then + return 0 + fi + if command -v systemctl >/dev/null && systemctl list-unit-files frpc.service >/dev/null 2>&1 && systemctl cat frpc >/dev/null 2>&1; then + if [ "$FRPC_NEEDS_RESTART" = 1 ]; then + warn "frpc: restarting it once so it reads the admin lines (its other tunnels drop for a few seconds; later changes reload without this)." + else + warn "frpc: reload didn't work, so restarting it (its other tunnels drop for a few seconds)." + fi + report_result frpcRestarted true + systemctl restart frpc && FRPC_NEEDS_RESTART=0 && return 0 + fi + warn "frpc couldn't be reloaded or restarted: restart it by hand (systemctl restart frpc) to pick up the change." + return 1 } diff --git a/scripts/tests/frpc.bats b/scripts/tests/frpc.bats index ed3be36..6376a95 100644 --- a/scripts/tests/frpc.bats +++ b/scripts/tests/frpc.bats @@ -103,3 +103,27 @@ EOF frpc_add hope events 20010 20011 grep -qx 'server_addr = x' "$FRPC_INI" } + +@test "the first time (admin lines just added) frpc is restarted; afterwards it's reloaded" { + cat > "$TMP/bin/systemctl" <<'STUB' +#!/usr/bin/env bash +echo "systemctl $*" >> "$(dirname "$0")/../calls" +STUB + chmod +x "$TMP/bin/systemctl" + PATH="$TMP/bin:$PATH" + FRPC_NEEDS_RESTART=0 + frpc_add hope events 20010 20011 + [ "$FRPC_NEEDS_RESTART" = 1 ] + run frpc_reload + [ "$status" -eq 0 ] + grep -q "systemctl restart frpc" "$TMP/calls" + [[ "$output" == *"restarting it once"* ]] + # Admin lines now there: the next change reloads. + : > "$TMP/calls" + FRPC_NEEDS_RESTART=0 + frpc_add bridge events 20020 20021 + [ "$FRPC_NEEDS_RESTART" = 0 ] + frpc_reload + ! grep -q "restart frpc" "$TMP/calls" + grep -q reload "$TMP/frp/reloads" +}