From 9b1edf26e647299f312bd9344f191b2fd1226523 Mon Sep 17 00:00:00 2001 From: "yury.niakhai" Date: Fri, 9 Feb 2024 11:45:42 +0200 Subject: [PATCH] fix deadlock on main thread --- Sources/SignalRClient/HttpConnection.swift | 27 ++++++++++++---------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/Sources/SignalRClient/HttpConnection.swift b/Sources/SignalRClient/HttpConnection.swift index 1dcbc92..2af4eca 100755 --- a/Sources/SignalRClient/HttpConnection.swift +++ b/Sources/SignalRClient/HttpConnection.swift @@ -215,19 +215,22 @@ public class HttpConnection: Connection { logger.log(logLevel: .warning, message: "Connection not yet started") return } - - self.startDispatchGroup.wait() - // The transport can be nil if connection was stopped immediately after starting - // or failed to start. In this case we need to call connectionDidClose ourselves. - if let t = transport { - self.stopError = stopError - t.close() - } else { - logger.log(logLevel: .debug, message: "Connection being stopped before transport initialized") - logger.log(logLevel: .debug, message: "Invoking connectionDidClose (\(#function): \(#line))") - options.callbackQueue.async { - self.delegate?.connectionDidClose(error: stopError) + self.startDispatchGroup.notify(queue: DispatchQueue.main) { [weak self] in + guard let self else { + return + } + // The transport can be nil if connection was stopped immediately after starting + // or failed to start. In this case we need to call connectionDidClose ourselves. + if let t = transport { + self.stopError = stopError + t.close() + } else { + logger.log(logLevel: .debug, message: "Connection being stopped before transport initialized") + logger.log(logLevel: .debug, message: "Invoking connectionDidClose (\(#function): \(#line))") + options.callbackQueue.async { + self.delegate?.connectionDidClose(error: stopError) + } } } }