Skip to content

fix(api): fix subscription failures #3998

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

Merged
merged 2 commits into from
Jun 12, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,13 @@ extension WebSocketClient: URLSessionWebSocketDelegate {

let nsError = error as NSError
switch (nsError.domain, nsError.code) {
case (NSURLErrorDomain.self, NSURLErrorNetworkConnectionLost), // connection lost
(NSPOSIXErrorDomain.self, Int(ECONNABORTED)): // background to foreground
case (NSURLErrorDomain.self, NSURLErrorNetworkConnectionLost),
(NSURLErrorDomain.self, NSURLErrorCannotConnectToHost),
(NSURLErrorDomain.self, NSURLErrorTimedOut),
(NSURLErrorDomain.self, NSURLErrorNotConnectedToInternet),
(NSURLErrorDomain.self, NSURLErrorDataNotAllowed),
(NSPOSIXErrorDomain.self, Int(ECONNABORTED)),
(NSPOSIXErrorDomain.self, 57):
self.subject.send(.error(WebSocketClient.Error.connectionLost))
Task { [weak self] in
await self?.networkMonitor.updateState(.offline)
Expand Down Expand Up @@ -283,11 +288,11 @@ extension WebSocketClient {
}

switch stateChange {
case (.online, .offline):
log.debug("[WebSocketClient] NetworkMonitor - Device went offline")
case (.online, .offline), (.none, .offline), (.online, .none):
log.debug("[WebSocketClient] NetworkMonitor - Device went offline or network status became unknown")
self.connection?.cancel(with: .invalid, reason: nil)
self.subject.send(.disconnected(.invalid, nil))
case (.offline, .online):
case (.offline, .online), (.none, .online):
log.debug("[WebSocketClient] NetworkMonitor - Device back online")
await self.createConnectionAndRead()
default:
Expand Down Expand Up @@ -335,13 +340,19 @@ extension WebSocketClient {
}

switch closeCode {
case .internalServerError:
case .internalServerError,
.abnormalClosure,
.invalid,
.policyViolation:
log.debug("[WebSocketClient] Retrying on closeCode: \(closeCode)")
let delayInMs = await retryWithJitter.next()
Task { [weak self] in
try await Task.sleep(nanoseconds: UInt64(delayInMs) * 1_000_000)
await self?.createConnectionAndRead()
}
default: break
default:
log.debug("[WebSocketClient] Not retrying for closeCode: \(closeCode)")
break
}

}
Expand Down
Loading