From 50e87c86cb0cad75fb427029846591fe004f65fd Mon Sep 17 00:00:00 2001 From: Oleksander Piskun Date: Mon, 20 Oct 2025 10:19:17 +0300 Subject: [PATCH 1/2] fix(race-condition): use cycle with bigger interval to check FRP Server availability Signed-off-by: Oleksander Piskun --- start.sh | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/start.sh b/start.sh index 3bd5b43..3e1ea05 100644 --- a/start.sh +++ b/start.sh @@ -24,6 +24,9 @@ set -e # We do not generate /certs/cert.pem file, as for HaProxy it is admin task to mount generated cert if needed. # ---------------------------------------------------------------------------- +HP_WAIT_FRP=${HP_WAIT_FRP:-30} +HP_WAIT_INTERVAL=${HP_WAIT_INTERVAL:-0.5} + HP_VERBOSE_START=${HP_VERBOSE_START:-1} log() { if [ "$HP_VERBOSE_START" -eq 1 ]; then @@ -31,6 +34,23 @@ log() { fi } +wait_for_tcp() { + # $1 host, $2 port, $3 timeout(s), $4 interval(s) + host="$1"; port="$2"; timeout="${3:-30}"; interval="${4:-0.5}" + start_ts="$(date +%s)" + while :; do + if nc -z -w 1 "$host" "$port" >/dev/null 2>&1; then + return 0 + fi + now="$(date +%s)"; elapsed=$(( now - start_ts )) + if [ "$elapsed" -ge "$timeout" ]; then + echo "ERROR: Timeout waiting for TCP $host:$port to become ready (after ${timeout}s)." + return 1 + fi + sleep "$interval" + done +} + # Check if the required environment variables are set if [ -z "$HP_FRP_ADDRESS" ]; then echo "ERROR: HP_FRP_ADDRESS is not set." @@ -311,7 +331,11 @@ sleep 1s log "INFO: Starting FRP server on ${HP_FRP_ADDRESS}..." frps -c /frps.toml & -sleep 1s +# Wait for FRP port to be listening before starting frpc +LOCAL_FRP_HOST="$FRP_HOST" +[ "$LOCAL_FRP_HOST" = "0.0.0.0" ] && LOCAL_FRP_HOST="127.0.0.1" +log "INFO: Waiting for FRP server port ${LOCAL_FRP_HOST}:${FRP_PORT}..." +wait_for_tcp "$LOCAL_FRP_HOST" "$FRP_PORT" "$HP_WAIT_FRP" "$HP_WAIT_INTERVAL" if [ -e "/var/run/docker.sock" ]; then log "INFO: Starting FRP client for Docker Engine..." From 92f37f8514c777e50a27e6f10420fad35e2a7964 Mon Sep 17 00:00:00 2001 From: Oleksander Piskun Date: Mon, 20 Oct 2025 13:12:08 +0300 Subject: [PATCH 2/2] fix: deterministically wait for Python agent to start Signed-off-by: Oleksander Piskun --- start.sh | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/start.sh b/start.sh index 3e1ea05..a682b7c 100644 --- a/start.sh +++ b/start.sh @@ -24,6 +24,8 @@ set -e # We do not generate /certs/cert.pem file, as for HaProxy it is admin task to mount generated cert if needed. # ---------------------------------------------------------------------------- +HP_WAIT_AGENT_HTTP=${HP_WAIT_AGENT_HTTP:-60} +HP_WAIT_SPOA=${HP_WAIT_SPOA:-60} HP_WAIT_FRP=${HP_WAIT_FRP:-30} HP_WAIT_INTERVAL=${HP_WAIT_INTERVAL:-0.5} @@ -51,6 +53,23 @@ wait_for_tcp() { done } +wait_for_http() { + # $1 url, $2 timeout(s), $3 interval(s) + url="$1"; timeout="${2:-60}"; interval="${3:-0.5}" + start_ts="$(date +%s)" + while :; do + if curl -fsS --max-time 2 "$url" >/dev/null 2>&1; then + return 0 + fi + now="$(date +%s)"; elapsed=$(( now - start_ts )) + if [ "$elapsed" -ge "$timeout" ]; then + echo "ERROR: Timeout waiting for HTTP $url to become ready (after ${timeout}s)." + return 1 + fi + sleep "$interval" + done +} + # Check if the required environment variables are set if [ -z "$HP_FRP_ADDRESS" ]; then echo "ERROR: HP_FRP_ADDRESS is not set." @@ -326,7 +345,12 @@ fi log "INFO: Starting Python HaProxy Agent on 127.0.0.1:8200 and 127.0.0.1:9600..." nohup python3 /usr/local/bin/haproxy_agent.py & -sleep 1s +# Wait deterministically for the agent to be ready (HTTP) and for SPOA (TCP) +log "INFO: Waiting for HaRP Agent HTTP (GET http://127.0.0.1:8200/info) to be ready..." +wait_for_http "http://127.0.0.1:8200/info" "$HP_WAIT_AGENT_HTTP" "$HP_WAIT_INTERVAL" + +log "INFO: Waiting for SPOA port 127.0.0.1:9600..." +wait_for_tcp "127.0.0.1" "9600" "$HP_WAIT_SPOA" "$HP_WAIT_INTERVAL" log "INFO: Starting FRP server on ${HP_FRP_ADDRESS}..." frps -c /frps.toml &