Fix race condition during high CPU load in WebSocket::stop - #609
Open
cosminpolifronie wants to merge 1 commit into
Open
cosminpolifronie wants to merge 1 commit into
cosminpolifronie wants to merge 1 commit into
Conversation
…could trigger an automatic reconnect => _thread.join() blocks indefinitely
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.
We have found the bug below when using the library in production during high CPU load.
The fix has been tested and successfully eliminates the indefinite hang.
Could this please be merged and released? Thanks a lot!
Bug: if _stop is set only after close(), there is a window where close() has already driven the connection to CLOSED (e.g. a fast close round-trip, or the 300ms forced-close fallback) while _stop is still false. checkConnection()'s loop only bails out on isConnected() / isClosing() / _stop; if none of those are true yet and _automaticReconnection is enabled (the default), it will immediately reconnect - opening a brand new OPEN session that this call never asked to close. The run() thread then goes back into a blocking poll() on that new session, and _stop=true (set afterwards) is never observed by it in time: notify_one() only wakes the reconnection back-off wait, not the blocking poll() (which is only woken by the socket's own SelectInterrupt, and nothing triggers that again after this stop() call already used its one close()/wakeUpFromPoll). The result is that the join() below blocks forever.
Setting _stop = true first closes this race unconditionally: checkConnection()'s very first condition (isConnected() || isClosing() || _stop) will now always observe _stop == true before it could ever call connect() again, regardless of how the two threads interleave. The graceful close handshake performed by close() below is unaffected by this reordering (_stop is a WebSocket-level flag; the close/poll/dispatch machinery only look at the WebSocketTransport's own state).