Fix data race on WebSocketClient::_ctx (concurrent disconnect vs send/isConnected)#6
Open
voicetel wants to merge 1 commit into
Open
Fix data race on WebSocketClient::_ctx (concurrent disconnect vs send/isConnected)#6voicetel wants to merge 1 commit into
voicetel wants to merge 1 commit into
Conversation
isConnected()/sendBinary()/sendMessage() (callable from the media thread) read _ctx while connect()/disconnect() write it from another thread, with no synchronization -- a data race (ThreadSanitizer-confirmed) on the shared_ptr. Use C++11 atomic shared_ptr ops for every _ctx access; disconnect() atomically swaps _ctx out and calls stop() (the event-thread join) on the local copy, outside the swap, so it cannot deadlock a callback thread.
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.
Problem
WebSocketClient::_ctx(thestd::shared_ptr<WebSocketContext>) is read andwritten from different threads with no synchronization:
isConnected(),sendBinary(),sendMessage(), and thesetXxxCallback()setters — these are typically called from an application'smedia/sender thread;
connect()(_ctx = ctx) anddisconnect()(_ctx.reset()),called from the teardown/control thread.
This is a data race on the shared_ptr itself. It is easy to hit when a sender
thread is active while another thread tears the client down (e.g. a connection
that disconnects while audio is still being sent), and ThreadSanitizer reports it:
Fix
Make every
_ctxaccess atomic using the C++11std::atomic_load/std::atomic_store/std::atomic_exchangefree functions forshared_ptr.disconnect()atomically swaps_ctxout and then callsstop()(which joinsthe event thread) on the local copy, outside the atomic op — so a concurrent
sender either observes the old context (kept alive by its own shared_ptr copy)
or null, never a torn pointer, and the join can't deadlock a callback thread.
Notes:
<atomic>-on-shared_ptr member needed), so the class's existingmove semantics and public API are unchanged.
Verification
Built the transport standalone under ThreadSanitizer and drove it against a mock
server through a lifecycle matrix (connect / send / far-end drop / rapid restart /
reconnect-churn / disconnect-while-sending). Before: the
_ctxrace above isreported; after: clean.