Skip to content

Commit 489c6f9

Browse files
committed
Make sure to finish the events if the task never got started
If we don't have a network connection but start a task, the task stays closed. We still need to finish the events then though.
1 parent 32a6eaa commit 489c6f9

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

Sources/StructuredWebSocketClient/URLSession+MessageTransport.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ public actor URLSessionWebSocketTransport: MessageTransport, SimpleURLSessionTas
9393
Task { await self.onClose(closeCode: closeCode, reason: reason) }
9494
return
9595
}
96-
self.wsTask.cancel(with: closeCode, reason: reason)
96+
self.wsTask.cancel(with: closeCode, reason: reason) // sends close frame
97+
// although if the socket never opened, we still need to close the events… 🤦‍♂️
98+
Task { await self.onClose(closeCode: closeCode, reason: reason) }
9799
}
98100

99101
public nonisolated func connect() -> AsyncChannel<WebSocketEvent> {
@@ -126,6 +128,7 @@ public actor URLSessionWebSocketTransport: MessageTransport, SimpleURLSessionTas
126128

127129
private func onClose(closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?) async {
128130
guard !self.isAlreadyClosed else {
131+
logger.warning("⚠️ self.isAlreadyClosed in \(#function)")
129132
// Due to delegate callbacks, we can get here more than once. Don't send multiple
130133
// disconnect events or log multiple closures.
131134
return

0 commit comments

Comments
 (0)