Skip to content

Restart bluetoothd and reload btusb when the adapter is wedged - #13085

Open
pedrohfp wants to merge 6 commits into
omacom:quattrofrom
pedrohfp:restart-bluetooth-recovery
Open

pedrohfp wants to merge 6 commits into
omacom:quattrofrom
pedrohfp:restart-bluetooth-recovery

Conversation

@pedrohfp

@pedrohfp pedrohfp commented Sep 24, 2026 •

Copy link
Copy Markdown

Fixes #13078. Fixes #12519.

Problem

omarchy-restart-bluetooth summarises itself as "Unblock and restart the bluetooth service", but the whole script was:

rfkill unblock bluetooth
rfkill list bluetooth

It never restarted anything. That is the correct and complete fix for a radio the user switched off, and it is a no-op for the two ways an adapter actually goes missing:

What changed

rfkill unblock                              unprivileged, unchanged
sudo systemctl restart bluetooth.service    unconditional; exit 1 if it fails
systemctl is-active                         exit 1 if bluetoothd never started
wait up to 5s for a controller
  listed      -> print status, exit 0
  none, btusb -> reload btusb, wait up to 15s
still none    -> hint, exit 1

The restart is unconditional. A daemon that lost sync still lists its controller, so there is no reliable way to tell that case from a healthy one before restarting. Every invocation therefore asks for a password and briefly drops connected devices — that is the cost of the action doing what its summary promises.

Notes on the shape:

  • wait_for_controller budgets elapsed seconds, not polls. bluetoothctl waits indefinitely for bluetoothd, so each probe carries its own timeout; counting iterations would stretch the 5s window into ~35s of silent terminal when the daemon is absent.
  • systemctl is-active gates the wait. The unit carries ConditionPathIsDirectory=/sys/class/bluetooth, so a machine with no Bluetooth stack takes a clean exit from the restart without ever starting bluetoothd — and a btusb reload cannot help there either.
  • A failed modprobe -r leaves btusb loaded, so there is nothing to put back; the script reports that rather than running a reload that never came out.
  • Marked # omarchy:requires-sudo=true, matching omarchy-restart-trackpad, which already does sudo modprobe -r. The menu runs this in a floating terminal, so the prompt has a tty.
  • An adapter that stays dead exits 1 instead of finishing quietly, so the menu action stops claiming success.

Verification

test/shell.d/restart-bluetooth-test.sh is new. It stubs rfkill/sudo/systemctl/modprobe/lsmod/bluetoothctl on PATH and drives the escalation through its budgets in ~6s:

ok - restart bluetooth unblocks the rfkill soft block
ok - restart bluetooth restarts bluetooth.service
ok - restart bluetooth leaves btusb alone once a controller answers
ok - restart bluetooth fails when the service restart fails
ok - restart bluetooth stops before btusb when the restart failed
ok - restart bluetooth fails without polling when the daemon did not start
ok - restart bluetooth reloads btusb when no controller answers
ok - restart bluetooth reports a failed unload instead of reloading
ok - restart bluetooth only reloads btusb when btusb is loaded
ok - restart bluetooth fails when no controller comes back
  • Against the upstream script the suite fails at restart bluetooth restarts bluetooth.service, so it pins the reported bug rather than the new code.
  • Mutation controls: collapsing the unload/load branch back to sudo modprobe -r btusb && sudo modprobe btusb fails the suite, and so do a mutant that keeps escalating after a failed restart and only exits 1 at the end and one that restores the old second sentence of the restart-failure message. The stubs log sudo, so the denied-restart case asserts exactly one call — it is 1 when the script stops there and 2 when it goes on to the reload (the refused modprobe -r skips the load).
  • bash test/shell.d/bin-style-test.sh, bash test/shell.d/command-operand-boundaries-test.sh
  • omarchy restart still lists the command with its summary, so the added metadata parses.
  • Real hardware (MacBook, btusb, Intel controller 08:6D:41:D0:57:C9, Bose QC35 on A2DP). bluetooth.service restarted cleanly — bluetoothd came back on a new PID, re-registered every A2DP endpoint, and the headset reconnected on its own ~7s later. journalctl -k shows nothing from btusb, confirming a controller that answers never reaches the reload.
  • Real hardware, restart denied: with no tty for the sudo prompt the script now exits 1 with Could not restart bluetooth.service on a machine whose controller is perfectly healthy. The first pass printed that controller and exited 0.
  • Real hardware, bt-agent after the restart (Bluetooth agent stays stale after bluetoothd restarts #11936 asks whether a restart leaves the pairing agent registered but stale). It does not: bluetoothd sends Release, the agent's UnregisterAgent fails with org.freedesktop.DBus.Error.ServiceUnknown, it exits 1, and Restart=on-failure has it registered again 2s later on a new PID.
18:37:31 bt-agent[1015]:  Agent released
18:37:31 bt-agent[1015]:  unregistering agent...
18:37:31 bt-agent[1015]:  org.freedesktop.DBus.Error.ServiceUnknown
18:37:31 systemd[946]:    bt-agent.service: Main process exited, status=1/FAILURE
18:37:33 systemd[946]:    bt-agent.service: Scheduled restart job, restart counter is at 1.
18:37:33 bt-agent[86665]: Agent registered
18:37:33 bt-agent[86665]: Default agent requested

omarchy-restart-bluetooth has always summarised itself as "Unblock and
restart the bluetooth service", but it only ran rfkill unblock. That is the
right fix for a radio the user switched off and nothing else, so the menu's
Update > Hardware > Bluetooth action was a no-op for the two ways an adapter
actually goes missing.

A bluetoothd that lost sync with D-Bus adapter state needs the restart the
summary already promised: the shell's panel reads adapter as null and
toggleBluetooth() returns early, so the GUI cannot recover it either.

A controller whose firmware handshake timed out ("Reading Intel version
command failed (-110)") is worse. It leaves an hci device that BlueZ will
not adopt, and no amount of restarting the daemon clears it. Reloading btusb
re-runs the firmware upload, which is the only way back without a reboot.

So escalate: unblock, restart the service, and only if no controller answers
reload btusb. A radio that was merely soft-blocked never reaches the reload.
An adapter that stays dead now says so and exits non-zero instead of
finishing quietly.

Fixes omacom#13078
Fixes omacom#12519

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

llstrk commented Sep 25, 2026

Copy link
Copy Markdown

Automated AI review

Community review: Independent automated community review, unaffiliated with the Omarchy team, intended to help prepare PRs for their review.

Verified: in the single-adapter cases from #13078 and #12519, the escalation behaves as described, based on source reading and sandboxed runs with stubbed commands. The new test passes 7/7 on this head. Run against the previous script, it fails at restart bluetooth restarts bluetooth.service, as the PR states. BlueZ, systemd, kmod and kernel source support the approach:

  • non-interactive bluetoothctl list prints plain Controller ... lines and waits indefinitely for bluetoothd, so the timeout 5s guard is needed
  • bluetooth.service is Type=dbus, so systemctl restart returns once org.bluez is on the bus
  • an Intel controller whose setup failed stays in HCI_SETUP and is hidden from bluetoothd, so only a driver reload can recover it
rfkill unblock                          (unprivileged, unchanged)
sudo systemctl restart bluetooth.service   <- exit status not checked
poll `bluetoothctl list` x5
  controller listed -> print status, exit 0
  none listed, btusb loaded -> modprobe -r btusb; modprobe btusb; poll x15
none listed at the end -> hint, exit 1

Four smaller points remain.

A failed restart can still end in success

The status of sudo systemctl restart bluetooth.service is not checked. In a stubbed run where sudo was denied and a controller was still listed, the script printed the controller and exited 0, so the menu shows Done although nothing restarted. With no controller listed, it continued to the btusb reload and invoked sudo twice more (3 sudo attempts in total) before exiting 1.

Impact: for #13078, the desynced daemon may still list a controller. A cancelled or denied password prompt then reports success without the restart that fixes it. This undercuts the PR's aim that the menu action stops claiming success.

Suggested change: branch on the restart's exit status, report the failure and exit nonzero before polling or reloading, as omarchy-restart-audio does with its restart.

The restart now runs on every invocation, including a plain soft block

The PR description says a soft-blocked radio "never goes further" than rfkill unblock. The script always runs sudo systemctl restart bluetooth.service after the unblock.

Impact: every use of Update > Hardware > Bluetooth now asks for a password and restarts bluetoothd, which briefly drops connected devices. Before this change, the soft-block case needed neither. An unconditional restart may be the intended trade-off: it matches the summary, and gating it on bluetoothctl list could miss #13078 if a desynced daemon still lists a controller (not verified).

Suggested change: update the description to state that the restart is unconditional, or document the trade-off in the script comment.

The "5s" window stretches when bluetoothd is not on the bus

wait_for_controller counts polls, not elapsed time. When bluetoothd is not running, each timeout 5s bluetoothctl list uses its full 5 s timeout.

Condition Result
Daemon up, controller slow to appear about 1 s per poll, as intended
Daemon absent, btusb not loaded 35.04 s measured before the failure message (real timeout/sleep, stubbed bluetoothctl)
Daemon absent, btusb loaded about 125 s modeled (5 + 15 polls plus the final check)

This can happen when no Bluetooth stack is present: the unit's ConditionPathIsDirectory=/sys/class/bluetooth skips the start and systemctl still exits 0. A masked unit or a daemon that fails to start has the same effect.

Impact: in those cases the menu terminal shows no further output for about 35 s (measured) to about 125 s (modeled) before the failure message, instead of the 5 s window the PR description states.

Suggested change: use an elapsed-time deadline (for example Bash SECONDS) or a shorter per-poll timeout. Alternatively, check systemctl is-active --quiet bluetooth.service before polling and before a reload that cannot help without the daemon.

Failure hint

Documentation note: the message suggests journalctl -b -u bluetooth for firmware errors. Reading Intel version command failed (-110) is a kernel message (btintel via bt_dev_err), and -u matches only unit fields, so it does not appear there. journalctl -b -k shows it. lsusb comes from usbutils, which is not in install/omarchy-base.packages or install/omarchy-other.packages, so the command may not exist on a default install.

Optional test improvement: the "busy module" case never makes modprobe -r fail, and a mutation replacing the two calls with sudo modprobe -r btusb && sudo modprobe btusb still passes the whole suite. With kmod, a failed modprobe -r btusb leaves btusb loaded, so the following modprobe btusb is a no-op in that case. The separate calls are harmless but do not "put it back". Rewording that script comment and the test comment is enough. A stub that fails -r would make the test match its description.

Scope note: with two controllers, the reload happens only when neither is listed, so a wedged second USB adapter is not recovered while another controller works, and the script exits 0. A forced modprobe -r btusb would drop every btusb controller's connections, so the current gate is the safer default.


Review information

Test scope: pinned head af0c458c81d536ea7776954cead0a0915871b011. Source reading of the script, test, router and menu entry point, plus BlueZ 5.87, systemd (v258 and v262), kmod 34, coreutils 9.11 and Linux 7.2 source. The PR test and extra scenarios (sudo denied, busy unload, daemon absent, mutation controls) ran in an isolated sandbox with every privileged command stubbed. Real hardware, a real bluetoothd, real sudo prompts and a genuinely wedged adapter were not tested. The btusb reload path rests on kernel source and the reporter's manual recovery in #12519.

AI process: Opus 5.5 Medium coordination and synthesis, independent Opus 5.5 Xhigh and GPT 6 Sol Xhigh technical assessments with targeted follow-up questions, Opus 5.5 Medium editorial check.

Opt out: To stop receiving these reviews, reply to this comment saying so.

Review of the first pass found four ways the escalation could still mislead.

The restart's exit status was not checked. A password prompt the user cancels
leaves a desynced daemon listing its controller, so the script printed that
controller and exited 0 while the menu showed Done. That is the exact claim
the change set out to remove. Branch on the restart and stop there.

bluetooth.service carries ConditionPathIsDirectory=/sys/class/bluetooth, so a
machine with no Bluetooth stack takes a clean exit from the restart without
ever starting bluetoothd. Waiting for a controller then spent the whole budget
on a daemon that was never meant to run, and a btusb reload could not help
either. Confirm the daemon is active before polling.

wait_for_controller counted polls, not seconds, and bluetoothctl waits
indefinitely for bluetoothd, so each poll could spend its whole guard: the
five second window became about 35 seconds of silent terminal, and about 125
with btusb loaded. Budget elapsed seconds and shorten the per-poll guard.

The failure hint pointed at 'journalctl -b -u bluetooth' for an error that
btintel emits through the kernel, where -u cannot match it, and suggested
lsusb, which usbutils ships and Omarchy does not install. Point at -k instead.

Also drop a comment that justified splitting the modprobe calls with a case
that cannot happen: a failed unload leaves btusb loaded, so the reload was
always a no-op there. Report the failed unload instead, and cover it with a
stub that fails -r, which the previous suite never did.

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

Copy link
Copy Markdown
Author

Thanks — all four held up, and two of them I could confirm on the machine rather than by reading. Pushed in e1c1b1b.

Restart status unchecked. The clearest one. Reproduced on real hardware without needing a wedged adapter: run it where sudo has no tty and the first pass printed the healthy controller and exited 0. It now exits 1 with Could not restart bluetooth.service and stops before the reload, so the sudo attempt count drops from 3 to 1.

Daemon absent. Added a systemctl is-active --quiet gate after the restart, so the ConditionPathIsDirectory case fails immediately instead of polling a daemon that was never started.

Elapsed time. wait_for_controller now budgets SECONDS instead of counting polls, and the per-probe guard is down to timeout 2s. The two budgets read from OMARCHY_BLUETOOTH_RESTART_WAIT / OMARCHY_BLUETOOTH_RELOAD_WAIT, defaulting to the shipped 5 and 15 — that is a test seam in the shape of OMARCHY_POWER_SUPPLY_PATH in omarchy-battery-status, and it keeps the expiry paths in the suite at ~6s total.

Failure hint. Confirmed both halves on a stock install: journalctl -b -u bluetooth returns zero kernel lines, and lsusb is genuinely absent since usbutils is in neither packages file. Now points at journalctl -b -k, with the daemon's own log named separately, and no lsusb.

On the unconditional restart. Keeping it that way and documenting it, rather than gating on bluetoothctl list — as you note, a desynced daemon can still list its controller, which is exactly the #13078 case, so the gate would skip the machines the change is for. The PR description said a soft-blocked radio "never goes further"; that was wrong about the restart and is fixed.

On the modprobe comment. You were right that the stated reason could not happen — a failed unload leaves btusb loaded, so the reload was always a no-op there. The script now reports the failed unload instead, and the suite has a stub that fails -r. Collapsing the branch back to && fails the suite now, where it survived before.

The two-controller scope note I left alone, for the reason you give: a forced reload would drop every btusb controller's connections.

Not opting out — this was useful.

@MuseHorizon

Copy link
Copy Markdown

I tested your solution and it works on my end. Thanks

@llstrk

llstrk commented Sep 27, 2026

Copy link
Copy Markdown

Automated AI review

Community review: Independent automated community review, unaffiliated with the Omarchy team, intended to help prepare PRs for their review.

Follow-up to the earlier review and the author's reply, checked at e1c1b1b.

Verified: all five points from the earlier review are resolved on this head. The two-controller scope note is unchanged, as intended. None of the earlier points turned out wrong. The updated description matches the code: the flow diagram, the ten test names, the failure of the suite against the upstream script, and the mutation control (sudo modprobe -r btusb && sudo modprobe btusb now fails the suite).

Earlier point Result at e1c1b1b (sandbox, privileged commands stubbed)
Failed restart could end in success Resolved. Sudo denied with a controller listed: exit 1 after 1 sudo call (previous head: exit 0). Sudo denied, no controller: 1 sudo call and exit 1 at once (previous head: 3 calls over 20 s).
Restart runs on every invocation Resolved. The description and the script comment now state that the restart is unconditional and why.
5 s window stretched without a daemon Resolved. Unit inactive after restart: exit 1 in under 0.01 s with no probes (previous head: 35 s). Daemon up, no controller: about 5 s, reload, about 15 s, exit 1.
Failure hint Resolved. Points at journalctl -b -k, names the daemon's log separately, no lsusb. The suite now checks it.
Busy-module test Resolved. The stub fails -r; the script reports the failed unload and skips the load.
Two-controller scope Unchanged by design.

The systemctl is-active gate does not block the #12519 recovery: /sys/class/bluetooth is registered when bluetooth.ko initialises, and btusb depends on it, so the unit's condition can only be unmet when btusb is not loaded.

Two small points on the new code, plus two related notes.

The restart-failure message can be inaccurate

When the restart fails, the script prints Could not restart bluetooth.service. Bluetooth is unblocked; nothing else was changed. The second clause holds when sudo is refused. When sudo succeeds but the restart job fails in its start phase, systemd has already stopped bluetoothd, so the daemon is now down (systemd job.c: a restart runs as stop, then start; the stubbed run shows the same message on this path). "Bluetooth is unblocked" is also not guaranteed: rfkill unblock exits 0 under a hard block, and its failure (no write access to /dev/rfkill) is not checked.

Impact: on a failed restart, the message can say nothing changed while bluetoothd has been stopped.

Suggested change: drop the second sentence, or replace it with the systemctl status bluetooth.service hint the is-active branch already prints.

Optional test improvement: the assertion restart bluetooth stops before btusb when the restart failed cannot fail. The sudo stub fails every call, so no modprobe can be logged, and the case already lists a controller, so the reload branch is never reached. A mutant that keeps escalating after the failed restart and only exits 1 at the end still passes 10/10. Logging sudo calls in the stub, running this case with no controller listed, and asserting exactly one sudo call would make it test what its name says.

Related open PR: #13101 also adds a bluetooth.service restart to this script for #13078. Merging this PR's head and #13101's head against their common base (git merge-tree) gives two conflict hunks in bin/omarchy-restart-bluetooth, and #13101's unchecked restart lines sit outside the conflict markers, so any resolution keeps them next to this PR's restart. Keeping both sides of the second hunk would also keep #13101's trailing exit "$restart_status".

Related report, not verified: open issue #11936 (BlueZ 5.87) reports that after systemctl restart bluetooth.service, bt-agent.service stays running with the same PID but unregistered, so pairing fails with "No agent available". This action now restarts bluetoothd on every use. Source reading and a test of the real bt-agent against a stand-in BlueZ on a private bus point the other way for a clean stop: bluetoothd sends Release, bt-agent's follow-up UnregisterAgent fails, it exits 1, and Restart=on-failure brings it back after about 2 s, provided bluetooth.service is active again by then (the unit's ExecCondition). A stale agent appeared only when the stand-in exited without sending Release (as after a crash or SIGKILL). One check on hardware would settle it: after running the menu action, journalctl --user -b -u bt-agent should show Agent released followed by a new registration.


Review information

Test scope: pinned head e1c1b1b938b2f97d662f3c9a05959fbb334b1999, compared with the previously reviewed af0c458. Source reading of the script, test, menu entry and bt-agent.service, plus BlueZ 5.87, systemd v262, kmod 34.2, util-linux, bash 5.3, Linux 7.2 and bluez-tools source. The PR suite, extra scenarios and mutants ran in an isolated sandbox with sudo, systemctl, modprobe, lsmod, rfkill and bluetoothctl stubbed and real timeout/sleep. The bt-agent check used a stand-in BlueZ service, not bluetoothd. Real hardware, a real bluetoothd, real sudo prompts and a wedged adapter were not tested.

AI process: Opus 5.5 Medium coordination and synthesis, Opus 5.5 Xhigh technical review and final fact check, GPT 6 Sol Xhigh search for related issues, Opus 5.5 Medium editorial check.

Opt out: To stop receiving these reviews, reply to this comment saying so.

A restart systemd started but could not finish has already stopped
bluetoothd, so the old message was wrong on exactly the path where the
daemon is now down. Point at systemctl status instead, matching the
branch below it.

The test that named this case could not fail: the sudo stub refuses
every call, so no modprobe could ever be logged, and the case listed a
controller, so the reload was unreachable anyway. Run it with nothing
listed and count sudo instead. A mutant that keeps escalating after the
failed restart and only exits 1 at the end passed the old suite 10/10
and now fails here.

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

Copy link
Copy Markdown
Author

Both held up again, and the bt-agent question turned out to be answerable on hardware. Pushed in 48d205c.

The restart-failure message. Replaced the second sentence with the systemctl status bluetooth.service hint the is-active branch already prints. That drops both unsafe claims at once — the "nothing changed" one you traced to systemd's stop-then-start, and "Bluetooth is unblocked", which rfkill unblock cannot promise under a hard block and the script never checked anyway. Neither was worth rescuing: on a failed restart the only useful thing to say is where to look.

The assertion that could not fail. You were right that it was decorative, and for the reason you give — the sudo stub refuses every call, so no modprobe could ever be logged, and the case listed a controller, so the reload was unreachable regardless. Took all three of your suggestions: the stub logs sudo, the case now runs with nothing listed, and it asserts exactly one call. The count is what separates the behaviours:

restart denied, no controller      sudo=1   rfkill x2, sudo systemctl restart
sudo works, no controller          sudo=3   ... + sudo modprobe -r btusb, sudo modprobe btusb

The mutant you described — keeps escalating, only exits 1 at the end — passes the previous suite 10/10 and fails this one at restart bluetooth stops before btusb when the restart failed, which is now the assertion's actual name.

#11936 and bt-agent. Ran the check you specified. Your source reading was right and the stale agent does not happen on a clean restart:

18:37:31 bt-agent[1015]:  Agent released
18:37:31 bt-agent[1015]:  unregistering agent...
18:37:31 bt-agent[1015]:  org.freedesktop.DBus.Error.ServiceUnknown
18:37:31 systemd[946]:    bt-agent.service: Main process exited, code=exited, status=1/FAILURE
18:37:33 systemd[946]:    bt-agent.service: Scheduled restart job, restart counter is at 1.
18:37:33 bt-agent[86665]: Agent registered
18:37:33 bt-agent[86665]: Default agent requested

Release arrives, UnregisterAgent fails against a bluetoothd that is already gone, the agent exits 1, and Restart=on-failure puts it back registered 2s later on a new PID — ExecCondition passes because bluetooth.service is active again by then. So the orphan in #11936 needs a bluetoothd that died without sending Release, which is what your stand-in reproduced and not what a restart does. Added to the PR description.

#13101. Thanks for spotting it — it is the narrower fix for #13078 alone, opened about 90 minutes after this one. Happy either way if a maintainer prefers that shape; I have not touched the conflict, since resolving it depends on which lands first.

Still open: a genuinely wedged AX200. I have no adapter that fails its firmware handshake, so the btusb path is still only exercised by stubs and the reporter's manual recovery in #12519.

@llstrk

llstrk commented Oct 1, 2026

Copy link
Copy Markdown

Automated AI review

Community review: Independent automated community review, unaffiliated with the Omarchy team, intended to help prepare PRs for their review.

Outcome: No new defects in 48d205c; the restart-failure and assertion findings from the previous review are resolved, and the #13101 overlap remains in narrowed form.

Details

Verified: sudo refused and a failed restart job both print the neutral message. The PR suite passes 10/10 at 48d205c and on a merge with quattro. The added sudo logging changes no other assertion.

Note, new related open PR: #13668 also changes bin/omarchy-restart-bluetooth for #13078 and adds a same-named test/shell.d/restart-bluetooth-test.sh. Merging it with this head conflicts in both files.

#13101 correction: the previous review said any conflict resolution keeps #13101's unchecked restart lines. That is too strong: a resolver can delete them by hand. Picking this PR's side of both hunks does keep them. The bluetooth service then restarts twice per run, and this PR's suite fails at restart bluetooth stops before btusb when the restart failed (2 sudo calls). #13101's own test file still passes.

Suggested change: whichever PR lands second, delete #13101's unchecked restart lines when resolving the conflict, not only the marked hunks; this PR's suite catches a leftover restart.

bt-agent: #11936's reporter saw the opposite (same PID, no re-registration), which remains unexplained. Open issue #13247 describes a related outcome of the same sequence. bt-agent's replacement starts 2 s after it exits, and its ExecCondition checks that bluetooth.service is active. If bluetooth.service is not active again within those 2 s, the unit is skipped and not retried for that session, which is what #13247's journal shows. Not tested here. After the menu action, systemctl --user show bt-agent -p Result -p ConditionResult showing Result=exec-condition would confirm it.

Optional notes:

  • The PR description says the denied-restart count "is 1 when the script stops there and 3 when it reaches the reload". With sudo refused, a script that goes on makes 2 calls: the refused modprobe -r skips the load. 3 applies only when sudo works. The test comment's "twice over" has the same imprecision.
  • The failed-restart case asserts only the prefix Could not restart bluetooth.service, so restoring the old second sentence still passes 10/10. Matching Check 'systemctl status bluetooth.service' as well would pin it.
Reproducer: sudo count in the denied-restart case

Reproducer: the PR description's sudo count for the denied-restart case

Status: PR description wording only (not a code defect). Reproduced at 48d205cfc9adb5a4a0ca46f1f1cf27884c356eb3.

The PR description says the denied-restart case "asserts exactly one call — it is 1 when the script stops there and 3 when it reaches the reload". With sudo refused on every call (as that test case sets it up), a script that keeps escalating reaches sudo modprobe -r btusb. That call is also refused, so the else branch runs and sudo modprobe btusb is never attempted: the count is 2, not 3. The count is 3 only when sudo succeeds (the author's reply table labels that row correctly). The assertion (( sudo_calls == 1 )) still fails the mutant, so the test itself is sound.

Script

Save as repro-sudo-count.sh and run bash repro-sudo-count.sh <omarchy checkout at 48d205c>. It uses the stubs from test/shell.d/restart-bluetooth-test.sh verbatim, the arguments of the case restart bluetooth stops before btusb when the restart failed (never yes yes yes), and a mutant in which the failed restart sets a flag instead of exiting and the script exits 1 only at the end.

#!/bin/bash
# Count the sudo calls the PR's denied-restart case sees for the pinned script
# and for a "keeps escalating" mutant of it (the failed restart no longer exits;
# the script exits 1 only at the end). Uses the PR test's own stubs.
# Usage: repro-sudo-count.sh <omarchy checkout at the PR head>
set -euo pipefail
repo=$1
tmp_dir=$(mktemp -d)
trap 'rm -rf "$tmp_dir"' EXIT
mkdir -p "$tmp_dir/bin"

# Stubs copied from test/shell.d/restart-bluetooth-test.sh at 48d205c.
cat >"$tmp_dir/bin/rfkill" <<'STUB'
#!/bin/bash
printf 'rfkill %s\n' "$*" >>"$STUB_CALLS"
STUB
cat >"$tmp_dir/bin/sudo" <<'STUB'
#!/bin/bash
printf 'sudo %s\n' "$*" >>"$STUB_CALLS"
[[ $STUB_SUDO_FAILS == "yes" ]] && exit 1
exec "$@"
STUB
cat >"$tmp_dir/bin/systemctl" <<'STUB'
#!/bin/bash
printf 'systemctl %s\n' "$*" >>"$STUB_CALLS"
if [[ $1 == "is-active" ]]; then
  [[ $STUB_DAEMON_ACTIVE == "yes" ]] && exit 0
  exit 3
fi
[[ $1 == "restart" && $STUB_CONTROLLER_AFTER == "restart" ]] && touch "$STUB_STATE/controller"
exit 0
STUB
cat >"$tmp_dir/bin/modprobe" <<'STUB'
#!/bin/bash
printf 'modprobe %s\n' "$*" >>"$STUB_CALLS"
[[ $1 == "-r" && $STUB_UNLOAD_FAILS == "yes" ]] && exit 1
[[ $1 == "btusb" && $STUB_CONTROLLER_AFTER == "btusb" ]] && touch "$STUB_STATE/controller"
exit 0
STUB
cat >"$tmp_dir/bin/lsmod" <<'STUB'
#!/bin/bash
[[ $STUB_BTUSB_LOADED == "yes" ]] && echo "btusb                  73728  0"
exit 0
STUB
cat >"$tmp_dir/bin/bluetoothctl" <<'STUB'
#!/bin/bash
printf 'bluetoothctl %s\n' "$*" >>"$STUB_CALLS"
if [[ $1 == "list" && -f $STUB_STATE/controller ]]; then
  echo "Controller AA:BB:CC:DD:EE:FF omarchy [default]"
fi
exit 0
STUB
chmod +x "$tmp_dir"/bin/*

cp "$repo/bin/omarchy-restart-bluetooth" "$tmp_dir/pinned"
# Mutant: the failed restart sets a flag instead of exiting; exit 1 at the end.
sed -e '/^if ! sudo systemctl restart bluetooth.service; then$/,/^fi$/ s/^  exit 1$/  restart_failed=1/' \
  "$tmp_dir/pinned" >"$tmp_dir/mutant"
printf '\nif [[ -n ${restart_failed:-} ]]; then exit 1; fi\n' >>"$tmp_dir/mutant"
chmod +x "$tmp_dir/pinned" "$tmp_dir/mutant"
echo "mutant diff:"
diff "$tmp_dir/pinned" "$tmp_dir/mutant" || true

run() { # script sudo_fails
  rm -rf "$tmp_dir/state"; mkdir -p "$tmp_dir/state"; : >"$tmp_dir/calls"
  local rc=0
  STUB_STATE="$tmp_dir/state" STUB_CALLS="$tmp_dir/calls" STUB_CONTROLLER_AFTER=never \
    STUB_BTUSB_LOADED=yes STUB_DAEMON_ACTIVE=yes STUB_SUDO_FAILS="$2" STUB_UNLOAD_FAILS=no \
    OMARCHY_BLUETOOTH_RESTART_WAIT=1 OMARCHY_BLUETOOTH_RELOAD_WAIT=1 \
    PATH="$tmp_dir/bin:$PATH" "$tmp_dir/$1" >/dev/null 2>&1 || rc=$?
  printf '%-7s sudo_fails=%-3s exit=%s sudo_calls=%s: %s\n' "$1" "$2" "$rc" \
    "$(grep -c '^sudo ' "$tmp_dir/calls" || true)" "$(grep '^sudo ' "$tmp_dir/calls" | paste -sd ';' -)"
}
run pinned yes
run mutant yes
run pinned no
run mutant no

Actual output (sandboxed run, all six commands stubbed)

mutant diff:
46c46
<   exit 1
---
>   restart_failed=1
83a84,85
> 
> if [[ -n ${restart_failed:-} ]]; then exit 1; fi
pinned  sudo_fails=yes exit=1 sudo_calls=1: sudo systemctl restart bluetooth.service
mutant  sudo_fails=yes exit=1 sudo_calls=2: sudo systemctl restart bluetooth.service;sudo modprobe -r btusb
pinned  sudo_fails=no  exit=1 sudo_calls=3: sudo systemctl restart bluetooth.service;sudo modprobe -r btusb;sudo modprobe btusb
mutant  sudo_fails=no  exit=1 sudo_calls=3: sudo systemctl restart bluetooth.service;sudo modprobe -r btusb;sudo modprobe btusb

The sudo_fails=yes rows are the test case's arrangement: 1 call for the pinned script, 2 for the mutant. The sudo_fails=no rows show where 3 comes from. Suggested wording: "it is 1 when the script stops there and 2 when it goes on to the reload (the refused modprobe -r skips the load)".


Review information

Test scope: source, merge checks and sandbox tests with privileged commands stubbed, at 48d205c; not tested on hardware.

AI process: Opus 5.5 Medium coordination and synthesis, Opus 5.5 Xhigh technical review and final fact check, GPT 6 Sol Xhigh search for related issues, Opus 5.5 Medium editorial check.

Opt out: To stop receiving these reviews, reply to this comment saying so.

The case only checked the message's prefix, so restoring the old second
sentence, which claimed nothing had changed after a restart that may
already have stopped bluetoothd, still passed 10/10. Match the full
line; that mutant now fails here.

The comment on the denied-restart case said escalating would ask for
the password twice over. With sudo refused, the refused unload skips
the load, so it asks once more, not twice.
@pedrohfp

pedrohfp commented Oct 2, 2026

Copy link
Copy Markdown
Author

Both fixed in bb009ce: the test comment no longer says "twice over", and the failed-restart case now matches the whole message, so restoring the old sentence fails the suite.

#13668 noted; this PR is the only one of the three that also covers #12519.

@greptile-apps

greptile-apps Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[High impact] The PR appears safe to merge, with one non-blocking test gap from the previous review.

Findings

  1. P2 Stalled requests go untested ▶

Summary

The PR makes omarchy-restart-bluetooth restart bluetooth.service and reload btusb when no controller appears.

  • The latest change checks and prints one controller listing, avoiding a second request after the check.
  • The test now checks that the controller listing reaches the output.
  • No new actionable issues were found.

Reviews (3) · Last reviewed commit: "Print the controller listing that was ch..." · Reviewed by Greptile

Comment thread bin/omarchy-restart-bluetooth Resolved
Comment on lines +55 to +62
cat >"$tmp_dir/bin/bluetoothctl" <<'STUB'
#!/bin/bash
printf 'bluetoothctl %s\n' "$*" >>"$STUB_CALLS"
if [[ $1 == "list" && -f $STUB_STATE/controller ]]; then
echo "Controller AA:BB:CC:DD:EE:FF omarchy [default]"
fi
exit 0
STUB

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.

P2 Stalled requests go untested

The new tests never make bluetoothctl stall or check elapsed time. Removing the probe timeout or changing the wait back to a fixed number of polls would still pass these cases, despite breaking the bounded-wait behavior. Add a stalled-command case with an outer timeout and an elapsed-time check.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

omarchybot and others added 2 commits October 15, 2026 02:50
Every other bluetoothctl call here is guarded, because bluetoothctl waits indefinitely for a bluetoothd that is not on the bus. The status listing after the final probe was not, so a daemon that went away between the two left the menu's floating terminal hanging.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The status block probed for a controller and then listed again. If bluetoothd went away between the two, the second call came back empty with status 0, so the menu reported success with no controller in it. One bounded listing, checked and printed as is, closes the hang and the empty success together.

The test now lets bluetoothd answer exactly the probes the script needs and requires the controller in the output, so a listing that hangs, times out or prints nothing all fail it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Codex Medium <noreply@openai.com>
@omarchybot omarchybot added verified Omarchy Triage has verified that this issue is ready for final review ready Good to merge and removed ready Good to merge labels Oct 15, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed at 0971ad02. I pushed one fix to the branch (two commits); the rest stands as you wrote it.

What I pushed. The status block at the end checked for a controller and then ran bluetoothctl list a second time with no timeout. If bluetoothd went away between the two calls, the second one could hang the floating terminal, or come back empty with status 0 so the menu reported success with no controller listed. It now lists once, under timeout 2s, and prints the output it checked. restart bluetooth prints the controller it checked in test/shell.d/restart-bluetooth-test.sh lets the stub answer two listings and then stall. It fails against the old status block and against a version that only wrapped the second call in timeout.

Reproduced on a worker built from the Omarchy ISO, with real systemd, bluetoothd and kernel. The worker has no Bluetooth hardware, so a virtual controller was created through hci_vhci. I checked whether the menu action actually restarts bluetoothd by comparing its PID before and after:

  • quattro's script: same PID, so bluetoothd was never restarted.
  • This head: new PID, and the controller is listed with exit 0.
  • This head with the sudo systemctl restart bluetooth.service block removed: same PID.

A virtual controller that never answers setup leaves a zombie hci0 (Opcode 0x0c03 failed: -110), the same shape as #12519. With that controller, quattro's script exits 0 and shows only the rfkill state. This head exits 1 after its 5 s wait, with the journalctl -b -k hint, and does not touch btusb, which is not loaded there.

Not reproduced: the desync in #13078 itself, and the btusb reload for #12519. Both need the reporters' hardware. The reload path is covered only by the stubs.

Tests, on the same worker. On a merge with current quattro: ./test/cli passes 125/125, and so do restart-bluetooth-test.sh (11), bluetooth-test.sh (48), bin-style-test.sh and command-operand-boundaries-test.sh. On the branch alone, ./test/cli stops at vscode generated theme references current theme file. The branch's fork point fails the same way, and current quattro passes it.

Second opinion: Codex Medium reviewed it in two rounds. In the first, it found the empty-listing case above and that the first version of the test only measured elapsed time; both are fixed in what I pushed. It also noted that a working second adapter satisfies the wait, so a wedged AX200 beside it is not reloaded. That is the gate you chose deliberately, because a forced reload drops every btusb controller's connections, so I left it for the maintainer. The second round, at this head, found nothing. Independence between the two reviews is not guaranteed.

Competing fixes: #13101 and #13668 also fix #13078, in the same lines of this script. This one is the oldest and the only one that also covers #12519. Codex Medium, asked separately which should land, chose this one for the same reasons. Choosing between them is the maintainer's call.

@codeface-io, could you try this exact commit the next time your AX200 wedges after a cold boot? It is the full head, 0971ad020e5c88b773d7c57ca058dad6c27821be:

curl -fsSL https://raw.githubusercontent.com/pedrohfp/omarchy/0971ad020e5c88b773d7c57ca058dad6c27821be/bin/omarchy-restart-bluetooth -o /tmp/omarchy-restart-bluetooth
bash /tmp/omarchy-restart-bluetooth

It should say No controller yet. Reloading btusb... and then list the controller. Please paste what it prints, and whether Bluetooth came back.

This is waiting on that confirmation and on the maintainer's choice among the three pull requests.

@omarchybot omarchybot added verified Omarchy Triage has verified that this issue is ready for final review and removed verified Omarchy Triage has verified that this issue is ready for final review labels Oct 15, 2026
@paulogeyer

Copy link
Copy Markdown
Contributor

Compared the PRs for omarchy-restart-bluetooth only running rfkill unblock and never restarting bluetooth.service (#13078, plus the wedged Intel adapter in #12519). Tested on my laptop with a stub harness (stub sudo, systemctl, bluetoothctl, lsmod, modprobe); stock quattro restarts nothing and exits 0.

PR Restarts bluetooth.service with sudo Failure exits non-zero No controller, reloads btusb
#13085 pass pass pass
#13101 pass pass fail
#13668 fail (no sudo, leaves it to polkit) pass fail

#13085 is the one to merge: it closes both #13078 and #12519, checks a controller is back, and falls back to reloading btusb. #13101 and #13668 are duplicates of it.

@omarchybot omarchybot removed the bug Something isn't working label Nov 1, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Checked again at 0971ad02, which is unchanged since my last comment and still merges cleanly into quattro.

Classification is unsettled, so I took the bug label off. I asked again because the head moved past the commit the earlier bug classification was made on. I read this as a bug fix: on quattro the menu's Bluetooth restart only runs rfkill, so it neither restarts bluetoothd (#13078) nor recovers an adapter whose firmware handshake timed out (#12519). Asked separately and given only this pull request, Codex Medium answered enhancement. It answers with one word, so its reasons are not recorded. The likely reading is that the btusb reload counts as new behaviour added alongside the restart. Until the two classifications agree, this carries no class label. The verified label stays, because it records the worker reproduction at this head, which has not moved.

Still owed here: #13101's review handed over one improvement to fold in. quattro's AGENTS.md asks for tests in the area's existing file, so the tests in test/shell.d/restart-bluetooth-test.sh should move into test/shell.d/bluetooth-test.sh, with credit to surim0n. I have not made that change yet. It waits until the classification is settled.

Nothing for you to do, @pedrohfp. This is waiting on the maintainer to settle the classification, and on the AX200 confirmation asked for above.

@omarchybot

Copy link
Copy Markdown
Collaborator

Checked the CI failure recorded against 0971ad020e5c88b773d7c57ca058dad6c27821be. The Check translation structure run records a failure, but GitHub returns no jobs or check runs for it, and its log is unavailable. I could not identify which step failed or why.

On a disposable worker built from the Omarchy ISO, the current translation checks pass on both current quattro (f989b43e) and its conflict-free merge with this head: i18n-test.sh passes 415 checks on each tree, and extract-scan --check passes on each. These check current integration; they do not reproduce or explain the historical failure.

On the unchanged PR head, restart-bluetooth-test.sh passes 11 checks and bluetooth-test.sh passes 48. ./test/cli fails at vscode generated theme references current theme file; the same assertion fails at the PR's fork point, 28ceaae7, so that failure predates this change.

Codex High checked the run metadata, source and worker results. Opus 5.5 High and muse-wonder via pi, thinking high, reviewed the follow-up and agreed that no CI defect requiring a code change was established; independence between these reviews is not guaranteed. Nothing was pushed and no GitHub job was rerun. This is waiting on the maintainer to decide how to handle the unexplained historical CI failure. The classification disagreement and AX200 confirmation remain as described above.

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

Labels

verified Omarchy Triage has verified that this issue is ready for final review

Projects

None yet

5 participants