From 04cf74ccd34ab8833d570e977549cff6402ff1ad Mon Sep 17 00:00:00 2001 From: Mathieu Moran Date: Sun, 9 Aug 2026 15:44:19 -0400 Subject: [PATCH] Fix flaky hotspot->home WiFi reconnect in netwatch Real-hardware finding: netwatch repeatedly reported 'home WiFi not found' 4 times over 13min while sitting right next to the router. Root cause: a plain 'nmcli con up home-wifi' straight out of AP mode relies on NM's scan cache for wlan0, which is stale/empty coming out of AP mode (the radio can't scan while acting as an AP). A single con-up attempt didn't wait for a fresh scan to land before giving up. Fix: explicitly drop the AP connection first to free the radio, force a fresh scan with a settle delay, and retry the connect attempt (longer 30s timeout) up to 2 times before conceding and restoring the hotspot. Bumped the systemd unit's TimeoutStartSec to 180s to give the retry loop enough headroom (was defaulting to 90s). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- deploy/yaris-netwatch.service | 3 +++ deploy/yaris-netwatch.sh | 27 +++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/deploy/yaris-netwatch.service b/deploy/yaris-netwatch.service index 65cb664..eb96acf 100644 --- a/deploy/yaris-netwatch.service +++ b/deploy/yaris-netwatch.service @@ -5,4 +5,7 @@ Wants=NetworkManager.service [Service] Type=oneshot +# Retry loop (down AP, rescan, retry connect up to 2x) can take ~80s worst +# case - default 90s systemd start timeout was too tight a margin. +TimeoutStartSec=180 ExecStart=/usr/local/bin/yaris-netwatch.sh diff --git a/deploy/yaris-netwatch.sh b/deploy/yaris-netwatch.sh index 9eb3ca8..a068d99 100755 --- a/deploy/yaris-netwatch.sh +++ b/deploy/yaris-netwatch.sh @@ -39,9 +39,32 @@ fi logger -t "$LOG_TAG" "wlan0 is on hotspot, checking for home WiFi ($HOME_CON)..." -if nmcli --wait 20 con up "$HOME_CON" >"$LAST_LOG" 2>&1; then +# Real-hardware finding: a plain "nmcli con up home-wifi" straight out of AP +# mode reliably reports "not found" even when actually in range, repeatedly +# (confirmed: 4 failures in a row over 13min while sitting right next to the +# router). NM's scan cache for wlan0 is stale/empty coming out of AP mode +# (the radio literally can't scan while acting as an AP - see comment above), +# and a single con-up attempt doesn't wait for a fresh scan to land before +# giving up. Fix: explicitly drop the AP connection first to free the radio, +# force a fresh scan and give it a few seconds to populate, then retry the +# connect attempt with a longer timeout up to 2 times before conceding and +# restoring the hotspot. +nmcli con down "$AP_CON" >"$LAST_LOG" 2>&1 + +connected=false +for attempt in 1 2; do + nmcli device wifi rescan ifname "$IFACE" >>"$LAST_LOG" 2>&1 + sleep 5 + if nmcli --wait 30 con up "$HOME_CON" >>"$LAST_LOG" 2>&1; then + connected=true + break + fi + logger -t "$LOG_TAG" "home WiFi attempt $attempt failed, retrying..." +done + +if [ "$connected" = true ]; then logger -t "$LOG_TAG" "home WiFi found, switched to '$HOME_CON'" else - logger -t "$LOG_TAG" "home WiFi not found, restoring hotspot" + logger -t "$LOG_TAG" "home WiFi not found after retries, restoring hotspot" nmcli con up "$AP_CON" >>"$LAST_LOG" 2>&1 fi