Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IXWebSocketTransport::setReadyState(): Run under lock
When setReadyState(CLOSED) is executed from two different threads (the server's thread, detecting a close trying to receive and a separate sending thread), there is a race where both see _readyState as non-CLOSED and both execute the _onCloseCallback(). Worse, the server's thread might be returning from ->run(), unsetting the callback, while the other thread is still about to call the _onCloseCallback(), resulting in a crash rather than "just" a duplicate invocation. This change ensures that setReadyState() *and* the_onCloseCallback() are executed by a single thread till completion. I was tempted to remove the std::atomic<> for _readyState, but left it assuming it's still good having for getReadyState(). I've first tried _readyState.exchange() which seemed promising, but still crashed once in a while as racing threads do not wait for the _onCloseCallback() to complete.
- Loading branch information