Repository navigation
Conversation
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.
74bd91a to
91a3b88
Compare
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>
|
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, Pushed. For the maintainer to weigh, none of it a defect in the code as written:
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. |
|
| # 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 |
There was a problem hiding this comment.
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.
| 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" |
There was a problem hiding this comment.
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!
The problem
omarchy-bluetooth-powerhas 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:
Nothing retries the failed commands. The adapter comes back
Powered: yesand 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 patternomarchy-brightness-displayalready uses to drop overlapping key events, and hold the lock pastPowered: yeswhen the call is what brought the adapter up.is-ondeliberately stays outside the lock. It answers through its exit status, so a dropped call exiting 0 would report a blocked radio as powered.omarchy-bluetooth-power onstays 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.bluetoothIPC 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" —Poweredflips 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/shelland./test/clipass. Four cases added totest/shell.d/bluetooth-test.shon the existing mockedrfkill/bluetoothctlharness: a secondoffand a secondonarriving under a held lock touch nothing,is-onstill 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.