Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deadlock on main thread #294

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions Sources/SignalRClient/HttpConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Expand Down