Skip to content

Restart bt-agent when org.bluez loses its owner - #15097

Open
omarchybot wants to merge 2 commits into
quattrofrom
fix/issue-11936
Open

omarchybot wants to merge 2 commits into
quattrofrom
fix/issue-11936

Conversation

@omarchybot

Copy link
Copy Markdown
Collaborator

An agent registration lives in the bluetoothd process that accepted it. When bluetoothd dies without a clean shutdown (a crash, a SIGKILL, a stop that runs into its timeout), bt-agent stays alive, still registered with a daemon that no longer exists. Restart=on-failure never fires because nothing failed, systemd keeps reporting the agent as active, and the replacement daemon has no agent, so pairing a passkey device fails with No agent available for request type 2.

The unit now runs gdbus monitor --system --dest org.bluez beside bt-agent and exits 1 as soon as either org.bluez loses its owner (matched on gdbus's whole The name org.bluez does not have an owner line, so a device name cannot trigger it) or bt-agent exits. systemd then restarts it against the live daemon. This reacts to the ownership change rather than polling, and is the same supervision the reporter tested on their machine.

bt-agent also does not exit on SIGTERM, so systemctl --user stop bt-agent.service, and with it every logout, waited out the 90-second stop timeout and left the unit failed. KillMode=mixed sends SIGTERM to the wrapper and SIGKILL to the rest once it has gone; bluetoothd drops an agent along with its bus connection, so nothing is left registered.

On a worker built from the Omarchy ISO (BlueZ 5.87, bluez-tools 0.2.0, a virtual HCI controller, the shipped user unit), with quattro as it stands, SIGKILLing bluetoothd and starting it again leaves bt-agent on the same PID with no new registration. With this change it restarts once and re-registers within about two seconds. A clean systemctl restart bluetooth.service already recovered before this change, because bt-agent exits 1 when its daemon disconnects, and still does.

Two limits are unchanged. If bluetooth.service is not active when the restart comes two seconds later, ExecCondition skips the unit and nothing retries it. Stock takes the same path after every clean restart today; it is the race in #8962 and #13247 and is not addressed here. And the watcher subscribes at about the moment bt-agent registers, so a daemon replaced within that window at unit start would go unnoticed.

#13258, #13598, #13620 and #14795 edit this unit for other reasons and will conflict textually; #13258 also replaces ExecStart.

Fixes #11936

An agent registration lives in the bluetoothd process that accepted it. When bluetoothd dies without a clean shutdown (a crash, a SIGKILL, a stop that times out) bt-agent stays alive and registered with nobody, so Restart=on-failure never fires and the replacement daemon has no agent to pair with. Watch org.bluez with gdbus monitor beside bt-agent and exit as soon as either the owner goes away or bt-agent exits, so systemd restarts it against the live daemon.

bt-agent also does not exit on SIGTERM, so stopping the unit waited out the 90s stop timeout and ended failed. KillMode=mixed sends SIGTERM to the wrapper and SIGKILL to the rest once it has gone.

Co-Authored-By: Codex Medium <noreply@openai.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@greptile-apps

greptile-apps Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable defect was established.

Summary

Adds a watcher that restarts bt-agent after org.bluez loses its owner.

  • The pairing service restarts when BlueZ loses its owner.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Start wrapper] --> B[Watch org.bluez]
  A --> C[Run bt-agent]
  B --> D[Owner disappears]
  C --> E[Agent exits]
  D --> F[Wrapper exits with failure]
  E --> F
  F --> G[systemd cleans up children]
  G --> H[Restart after two seconds]
Loading

Reviews (2) · Last reviewed commit: "Merge quattro into fix/issue-11936" · Reviewed by Greptile

Comment thread test/shell.d/systemd-test.sh Resolved
@omarchybot

Copy link
Copy Markdown
Collaborator Author

Verified at 629b95a on a worker built from the Omarchy ISO (BlueZ 5.87, bluez-tools 0.2.0, a virtual HCI controller via hci_vhci). Each run SIGKILLed bluetoothd, started it again, then stopped the agent:

  • quattro's unit: bt-agent kept the same PID, did not restart, and never registered with the new daemon. systemctl --user stop took 90 seconds and left the unit failed (Result=timeout).
  • This head: the unit restarted once and logged Agent registered / Default agent requested against the new daemon. Stop took 0.01 seconds, ended success, and left no gdbus or bt-agent process behind. A clean systemctl restart bluetooth.service also brought it back registered.
  • With the unit reverted to quattro's, it went stale again, and stop timed out again.

./test/cli and test/shell.d/systemd-test.sh pass on this head, and on this head merged with current quattro (33783b1).

Codex at medium effort reviewed it as a second opinion and found no defect in the change. It checked that the wrapper always exits 1 (owner loss, agent exit, monitor EOF), that a device name cannot match gdbus's whole owner-loss line, that systemd passes the quoting through intact, and that the default start limit is not exhausted by these restarts. It agreed with the two limits the description already states. Its independence is not guaranteed. The ExecCondition gap when bluetoothd is still down at the retry is what #13620 addresses, so the two are complementary. The four open PRs that edit this unit (#13258, #13598, #13620, #14795) fix other problems and do not compete with this one, though each will conflict textually.

Labelled bug, verified and ready. Waiting on the maintainer.

quattro removed tests that only read source text (#16334), and this branch's two systemd-test.sh checks grepped the unit for the exact ExecStart and KillMode lines it adds, so the conflict resolves to quattro's file and the branch now changes only the unit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@omarchybot omarchybot added verified Omarchy Triage has verified that this issue is ready for final review ready Good to merge labels Nov 1, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator Author

This branch stopped merging into quattro after #16334 removed tests that only read source text. Its two systemd-test.sh checks grepped the unit for the exact ExecStart and KillMode lines this change adds, which is the kind of test AGENTS.md now rules out. I merged quattro into the branch and took quattro's test file (ae814b8), so the pull request now changes only default/systemd/user/bt-agent.service, and the unit itself is unchanged.

Re-verified at ae814b8 on a worker built from the Omarchy ISO (BlueZ 5.87, bluez-tools 0.2.0, a virtual HCI controller through /dev/vhci). Each run SIGKILLed bluetoothd, started bluetooth.service again, then stopped the agent:

  • quattro's unit: bt-agent kept its PID, did not register with the new daemon, and systemctl --user stop took 90.14 seconds and ended Result=timeout.
  • This head: the unit restarted once and registered with the new daemon. Stop took under 0.01 seconds, ended success, and left no gdbus or bt-agent process behind.
  • With the unit reverted to quattro's, it went stale again and stop timed out again (90.24 seconds).

./test/cli and test/shell.d/systemd-test.sh pass on the merged tree, and CI is green.

Codex at medium effort and muse-wonder (via pi, at high thinking) reviewed the merge as a second opinion. Both found the conflict resolution correct and the change proportionate. Neither found a defect in the change; their independence is not guaranteed. muse-wonder raised the case where bluetooth.service is still down two seconds after the crash. Then ExecCondition skips the restart and nothing retries it. The description already names this limit: it is the existing race from #8962 and #13247, which #13258 and #13620 address. #13258, #13598, #13620 and #14795 edit this unit for other reasons. None of them fixes this bug, but #13258 also replaces ExecStart, so whichever lands second will need a textual merge.

Labelled verified and ready. Waiting on the maintainer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ready Good to merge verified Omarchy Triage has verified that this issue is ready for final review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bluetooth agent stays stale after bluetoothd restarts

1 participant