From 28b3e21dee7e0a04104215f1826dfa7cc8732cfd Mon Sep 17 00:00:00 2001 From: Michael Uray Date: Sat, 18 Jul 2026 21:46:33 +0000 Subject: [PATCH] [fix] Retry registration on transient controller 5xx errors During registration the agent called check_header() before checking the HTTP status. A transient server-side error (HTTP 5xx, e.g. 502/503/504 from a reverse proxy while the backend is restarting) carries no X-Openwisp-Controller header, so check_header treated it as a wrong server and called `exit 4`. Combined with procd's respawn limit (respawn ... 5), a short controller outage during (re)registration made procd give up permanently, leaving the agent dead until a manual restart. The other three check_header call sites (get_checksum, update_info, status report) already guard with an `is_http_status ... 200` check before calling check_header, so a 5xx there returns a retryable code. Only the registration path was missing this guard. Treat a 5xx registration response as a temporary failure (return 2, retryable) before check_header runs. Non-5xx responses still go through check_header, preserving the "wrong server" safety (exit 4) for a 2xx response without the controller header. Signed-off-by: Michael Uray --- openwisp-config/files/openwisp.agent | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/openwisp-config/files/openwisp.agent b/openwisp-config/files/openwisp.agent index 01a4ab9d..983a0479 100755 --- a/openwisp-config/files/openwisp.agent +++ b/openwisp-config/files/openwisp.agent @@ -348,6 +348,17 @@ register() { -p daemon.err return 1 fi + # A transient server-side error (HTTP 5xx, e.g. 502/503/504 from a reverse + # proxy while the backend is restarting) carries no X-Openwisp-Controller + # header. Without this guard check_header below would treat it as a wrong + # server and call `exit 4`, killing the agent for good once procd's respawn + # limit is exhausted. Treat 5xx as a temporary failure and retry instead. + if head -n 1 "$REGISTRATION_PARAMETERS" | tr -d '\r' | grep -qE "^HTTP/[0-9.]+ 5[0-9][0-9]( |$)"; then + logger -s "Registration temporarily failed (controller returned 5xx), will retry: $(head -n 1 "$REGISTRATION_PARAMETERS" | tr -d '\r')" \ + -t openwisp \ + -p daemon.warning + return 2 + fi # exit if response does not seem to come from openwisp controller check_header $REGISTRATION_PARAMETERS if ! is_http_status $REGISTRATION_PARAMETERS 201; then