Skip to content

Keep Dropbear alive during Supervisor and Docker shutdown - #4923

Merged
agners merged 1 commit into
devfrom
keep-dropbear-alive-during-shutdown
Jul 28, 2026
Merged

agners merged 1 commit into
devfrom
keep-dropbear-alive-during-shutdown

Conversation

@agners

@agners agners commented Jul 28, 2026 •

Copy link
Copy Markdown
Member

Description

dropbear.service has no ordering relationship to haos-supervisor.service or docker.service, so systemd schedules all three stop jobs in the same batch and runs them concurrently. Dropbear exits within milliseconds while the Supervisor gets up to 450s to stop containers (ExecStop=docker stop -t 420), so an SSH session is dropped right when the interesting part of shutdown starts. That makes it needlessly hard to debug slow shutdowns.

This orders Dropbear before both units. Since stop order is the reverse of start order, systemd now stops it only once the Supervisor and Docker are gone.

Notes on safety:

  • Ordering-only dependency. If Dropbear fails, is masked, or is skipped by its ConditionFileNotEmpty=/root/.ssh/authorized_keys, the ordering counts as satisfied and both units start as before.
  • No added boot latency: docker.service already waits for the overlay partition that Dropbear needs for /etc/dropbear.
  • No ordering cycle: /mnt/overlay is a separate partition with DefaultDependencies=no, and nothing below it depends on Docker. Verified with systemd-analyze verify against a built rootfs.

This only covers the unit-stop phase. Unmounting /mnt/data and the final systemd-shutdown phase still happen after Dropbear is gone, since its own mount dependencies have to be released first. Covering those would need DefaultDependencies=no + IgnoreOnIsolate=yes (debug-shell style), which is both riskier and a poor instrument โ€” the live session's cwd on /mnt/data would itself become an unmount blocker. That phase is better observed on the console with systemd.log_level=debug.

Verified on hardware โ€” see the comment below for the timings.

๐Ÿค– Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved shutdown sequencing to keep SSH/remote access available during supervisor and container teardown.
    • Updated service ordering so the remote access service stops after the supervisor/Docker services begin stopping, reducing interruptions during shutdown.

@coderabbitai

coderabbitai Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
๐Ÿ“ Walkthrough

Walkthrough

The Dropbear systemd override adds ordering constraints so Dropbear stops after the supervisor and Docker services during teardown.

Changes

Dropbear shutdown ordering

Layer / File(s) Summary
Shutdown ordering constraint
buildroot-external/rootfs-overlay/usr/lib/systemd/system/dropbear.service.d/haos.conf
Adds Before=haos-supervisor.service docker.service and comments describing the shutdown ordering.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Possibly related PRs

Suggested reviewers: copilot

Poem

A rabbit sees Dropbear wait,
While Docker shuts the garden gate.
The supervisor hops away,
Then SSH ends its day.
Hop, hopโ€”order saves the hay!

๐Ÿšฅ Pre-merge checks | โœ… 5
โœ… Passed checks (5 passed)
Check name Status Explanation
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.
Description Check โœ… Passed Check skipped - CodeRabbitโ€™s high-level summary is enabled.
Title check โœ… Passed The title clearly matches the main change: keeping Dropbear available while Supervisor and Docker shut down.
โœจ Finishing Touches
๐Ÿ“ Generate docstrings
  • Create stacked PR
  • Commit on current branch
๐Ÿงช Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch keep-dropbear-alive-during-shutdown

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.

@agners agners added the os label Jul 28, 2026

@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.

Actionable comments posted: 2

๐Ÿค– 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.

Inline comments:
In
`@buildroot-external/rootfs-overlay/usr/lib/systemd/system/dropbear.service.d/haos.conf`:
- Around line 4-8: Keep the existing Before=haos-supervisor.service
docker.service ordering, but revise the preceding comment to state that it
primarily preserves reverse shutdown ordering and does not serialize boot unless
the units start in the same transaction. Remove the claim that the shared mounts
add no boot work, and accurately describe the differing mount requirements
instead.
- Around line 17-21: Rewrite the comment above TimeoutStopSec=10s so it
describes only a 10-second upper bound or fallback for service shutdown, not a
guaranteed grace period for active sessions. Accurately note that KillMode=mixed
may terminate remaining session processes when Dropbear exits before the
timeout.
๐Ÿช„ Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

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

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 07d3f285-7f24-45b0-8736-e791cda0dc26

๐Ÿ“ฅ Commits

Reviewing files that changed from the base of the PR and between a69129b and f95e5fc.

๐Ÿ“’ Files selected for processing (1)
  • buildroot-external/rootfs-overlay/usr/lib/systemd/system/dropbear.service.d/haos.conf

Comment on lines +4 to +8
# Systemd stops units in reverse start order, so ordering dropbear before the
# Supervisor and Docker keeps SSH available while their (potentially minutes
# long) teardown runs. Both already require the same mounts dropbear needs, so
# this adds no extra work to boot.
Before=haos-supervisor.service docker.service

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

๐Ÿš€ Performance & Scalability | ๐ŸŸก Minor | โšก Quick win

๐Ÿงฉ Analysis chain

๐Ÿ Script executed:

#!/bin/sh
set -eu

echo "== repo files =="
git ls-files | rg 'dropbear|haos-supervisor|docker|systemd|buildroot-external/rootfs-overlay/usr/lib/systemd' || true

echo "== target file =="
if [ -f buildroot-external/rootfs-overlay/usr/lib/systemd/system/dropbear.service.d/haos.conf ]; then
  cat -n buildroot-external/rootfs-overlay/usr/lib/systemd/system/dropbear.service.d/haos.conf
fi

echo "== related unit excerpts =="
for f in $(git ls-files | rg 'usr/lib/systemd/system/(dropbear|haos-supervisor|docker).service|buildroot-external/rootfs-overlay/usr/lib/systemd/system/(dropbear|haos-supervisor|docker).service'); do
  if [ -f "$f" ]; then
    echo "--- $f ---"
    cat -n "$f"
  fi
done

echo "== search RequiresMountsFor for mounts =="
rg -n "RequiresMountsFor|dropbear|haos-supervisor|docker.service|Before=|After=" buildroot-external/rootfs-overlay usr 2>/dev/null || true

Repository: home-assistant/operating-system

Length of output: 24905


Keep the order, adjust the boot rationale.

Before=haos-supervisor.service docker.service does not serialize those services unless they all need to start in the same systemd transaction; otherwise it only affects shutdown ordering. The โ€œadds no extra work to bootโ€ wording is therefore misleading. Also, Dropbearโ€™s mount is /etc/dropbear; Supervisor only requires /mnt/data, /mnt/boot, and /mnt/overlay, while Docker includes /etc/docker and /mnt/data.

๐Ÿค– 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/lib/systemd/system/dropbear.service.d/haos.conf`
around lines 4 - 8, Keep the existing Before=haos-supervisor.service
docker.service ordering, but revise the preceding comment to state that it
primarily preserves reverse shutdown ordering and does not serialize boot unless
the units start in the same transaction. Remove the claim that the shared mounts
add no boot work, and accurately describe the differing mount requirements
instead.

Comment on lines +17 to +21
# With KillMode=mixed only the listener gets SIGTERM, so an open session keeps
# the cgroup alive until the stop timeout expires. Since dropbear is now the
# last thing stopped, that wait sits on the critical path of shutdown: keep it
# short instead of the default 90s.
TimeoutStopSec=10s

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

๐Ÿฉบ Stability & Availability | ๐ŸŸก Minor | โšก Quick win

๐Ÿงฉ Analysis chain

๐Ÿ Script executed:

#!/bin/sh
set -eu

echo "Files matching haos.conf:"
fd -a 'haos\.conf$' . || true

file="$(fd 'haos\.conf$' . | head -n1)"
if [ -n "${file:-}" ]; then
  echo
  echo "Content of $file:"
  cat -n "$file"
fi

echo
echo "Search for dropbear systemd units/configs:"
rg -n "dropbear|KillMode|TimeoutStopSec|TimeoutStopUSec" . -g '!*.log' 2>/dev/null || true

echo
echo "Relevant file tree around possible systemd overlay path:"
fd . buildroot-external/rootfs-overlay/usr/lib/systemd/system 2>/dev/null | sed -n '1,80p' || true

Repository: home-assistant/operating-system

Length of output: 10772


๐ŸŒ Web query:

man systemd.kill 5 KillMode mixed behavior SIGTERM TimeoutStopSec

๐Ÿ’ก Result:

When KillMode=mixed is configured in a systemd unit, the termination process follows a specific sequence designed to gracefully stop the main process while cleaning up any lingering processes within the unit's control group [1][2][3]. 1. Initial Signal: systemd sends the KillSignal (defaulting to SIGTERM) to the main process of the unit [1][2][3]. 2. Transition to SIGKILL: A subsequent SIGKILL (or the signal configured via FinalKillSignal) is sent to all remaining processes in the unit's control group if either of the following conditions is met [1][2][3]: - The main process exits [1][2][3]. - The time duration configured in TimeoutStopSec= has elapsed [1][2][3]. This behavior allows the main process to perform a clean shutdown upon receiving the initial signal, while ensuring that all other processes associated with the service are forcibly terminated either immediately upon the main process's exit or after the specified timeout period [1][2][3]. If SendSIGKILL= is enabled (the default), systemd ensures that any processes remaining in the control group are cleaned up [4][1][2]. If disabled, systemd will not necessarily restart the service if processes from the prior instance are still detected in the control group [4][1][2].

Citations:


Do not describe TimeoutStopSec=10s as a guaranteed session grace period.

With KillMode=mixed, systemd sends SIGTERM to Dropbearโ€™s main process and can forcibly terminate remaining cgroup processes as soon as that process exits; TimeoutStopSec is only the fallback when shutdown has not completed. Active SSH sessions may therefore be killed immediately rather than after 10 seconds.

If the intent is only a 10-second upper bound, rewrite the comment accordingly. If a 10-second graceful window is required, verify Dropbearโ€™s shutdown behavior and consider a kill mode that signals session processes as well.

๐Ÿค– 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/lib/systemd/system/dropbear.service.d/haos.conf`
around lines 17 - 21, Rewrite the comment above TimeoutStopSec=10s so it
describes only a 10-second upper bound or fallback for service shutdown, not a
guaranteed grace period for active sessions. Accurately note that KillMode=mixed
may terminate remaining session processes when Dropbear exits before the
timeout.

Dropbear has no ordering relationship to haos-supervisor.service or
docker.service, so systemd schedules all three stop jobs in the same
batch and runs them concurrently. Dropbear exits within milliseconds
while the Supervisor gets up to 450s to stop containers, which means an
SSH session is dropped right when the interesting part of shutdown
starts. That makes it needlessly hard to debug slow shutdowns.

Order Dropbear before both units. Since stop order is the reverse of
start order, systemd now stops it only once the Supervisor and Docker are
gone. This is an ordering-only dependency: if Dropbear fails, is masked
or is skipped by its ConditionFileNotEmpty=, the ordering counts as
satisfied and both units start as before. It adds no boot latency
either, as docker.service already waits for the overlay partition that
Dropbear needs for /etc/dropbear.

Verified on a generic-x86-64 VM: an SSH session now survives the full
22.4s Supervisor teardown, and systemd stops Dropbear last, 3ms after
docker.service is gone.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@agners
agners requested a review from Copilot July 28, 2026 13:44
@agners
agners force-pushed the keep-dropbear-alive-during-shutdown branch from f95e5fc to 8d6bbc7 Compare July 28, 2026 13:44
@agners

agners commented Jul 28, 2026

Copy link
Copy Markdown
Member Author

Verification on hardware

Tested on a generic-x86-64 VM with an SSH session on port 22222 running journalctl -f across a reboot. The session survived the entire Supervisor teardown, where previously it was dropped at the very first Stopping HAOS supervisor... line.

Stop ordering

From the persisted journal of the previous boot (journalctl -b -1 -u dropbear -u docker -u haos-supervisor -o short-precise):

15:14:41.972  Stopped HAOS supervisor.
15:14:41.974  Stopping Docker Application Container Engine...
15:14:42.068  Stopped Docker Application Container Engine.
15:14:42.071  Stopping Dropbear SSH daemon...      <-- last
15:14:42.080  Stopped Dropbear SSH daemon

Both ordering edges hold: Dropbear is stopped last, 3ms after docker.service is gone.

Timings

Phase Start Container gone Duration
reboot initiated 15:14:16.0
homeassistant (Core) 15:14:16.35 15:14:23.17 6.8s
app_core_matter_server 15:14:23.22 15:14:26.29 3.07s
hassio_cli 15:14:26.33 15:14:29.36 3.02s
hassio_dns 15:14:29.43 15:14:32.47 3.04s
hassio_audio 15:14:32.54 15:14:35.58 3.04s
hassio_multicast 15:14:35.66 15:14:38.70 3.04s
Supervisor is down 15:14:38.74 22.4s
Stopped HAOS supervisor 15:14:41.97
Stopped Docker 15:14:42.07 94ms
Stopped Dropbear 15:14:42.08 9ms

All 22.4s of the Supervisor teardown is now observable over SSH.

An earlier revision of this PR also set TimeoutStopSec=10s โ€” dropped, and why

The reasoning was: Dropbear is built without PAM, so SSH sessions live in the service's own cgroup, and KillMode=mixed only sends SIGTERM to the listener. An open session would therefore keep the cgroup populated until the stop timeout expires โ€” and with Dropbear stopped last, that 90s default would sit on the critical path of shutdown.

The journal shows that is not how systemd behaves:

15:14:42.072353  dropbear[472]: Early exit: Terminated by signal
15:14:42.072563  dropbear.service: Killing process 2432 (dropbear) with signal SIGKILL.
15:14:42.072608  dropbear.service: Killing process 2433 (sh) with signal SIGKILL.
15:14:42.072648  dropbear.service: Killing process 2697 (journalctl) with signal SIGKILL.

The live session (sh plus the journalctl -f) was SIGKILLed 200ยตs after the listener exited, and the whole stop took 9ms. TimeoutStopSec= bounds how long systemd waits for the main process to exit; once that is gone, systemd proceeds straight to the final SIGKILL of whatever remains in the cgroup. Since Dropbear exits promptly on SIGTERM, the tail cannot occur, so the setting was dead config and has been removed. This PR is now a single Before= line.

Unrelated lead: ~3s of idle time per plugin container

Worth recording since this change is what made it visible. Every plugin takes almost exactly 3.0s to stop, and it is not spent doing work. For hassio_dns, s6 reports legacy-services, legacy-cont-init and fix-attrs all successfully stopped within ~30ms at 15:14:29.4, and then nothing happens until the scope deactivates at 15:14:32.47. The same shape repeats on every container.

Five plugins idling ~3s each is roughly 15s of the 22.4s, and it is a fixed delay rather than load-dependent. That points at one of s6-overlay's 3000ms defaults (S6_SERVICES_GRACETIME / S6_KILL_GRACETIME) โ€” container PID 1 waiting out a timer after everything beneath it has already stopped. Which of the two it is would need confirming. Not actionable in this repo (it belongs to the Supervisor plugin images), but it is the concrete lead behind a good chunk of HAOS shutdown time.

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 adjusts systemd unit ordering so dropbear.service remains running until haos-supervisor.service and docker.service have stopped, keeping SSH available during potentially long container/supervisor shutdown. It also reduces Dropbearโ€™s shutdown critical-path time by lowering its stop timeout.

Changes:

  • Add an ordering-only relationship: dropbear.service is ordered before Supervisor and Docker so it stops after them at shutdown.
  • Set TimeoutStopSec=10s for Dropbear to avoid long shutdown delays when active SSH sessions keep the unit cgroup populated under KillMode=mixed.

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

@agners
agners merged commit 6f55637 into dev Jul 28, 2026
1 check passed
@agners
agners deleted the keep-dropbear-alive-during-shutdown branch July 28, 2026 15:02
agners added a commit that referenced this pull request Jul 30, 2026
Dropbear has no ordering relationship to haos-supervisor.service or
docker.service, so systemd schedules all three stop jobs in the same
batch and runs them concurrently. Dropbear exits within milliseconds
while the Supervisor gets up to 450s to stop containers, which means an
SSH session is dropped right when the interesting part of shutdown
starts. That makes it needlessly hard to debug slow shutdowns.

Order Dropbear before both units. Since stop order is the reverse of
start order, systemd now stops it only once the Supervisor and Docker are
gone. This is an ordering-only dependency: if Dropbear fails, is masked
or is skipped by its ConditionFileNotEmpty=, the ordering counts as
satisfied and both units start as before. It adds no boot latency
either, as docker.service already waits for the overlay partition that
Dropbear needs for /etc/dropbear.

Verified on a generic-x86-64 VM: an SSH session now survives the full
22.4s Supervisor teardown, and systemd stops Dropbear last, 3ms after
docker.service is gone.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
@agners agners mentioned this pull request Jul 30, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 5, 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