From a450f47f6044722a0b211f881ddc4dd822e9fc23 Mon Sep 17 00:00:00 2001 From: Sam Pepose Date: Sat, 7 Jun 2025 16:13:15 -0400 Subject: [PATCH] Fix race condition in WebSocketClient connection handling --- Sources/StreamVideo/StreamVideo.swift | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/Sources/StreamVideo/StreamVideo.swift b/Sources/StreamVideo/StreamVideo.swift index 6777f38e2..2e26ed9a5 100644 --- a/Sources/StreamVideo/StreamVideo.swift +++ b/Sources/StreamVideo/StreamVideo.swift @@ -488,24 +488,13 @@ public class StreamVideo: ObservableObject, @unchecked Sendable { } else { throw ClientError.Unknown() } - var connected = false - var timeout = false - let control = DefaultTimer.schedule(timeInterval: 30, queue: .sdk) { - timeout = true - } log.debug("Listening for WS connection") - webSocketClient?.onConnected = { - control.cancel() - connected = true + let subject = PassthroughSubject() + webSocketClient?.onConnected = { subject.send(()) } + do { + try await subject.nextValue(timeout: 30) log.debug("WS connected") - } - - while (!connected && !timeout) { - try await Task.sleep(nanoseconds: 100_000) - } - - if timeout { - log.debug("Timeout while waiting for WS connection opening") + } catch { throw ClientError.NetworkError() } }