Skip to content

Make Supervisor startup resilient to transient network failures - #4874

Merged
agners merged 4 commits into
devfrom
supervisor-startup-resilience
Jul 7, 2026
Merged

agners merged 4 commits into
devfrom
supervisor-startup-resilience

Conversation

@agners

@agners agners commented Jul 6, 2026 •

Copy link
Copy Markdown
Member

Summary

Issue #4863 exposed two independent first-boot fragilities that turn a transient network failure into a permanent one. Both sit on the recovery path after a factory reset / data wipe, where the pre-seeded data partition content is gone and the OS must fetch things from the network on first boot.

1. Supervisor version fetch (permanent until reboot)

When the Supervisor image is missing and no local updater information exists, haos-supervisor fetches the current stable version from version.home-assistant.io in a single attempt with no error handling: if it fails, jq -e exits with code 4 under set -e and the script dies within ~400ms.

2. AppArmor profile download (permanent across reboots)

haos-apparmor only downloads the Supervisor profile when the profile directory is missing โ€” but it creates the directory before attempting the download. One failed first download (no DNS on first boot after wipe) poisons the state: every subsequent boot skips the download because the directory exists, the unit exits successfully having loaded nothing, and the Supervisor container fails to start forever with apparmor failed to apply profile: write .../attr/apparmor/exec: no such file or directory โ€” exactly what the #4863 reporter hit after fixing their DNS.

Changes

  • haos-supervisor: let curl retry the version fetch itself: --retry 5 --retry-delay 10 --retry-all-errors --max-time 30. --retry-all-errors is needed so DNS and TLS failures (the unsynced-clock case) are retried too, not just transient HTTP errors.
  • haos-supervisor.service: increase the start rate limit from 3 to 5 attempts per 30 minutes. The rate limit stays in place to avoid hammering the update infrastructure, but a slightly longer network/time-sync hiccup at boot no longer permanently disables the unit.
  • haos-supervisor: guard the docker image rm in the startup-marker recovery path against an empty image list, which currently logs a spurious docker: 'docker image rm' requires at least 1 argument usage error.
  • haos-apparmor: key the profile download on the profile file instead of the directory (self-healing on the next boot), download to a temporary file moved into place only on success, add -f and the same curl retry flags, and fail the unit loudly when the download does not succeed.

Testing

  • sh -n and shellcheck pass on both scripts.
  • Verified the curl flags retry on DNS resolution errors (exit 6 only after the configured retries elapse).
  • Simulated the poisoned AppArmor state (directory exists, profile missing, download failing then succeeding): the failed attempt now exits 1 without leaving stray files, and the next run re-downloads and installs the profile despite the pre-existing directory.

๐Ÿค– Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved Supervisor startup reliability when a previous start was interrupted.
    • Prevented unnecessary cleanup errors by only removing old Supervisor images when applicable.
    • Made stable-channel Supervisor version retrieval more resilient via retries with bounded time.
    • Improved AppArmor supervisor profile download reliability with fail-fast retries and atomic updates.
    • Increased the Supervisor serviceโ€™s restart tolerance to reduce failures from repeated starts.

@coderabbitai

coderabbitai Bot commented Jul 6, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. ๐ŸŽ‰

โ„น๏ธ Recent review info
โš™๏ธ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 23c5d144-29b4-4f9f-8b9a-03641a104696

๐Ÿ“ฅ Commits

Reviewing files that changed from the base of the PR and between c1e1d13 and 368be77.

๐Ÿ“’ Files selected for processing (1)
  • buildroot-external/rootfs-overlay/usr/sbin/haos-supervisor
๐Ÿšง Files skipped from review as they are similar to previous changes (1)
  • buildroot-external/rootfs-overlay/usr/sbin/haos-supervisor

๐Ÿ“ Walkthrough

Walkthrough

This PR updates supervisor startup cleanup and version fetching, makes AppArmor profile download atomic, and increases the supervisor unit restart burst limit.

Changes

Supervisor hardening

Layer / File(s) Summary
Startup flow
buildroot-external/rootfs-overlay/usr/sbin/haos-supervisor
Startup now skips supervisor image removal when no image IDs are present, and the stable-channel version fetch uses retrying fail-fast curl before jq extraction.
AppArmor download
buildroot-external/rootfs-overlay/usr/libexec/haos-apparmor
The supervisor profile check targets hassio-supervisor, downloads to a temporary file with retrying curl, removes the temp file on failure, and moves it into place only after success.
Systemd restart limit
buildroot-external/rootfs-overlay/usr/lib/systemd/system/haos-supervisor.service
StartLimitBurst changes from 3 to 5.

Estimated code review effort: 2 (Simple) | ~10 minutes

Poem

I hop through starts with a steadier pace,
And keep clean paws in the empty-case.
My curls retry, my profile lands true,
Then I tuck it in place, nice and new.
Thump! The supervisor feels spry. ๐Ÿ‡

๐Ÿšฅ Pre-merge checks | โœ… 5
โœ… Passed checks (5 passed)
Check name Status Explanation
Description Check โœ… Passed Check skipped - CodeRabbitโ€™s high-level summary is enabled.
Title check โœ… Passed The title accurately summarizes the main change: making Supervisor startup more resilient to transient network failures.
Docstring Coverage โœ… Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check โœ… Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check โœ… Passed Check skipped because no linked issues were found for this pull request.
โœจ Finishing Touches
๐Ÿงช Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch supervisor-startup-resilience

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

โค๏ธ Share

Comment @coderabbitai help to get the list of available commands.

When the Supervisor image is missing and no local updater information is
available (e.g. on first boot after a factory reset/data wipe), the start
script fetches the current stable version from version.home-assistant.io.
This fetch can fail transiently: since #3669 the boot waits at most 15s
for time synchronization, so the script may run with an unsynced clock
(making TLS validation fail), or before the network is fully functional.

Today such a failure makes the script exit within ~400ms (via jq -e exit
code 4 under set -e). Combined with RestartSec=5s and StartLimitBurst=3,
three quick failures trip the start rate limit within ~15 seconds and the
unit stays failed until reboot - even if NTP syncs moments later, leaving
the device at the emergency console (as seen in #4863).

Make this path more robust:
- Let curl retry the version fetch (5 retries, 10s apart). Use
  --retry-all-errors so DNS/TLS failures are retried as well, not just
  transient HTTP errors.
- Increase the service start rate limit from 3 to 5 attempts per 30
  minutes, so a slightly longer network/time-sync hiccup at boot doesn't
  permanently disable the unit.
- Skip "docker image rm" in the startup-marker recovery path when there
  are no Supervisor images at all, avoiding a spurious usage error.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@agners
agners force-pushed the supervisor-startup-resilience branch from d0145ce to 2be565b Compare July 6, 2026 17:23
@agners
agners marked this pull request as ready for review July 7, 2026 12:32
The haos-apparmor script only downloads the Supervisor AppArmor profile
when the profile directory is missing - but it creates the directory
before attempting the download. If the first download fails (e.g. no
DNS/network on first boot after a data wipe), every subsequent boot
skips the download because the directory exists, and the unit exits
successfully without loading any profile. The Supervisor container is
then created with --security-opt apparmor=hassio-supervisor and fails
to start forever with:

  apparmor failed to apply profile: write .../attr/apparmor/exec:
  no such file or directory

(observed in #4863 after the reporter fixed their DNS setup).

Key the download on the profile file instead of the directory, download
to a temporary file that is moved into place only on success, and let
curl retry transient failures like in the Supervisor version fetch.
Also fail the unit loudly when the download does not succeed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿงน Nitpick comments (1)
buildroot-external/rootfs-overlay/usr/libexec/haos-apparmor (1)

17-22: ๐Ÿฉบ Stability & Availability | ๐Ÿ”ต Trivial | โšก Quick win

Consider capping total retry time with --retry-max-time.

--max-time 30 bounds each individual transfer attempt, not the whole retry sequence โ€” curl's own documentation notes "The --max-time option still specifies the longest time a single of these transfers is allowed to spend." total retry duration is instead capped via "With --retry-max-time you cap the total time allowed for retries." As written, worst case is roughly (max-time + retry-delay) * (retries + 1) โ‰ˆ 4 minutes if the server is consistently slow/unreachable, which could noticeably delay boot before eventually failing loudly. Adding --retry-max-time would give an explicit upper bound on total wait time during startup.

This mirrors the same curl flag combination introduced in the haos-supervisor script for the stable-version fetch, so the same consideration applies there too.

โ™ป๏ธ Optional: bound total retry time
-    if ! curl -sfL --retry 5 --retry-delay 10 --retry-all-errors --max-time 30 \
+    if ! curl -sfL --retry 5 --retry-delay 10 --retry-all-errors --retry-max-time 120 --max-time 30 \
             -o "${PROFILES_DIR}/.hassio-supervisor.tmp" "${APPARMOR_URL}"; then
๐Ÿค– Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@buildroot-external/rootfs-overlay/usr/libexec/haos-apparmor` around lines 17
- 22, The curl download in haos-apparmor currently limits only each attempt via
the existing timeout, so the total retry window can grow much longer than
intended. Update the download command in the AppArmor profile fetch logic to add
a total retry cap with retry-max-time alongside the existing retry settings,
using the same pattern as the stable-version fetch in haos-supervisor. Keep the
current retry and max-time behavior, but ensure the overall startup wait is
explicitly bounded.
๐Ÿค– Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@buildroot-external/rootfs-overlay/usr/libexec/haos-apparmor`:
- Around line 17-22: The curl download in haos-apparmor currently limits only
each attempt via the existing timeout, so the total retry window can grow much
longer than intended. Update the download command in the AppArmor profile fetch
logic to add a total retry cap with retry-max-time alongside the existing retry
settings, using the same pattern as the stable-version fetch in haos-supervisor.
Keep the current retry and max-time behavior, but ensure the overall startup
wait is explicitly bounded.

โ„น๏ธ Review info
โš™๏ธ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: a65ebf10-2555-4958-87d8-89e8b8b4e0e9

๐Ÿ“ฅ Commits

Reviewing files that changed from the base of the PR and between 2be565b and 021d948.

๐Ÿ“’ Files selected for processing (1)
  • buildroot-external/rootfs-overlay/usr/libexec/haos-apparmor

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens first-boot recovery paths so transient network/time issues donโ€™t permanently prevent Supervisor startup after a factory reset/data wipe.

Changes:

  • Make Supervisor version resolution from version.home-assistant.io retryable and less fragile under transient DNS/TLS failures.
  • Prevent a โ€œpoisonedโ€ AppArmor state by keying downloads on the actual profile file and downloading atomically with retries + hard failure on download errors.
  • Increase haos-supervisor systemd start rate-limit tolerance and avoid spurious docker image rm usage errors when no images match.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
buildroot-external/rootfs-overlay/usr/sbin/haos-supervisor Adds retrying version fetch behavior and guards image cleanup against empty ID lists.
buildroot-external/rootfs-overlay/usr/libexec/haos-apparmor Makes Supervisor AppArmor profile download self-healing, atomic, and fail-fast with retries.
buildroot-external/rootfs-overlay/usr/lib/systemd/system/haos-supervisor.service Increases StartLimitBurst to reduce permanent failure during boot-time transient issues.

๐Ÿ’ก Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

agners and others added 2 commits July 7, 2026 14:56
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Match the naming convention of the other variables in the script.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@agners agners added the os label Jul 7, 2026
@agners
agners merged commit 54efae9 into dev Jul 7, 2026
1 check passed
@agners
agners deleted the supervisor-startup-resilience branch July 7, 2026 19:50
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants