Skip to content

Drop a Bluetooth power change that lands mid-transition - #10320

Closed
dontm1nd wants to merge 2 commits into
omacom:quattrofrom
dontm1nd:fix/bluetooth-power-race
Closed

dontm1nd wants to merge 2 commits into
omacom:quattrofrom
dontm1nd:fix/bluetooth-power-race

Conversation

@dontm1nd

@dontm1nd dontm1nd commented Sep 5, 2026

Copy link
Copy Markdown

The problem

omarchy-bluetooth-power has no interlock, so a second call can land while the first is still bringing the adapter up. The block that arrives in that window is not a wasted click — it takes the USB transport down mid-setup, and HCI commands fail against a controller that is no longer there.

Reproduced here by driving the helper the way two quick clicks on the bar toggle would:

omarchy-bluetooth-power off
sleep 3
omarchy-bluetooth-power on     # returns at Powered: yes, setup still running
omarchy-bluetooth-power off    # lands mid-setup
Bluetooth: hci0: Device setup in 2012942 usecs
Bluetooth: hci0: Opcode 0x0c1a failed: -71
Bluetooth: hci0: Error when powering off device on rfkill (-71)

Nothing retries the failed commands. The adapter comes back Powered: yes and reads as healthy afterwards, so there is no sign in the UI that anything went wrong.

Bonded LE devices take noticeably longer to find their way back after a cycle that hit this than after a clean one, though I only have single runs of each and touching an LE peripheral speeds its reconnect up, so I am claiming nothing firm beyond the failed commands themselves.

The fix

Serialize the verbs that move the block behind a non-blocking flock, following the pattern omarchy-brightness-display already uses to drop overlapping key events, and hold the lock past Powered: yes when the call is what brought the adapter up.

  • Dropped rather than queued: a click the user never saw land is not worth replaying seconds later against a radio whose state has moved on since.
  • is-on deliberately stays outside the lock. It answers through its exit status, so a dropped call exiting 0 would report a blocked radio as powered.
  • An adapter that was already on has no setup running and does not wait, so omarchy-bluetooth-power on stays as responsive as it is today in the common case.

The interlock lives in the helper rather than in the bar widget so that every caller is covered — the panel toggle, the omarchy.bluetooth IPC method, and the CLI alike.

Trade-off worth a second opinion

The settle is a fixed wait (OMARCHY_BLUETOOTH_POWER_SETTLE_SECONDS, default 5) because I could not find a cheap unprivileged signal for "the adapter is done coming up" — Powered flips well before setup finishes. The cost is that a real power-up from the CLI takes about five seconds longer to return. Happy to shorten it, or drop it in favour of the lock alone, if you would rather not carry a sleep here.

Testing

./test/shell and ./test/cli pass. Four cases added to test/shell.d/bluetooth-test.sh on the existing mocked rfkill/bluetoothctl harness: a second off and a second on arriving under a held lock touch nothing, is-on still answers correctly under one, and an already-powered adapter does not sit out the settle.

Verified on Omarchy 4.0.2, ThinkPad with a MediaTek 0e8d:e111 controller.

omarchy-bluetooth-power has no interlock, so a second call can land while
the first is still bringing the adapter up. The block that arrives in
that window takes the USB transport down mid-setup, and the commands
still in flight fail against a controller that is no longer there:

  Bluetooth: hci0: Device setup in 2012942 usecs
  Bluetooth: hci0: Opcode 0x0c1a failed: -71
  Bluetooth: hci0: Error when powering off device on rfkill (-71)

Nothing retries them, and the adapter comes back Powered: yes and reads
as healthy either way, so nothing surfaces the radio having been left
half-configured. Two quick clicks on the bar toggle reach it.

Serialize the verbs that move the block behind a non-blocking flock, the
way the brightness keys already drop overlapping events, and hold the
lock past Powered: yes when the call is what powered the adapter up. An
adapter that was already on has no setup running, so it does not wait.
is-on stays outside the lock: it answers through its exit status, and a
dropped call exiting 0 would report a blocked radio as powered.

Keeping the interlock in the helper rather than the bar widget covers
every caller — the panel toggle, the omarchy.bluetooth IPC method, and
the CLI alike.
@dontm1nd
dontm1nd force-pushed the fix/bluetooth-power-race branch from 74bd91a to 91a3b88 Compare September 5, 2026 13:42
@omarchybot omarchybot added the bug Something isn't working label Sep 27, 2026
The script now locks under $XDG_RUNTIME_DIR, so the tests took the real session's lock: a toggle settling on the machine running the suite would make the mocked calls drop and the "already powered" check pass without running.

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

Copy link
Copy Markdown
Collaborator

Reviewed at 91a3b88, then pushed one small test fix (a2912ad). This is waiting on the maintainer: the failure needs hardware we cannot run, and the trade-off you raised is theirs to make.

What was checked. On a disposable Omarchy VM, ./test/cli passed and test/shell.d/bluetooth-test.sh passed 53 of 53 at the head. bluetooth-migration-test.sh passed too. Your new tests run against the unpatched quattro script fail on "drops an off that lands while another call is still in flight", so the interlock does what it says. The VM has no Bluetooth controller, so the -71 failure itself was not reproduced. Your log from the MediaTek 0e8d:e111 is the only evidence of it, and a fix for a hardware-specific failure needs confirming on a machine other than the author's before it can be called verified.

Pushed. test/shell.d/bluetooth-test.sh now exports XDG_RUNTIME_DIR to its temp dir. Without it the tests took the real session's omarchy-bluetooth-power.lock. On a machine whose Bluetooth was settling, the mocked calls would then be dropped, and "does not sit out the settle when the adapter was already on" would pass without running anything.

For the maintainer to weigh, none of it a defect in the code as written:

  • A dropped call is silent. Turn Bluetooth on from the panel, then click off within the settle (about 5 s after Powered: yes, longer if the probes are slow). The off exits 0, the radio stays on, and the panel shows on with no sign the click was refused, so it has to be clicked again. That is the drop-versus-queue choice the description raises.
  • The 5 s settle is not tied to any signal. In the kernel, Powered is published only after hci_powered_update_sync() has restored most LE state. bluetoothd's adapter_start() work comes after it, though, so a window after Powered: yes is plausible. Five seconds is still a guess at its length.
  • omarchy-bluetooth-device (connect, pair, forget on an adapter that was off) now takes about 5 s longer before it talks to the device. If another call holds the lock, its power on returns 0 at once without powering anything.
  • This collides with Remember Bluetooth power and restore it after login #13667 (adds a restore verb that would sit outside the lock) and fix(bluetooth): allow Chromium to power on for phone passkeys #13867 (rewrites this script to use Powered=false instead of an rfkill block). Whichever lands second needs reworking.

Second opinion: Codex Medium reviewed the diff and the callers. It found the test isolation problem fixed above, and the kernel/BlueZ ordering point. On the silent drop it agreed with what I had already written down, so that agreement is not independent.

@greptile-apps

greptile-apps Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Medium risk] Adds serialization to Bluetooth power state changes.

The PR is not yet safe to merge because a contended device power-on can be dropped while pairing or connecting continues.

Findings

  1. P1 Dropped power-on breaks device actions ▶
  2. P2 Settle lock remains untested ▶

Summary

The PR adds a nonblocking lock to Bluetooth power-changing verbs and holds it briefly after a power-up, while leaving is-on ungated. It also adds mock-based tests for contention and the already-powered path.

  • A device pair/connect request can proceed after its power-on request is dropped.
  • The tests do not exercise lock retention during a real, nonzero settle.

Reviews (1) · Last reviewed commit: "Keep the Bluetooth tests off the session..."

# command itself the failing redirection is processed before the 2>/dev/null
# that was meant to cover it.
{ exec {lock_fd}>"${XDG_RUNTIME_DIR:-/tmp}/omarchy-bluetooth-power.lock"; } 2>/dev/null || return 0
flock -n "$lock_fd" || exit 0

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.

P1 Dropped power-on breaks device actions When omarchy-bluetooth-device tries to pair or connect while another call is powering up the radio, it can see Powered: no and request on during the settle window. This line exits successfully without powering up or waiting. The device command then proceeds before the adapter is ready and suppresses failures from bluetoothctl, so pairing or connecting can fail silently. The device path needs to wait for or verify power-up rather than treat a dropped on as complete.

Comment on lines +295 to +299
exec {held_fd}>"${XDG_RUNTIME_DIR:-/tmp}/omarchy-bluetooth-power.lock"
flock "$held_fd"
PATH="$mock_bin:$ROOT/bin:$PATH" BLUETOOTHCTL_LOG="$device_tmp/log" \
OMARCHY_BLUETOOTH_POWER_WAIT_SECONDS=0 OMARCHY_BLUETOOTH_POWER_SETTLE_SECONDS=0 \
"$ROOT/bin/omarchy-bluetooth-power" "$1"

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 Settle lock remains untested These contention tests hold the lock externally and set the settle time to zero. The only test with a nonzero settle starts with the adapter already powered, so it checks that the wait is skipped. No test checks that a call which powers up the adapter keeps its lock through a nonzero settle; the timing bug this PR aims to prevent could return while the tests still pass. Please test a second power request during an actual settle.

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!

@dontm1nd dontm1nd closed this by deleting the head repository Nov 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants