Skip to content

Sync access to web socket continuations #633

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
Mar 17, 2025
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .nanpa/socket-continuation.kdl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
patch type="fixed" "Race condition in WebSocket impl"
71 changes: 45 additions & 26 deletions Sources/LiveKit/Support/WebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ class WebSocket: NSObject, Loggable, AsyncSequence, URLSessionWebSocketDelegate
typealias AsyncIterator = WebSocketStream.Iterator
typealias Element = URLSessionWebSocketTask.Message

private var streamContinuation: WebSocketStream.Continuation?
private var connectContinuation: CheckedContinuation<Void, Error>?
private let _state = StateSync(State())

private struct State {
var streamContinuation: WebSocketStream.Continuation?
var connectContinuation: CheckedContinuation<Void, Error>?
}

private let request: URLRequest

Expand All @@ -44,7 +48,9 @@ class WebSocket: NSObject, Loggable, AsyncSequence, URLSessionWebSocketDelegate
private lazy var task: URLSessionWebSocketTask = urlSession.webSocketTask(with: request)

private lazy var stream: WebSocketStream = WebSocketStream { continuation in
streamContinuation = continuation
_state.mutate { state in
state.streamContinuation = continuation
}
waitForNextValue()
}

Expand All @@ -55,7 +61,9 @@ class WebSocket: NSObject, Loggable, AsyncSequence, URLSessionWebSocketDelegate
super.init()
try await withTaskCancellationHandler {
try await withCheckedThrowingContinuation { continuation in
connectContinuation = continuation
_state.mutate { state in
state.connectContinuation = continuation
}
task.resume()
}
} onCancel: {
Expand All @@ -70,10 +78,13 @@ class WebSocket: NSObject, Loggable, AsyncSequence, URLSessionWebSocketDelegate

func close() {
task.cancel(with: .normalClosure, reason: nil)
connectContinuation?.resume(throwing: LiveKitError(.cancelled))
connectContinuation = nil
streamContinuation?.finish(throwing: LiveKitError(.cancelled))
streamContinuation = nil

_state.mutate { state in
state.connectContinuation?.resume(throwing: LiveKitError(.cancelled))
state.connectContinuation = nil
state.streamContinuation?.finish(throwing: LiveKitError(.cancelled))
state.streamContinuation = nil
}
}

// MARK: - AsyncSequence
Expand All @@ -84,23 +95,27 @@ class WebSocket: NSObject, Loggable, AsyncSequence, URLSessionWebSocketDelegate

private func waitForNextValue() {
guard task.closeCode == .invalid else {
streamContinuation?.finish(throwing: LiveKitError(.invalidState))
streamContinuation = nil
_state.mutate { state in
state.streamContinuation?.finish(throwing: LiveKitError(.invalidState))
state.streamContinuation = nil
}
return
}

task.receive(completionHandler: { [weak self] result in
guard let continuation = self?.streamContinuation else {
guard let self, let continuation = self._state.streamContinuation else {
return
}

do {
let message = try result.get()
continuation.yield(message)
self?.waitForNextValue()
self.waitForNextValue()
} catch {
continuation.finish(throwing: LiveKitError.from(error: error))
self?.streamContinuation = nil
self._state.mutate { state in
state.streamContinuation?.finish(throwing: LiveKitError.from(error: error))
state.streamContinuation = nil
}
}
})
}
Expand All @@ -115,23 +130,27 @@ class WebSocket: NSObject, Loggable, AsyncSequence, URLSessionWebSocketDelegate
// MARK: - URLSessionWebSocketDelegate

func urlSession(_: URLSession, webSocketTask _: URLSessionWebSocketTask, didOpenWithProtocol _: String?) {
connectContinuation?.resume()
connectContinuation = nil
_state.mutate { state in
state.connectContinuation?.resume()
state.connectContinuation = nil
}
}

func urlSession(_: URLSession, task _: URLSessionTask, didCompleteWithError error: Error?) {
log("didCompleteWithError: \(String(describing: error))", error != nil ? .error : .debug)

if let error {
let lkError = LiveKitError.from(error: error) ?? LiveKitError(.unknown)
connectContinuation?.resume(throwing: lkError)
streamContinuation?.finish(throwing: lkError)
} else {
connectContinuation?.resume()
streamContinuation?.finish()
}
_state.mutate { state in
if let error {
let lkError = LiveKitError.from(error: error) ?? LiveKitError(.unknown)
state.connectContinuation?.resume(throwing: lkError)
state.streamContinuation?.finish(throwing: lkError)
} else {
state.connectContinuation?.resume()
state.streamContinuation?.finish()
}

connectContinuation = nil
streamContinuation = nil
state.connectContinuation = nil
state.streamContinuation = nil
}
}
}
Loading