Fix race condition in WebSocketClient connection handling#781
Fix race condition in WebSocketClient connection handling#781sampepose wants to merge 1 commit intoGetStream:developfrom
Conversation
|
Hey, I didn't see a CONTRIBUTORS file...hope it's ok to send this bug fix in for consideration! |
|
@martinmitrevski @ipavlidakis is there a process to get this reviewed? |
|
Hi @sampepose, Thank you for this PR. I would agree with you that this part is racey and requires improvement. However, may I suggest something around the following: log.debug("Listening for WS connection")
let subject = PassthroughSubject<Void, Error>()
webSocketClient?.onConnected = { subject.send(()) }
try await subject.nextValue(timeout: 30)
log.debug("WS connected")This one has the benefit of appearing simpler, being more readable, reusing logic that already exists in the SDK and avoids having many small rigid Best regards, |
|
Good idea @ipavlidakis! I made those changes. |
|
Hey @sampepose, It has been a long time since this PR was open and we have changed/improving things in the meantime. I'll close this one but feel free to check the latest version of the SDK and let us know if you find issues with it. Best regards, |
🔗 Issue Links
Provide all JIRA tickets and/or GitHub issues related to this PR, if applicable.
🎯 Goal
The current implementation uses shared mutable state (
connectedandtimeoutflags) that can be accessed concurrently from different threads, causing a data race when the WebSocket connects while the main task is checking connection status.This can be seen from the logs:
📝 Summary
I've refactored the connection handling to use Swift's modern concurrency features to eliminate the data race while maintaining the same timeout and connection logic behavior.
🛠 Implementation
🧪 Manual Testing Notes
See no crashes or ThreadSanitizer complaints in either case.
☑️ Contributor Checklist