From 708a5e33cd07cc636bfd01fd1f9e6cbf71c57367 Mon Sep 17 00:00:00 2001 From: jack Date: Mon, 24 Nov 2025 11:37:27 -1000 Subject: [PATCH] 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. --- bitchat/Services/BLE/BLEService.swift | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bitchat/Services/BLE/BLEService.swift b/bitchat/Services/BLE/BLEService.swift index 78df3254..50a170ff 100644 --- a/bitchat/Services/BLE/BLEService.swift +++ b/bitchat/Services/BLE/BLEService.swift @@ -521,8 +521,11 @@ final class BLEService: NSObject { } } - // Give leave message a moment to send - Thread.sleep(forTimeInterval: TransportConfig.bleThreadSleepWriteShortDelaySeconds) + // Give leave message a moment to send (cooperative delay allows BLE callbacks to fire) + let deadline = Date().addingTimeInterval(TransportConfig.bleThreadSleepWriteShortDelaySeconds) + while Date() < deadline { + RunLoop.current.run(until: Date().addingTimeInterval(0.01)) + } // Clear pending notifications collectionsQueue.sync(flags: .barrier) {