diff --git a/rootfs/overlay/opt/mu300/bin/mu300-update b/rootfs/overlay/opt/mu300/bin/mu300-update index c339508f..3aa824b7 100755 --- a/rootfs/overlay/opt/mu300/bin/mu300-update +++ b/rootfs/overlay/opt/mu300/bin/mu300-update @@ -30,9 +30,20 @@ have() { command -v "$1" >/dev/null 2>&1; } # A transfer that stalls is cut off and retried rather than waited for: with older boot images the IPA's downlink # can die in the middle of one (docs/FINDINGS.md 32), and without a timeout the update hung for good. +# A network error is tried again a few times (MU300_FETCH_DELAY s apart): right after boot the VPN comes up some +# seconds after mobile data, and a request in between is reset - "check" then said GitHub could not be reached. +# curl's own --retry leaves resets alone. An HTTP error (curl 22, a 404) is an answer and is not retried. fetch_stdout() { # fetch_stdout URL - if have curl; then curl -fsSL --connect-timeout 30 --max-time 120 --retry 3 "$1" - else wget -T 60 -qO- "$1"; fi + _try=1 + while :; do + if have curl; then _out=$(curl -fsSL --connect-timeout 30 --max-time 120 --retry 3 "$1"); _rc=$? + else _out=$(wget -T 60 -qO- "$1"); _rc=$?; fi + [ "$_rc" = 0 ] && { printf '%s\n' "$_out"; return 0; } + [ "$_rc" = 22 ] && have curl && return 1 + [ "$_try" -ge 4 ] && return 1 + _try=$((_try + 1)) + sleep "${MU300_FETCH_DELAY:-4}" + done } fetch_file() { # fetch_file URL OUT (resumes a partial OUT) if have curl; then curl -fL --retry 5 --retry-delay 5 --connect-timeout 30 --speed-limit 1024 --speed-time 60 -C - -o "$2" "$1" diff --git a/tests/test_update.py b/tests/test_update.py index 523d5a7c..41cca3ea 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -30,6 +30,22 @@ class Update(ShellTest): r = self.up(shell, 'echo loaded') self.assertEqual((r.returncode, r.stdout), (0, 'loaded\n'), r.stderr) + def test_fetch_retries_a_network_error(self): + # right after boot the VPN is up some seconds after mobile data, and a request in between is reset + # (curl 35/56): "mu300-update check" then said "could not reach GitHub". Network errors are retried a few + # times; an HTTP error (22: 404 and the like) is an answer and is not. + for shell in self.each_shell(): + for rc, fails, want_out, want_calls in ((56, 2, 'body', 3), (35, 1, 'body', 2), (22, 1, '', 1), + (7, 9, '', 4)): + n = self.tmp / 'n' + n.write_text('0') + self.stub('curl', f'c=$(($(cat "$STUBLOG/n") + 1)); echo $c > "$STUBLOG/n"; ' + f'[ $c -gt {fails} ] && {{ echo body; exit 0; }}; exit {rc}') + r = self.up(shell, 'fetch_stdout https://example.invalid/x; echo "rc=$?"', MU300_FETCH_DELAY=0) + self.assertEqual(int(n.read_text()), want_calls, (rc, fails)) + self.assertEqual(r.stdout.replace('rc=0\n', '').replace('rc=1\n', '').strip(), want_out, (rc, r.stdout)) + self.assertIn('rc=0' if want_out else 'rc=1', r.stdout) + def test_rootfs_asset(self): osr = self.disk / 'ubuntu' / 'etc' / 'os-release' cases = [('24.04', {}, 'mu300-ubuntu-rootfs.tar.gz'),