Fix race condition in WebSocketClient connection handling#781
Open
sampepose wants to merge 1 commit intoGetStream:developfrom
Open
Fix race condition in WebSocketClient connection handling#781sampepose wants to merge 1 commit intoGetStream:developfrom
sampepose wants to merge 1 commit intoGetStream:developfrom
Conversation
Author
|
Hey, I didn't see a CONTRIBUTORS file...hope it's ok to send this bug fix in for consideration! |
Author
|
@martinmitrevski @ipavlidakis is there a process to get this reviewed? |
Contributor
|
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, |
Author
|
Good idea @ipavlidakis! I made those changes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔗 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