



The bug only a restart could fix, so my app restarts itself
After macOS's Bluetooth daemon crashed, my menu bar app kept asking it to pair a device at address 00:00:00:00:00:00, and the only cure was a fresh process.
In November, Monnect stopped working in one direction. The Magic Mouse and Keyboard were on my work Mac, and pulling them to my personal Mac did nothing. The menu said it was pairing. The log said pairing had finished. The devices stayed where they were.
A pairing request for nobody
The system log was stranger. bluetoothd, the macOS daemon that owns Bluetooth, had crashed on wake earlier (macOS 27.0.1) and come back up. At that moment the devices were not paired to this Mac, because they were on the other one. Monnect had been running the whole time.
Every time Monnect asked IOBluetooth to pair the mouse, the daemon got a request for address 00:00:00:00:00:00. It reported the pairing as finished with status 0, which reads as success. It paired nothing and crashed again.
Here is what had happened. The device objects inside my long running process belonged to the old daemon. The new daemon had no record of devices that were not paired with it, so those objects pointed at nothing. Devices paired to this Mac were fine, because their pairing record survives a restart. Only the unpaired ones went stale, and IOBluetooth gave me no way to refresh them. Quitting Monnect and opening it again fixed it at once.
Noticing the restart
So the fix had two halves: notice that bluetoothd is no longer the daemon I started with, and get a fresh process when that matters.
For the first, I read the process table with sysctl and find bluetoothd by name. A pid alone is not enough, since pids get reused, so I keep the start time too.
struct BluetoothDaemon: Equatable {
let pid: pid_t
let startedSec: Int
let startedUsec: Int32
}The backend records this at launch. Before pairing a device that is not paired here, the switch engine compares that record with the daemon running now. If it is the same daemon, nothing changes. If it is a different one, the device objects can't be trusted.
An app that relaunches itself
I did not expect to write the second half. Monnect saves the switch in progress to a small resume.json: which devices are still wanted, and whether it had already stopped trying quickly and moved on to waiting. Then it starts a detached /bin/sh helper and quits. The helper waits until Monnect's process is gone, then runs open -g on the app. The new instance reads the resume file at launch, deletes it, and carries on with the switch.
Two guards keep this from going wrong. A resume file older than 60 seconds is ignored, so a leftover file can't start a surprise switch later. And Monnect relaunches at most once every 10 minutes, so a daemon that keeps crashing can't trap it in a restart loop. When a relaunch is blocked, it just carries on the old way.
To test this on real hardware without waiting for macOS to crash, I added an environment variable that makes the app pretend the daemon has changed. The first run failed. The relaunched copy inherited the variable through open, thought the daemon had restarted again, and hit the 10 minute limit. The helper now removes the variable before it relaunches.
On the two real Macs, the relaunched copy resumed the switch in 0.5 seconds, both devices connected within 3 seconds, and the daemon did not crash again. It shipped as Monnect 0.2.1.
What I'm keeping from this: when a system framework holds onto state you can't reach, restarting cleanly is a fine fix, not giving up. It just has to carry its work across and know when to stop.