Replace Thread.sleep with cooperative RunLoop delay in BLEService

Replace blocking Thread.sleep in stopServices() with a cooperative
RunLoop-based delay. This allows BLE callbacks to fire during the
shutdown delay, improving reliability of leave message transmission.

The delay is needed to give the leave message time to transmit before
disconnecting peers and stopping the BLE stack.
This commit is contained in:
jack
2025-11-24 11:37:27 -10:00
parent ce33132a0f
commit 708a5e33cd
+5 -2
View File
@@ -521,8 +521,11 @@ final class BLEService: NSObject {
} }
} }
// Give leave message a moment to send // Give leave message a moment to send (cooperative delay allows BLE callbacks to fire)
Thread.sleep(forTimeInterval: TransportConfig.bleThreadSleepWriteShortDelaySeconds) let deadline = Date().addingTimeInterval(TransportConfig.bleThreadSleepWriteShortDelaySeconds)
while Date() < deadline {
RunLoop.current.run(until: Date().addingTimeInterval(0.01))
}
// Clear pending notifications // Clear pending notifications
collectionsQueue.sync(flags: .barrier) { collectionsQueue.sync(flags: .barrier) {