From 92cd588a5c4e0d8b68fe43b94b02658ba7878774 Mon Sep 17 00:00:00 2001 From: Oskar Eichler Date: Wed, 2 Sep 2026 03:37:01 -0700 Subject: [PATCH] Fix Cxx WebSocket write queue state (#58201) Summary: The Cxx WebSocket client can write before its handshake, drop queued pre-connect and empty frames, allow concurrent Beast writes through a check-then-set race, and retain writer ownership after errors. Gate draining on a successful connection, atomically claim the single writer, preserve empty frames, and release state on every completion. Fixes https://github.com/react/react-native/issues/58200. ## Changelog: [GENERAL] [FIXED] - Preserve buffered and empty Cxx WebSocket frames and serialize writes. Pull Request resolved: https://github.com/react/react-native/pull/58201 Test Plan: - Exact temporary localhost Boost server harness on untouched main: `Operation canceled`, both expected frames missing, exit 1. - Fixed source harness: received the pre-connect buffered payload and empty frame, exit 0. - Actual source and harness compiled under C++20 with `-Wall -Werror`; only a local third-party Boost/Clang deprecation was demoted. - Repository clang-format and `git diff --check` passed. - Temporary harness/build wiring was removed; this PR contains one runtime file only. No UI change; screenshots are not applicable. Reviewed By: cortinico Differential Revision: D118266757 Pulled By: javache fbshipit-source-id: 2881893bdcb4c6873087e5cd821a82af76e8bbb8 --- .../http/platform/cxx/WebSocketClient.cpp | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/packages/react-native/ReactCxxPlatform/react/http/platform/cxx/WebSocketClient.cpp b/packages/react-native/ReactCxxPlatform/react/http/platform/cxx/WebSocketClient.cpp index f5a8224778b..eb236fe450a 100644 --- a/packages/react-native/ReactCxxPlatform/react/http/platform/cxx/WebSocketClient.cpp +++ b/packages/react-native/ReactCxxPlatform/react/http/platform/cxx/WebSocketClient.cpp @@ -53,6 +53,7 @@ struct WebSocketClient::Impl final : public std::enable_shared_from_this { std::mutex mutexOut_; std::queue messagesOut_; std::atomic isWriting_{false}; + std::atomic isConnected_{false}; std::atomic isClosing_{false}; }; @@ -215,6 +216,7 @@ void WebSocketClient::Impl::onHandshakeCompleted(boost::system::error_code ec) { return; } + isConnected_ = true; onConnectCallback(true, "Connected"); // Listen for any messages from the server @@ -252,26 +254,22 @@ void WebSocketClient::Impl::listen() { } void WebSocketClient::Impl::write() { - if (isClosing_) { + if (isClosing_ || !isConnected_) { return; } - if (isWriting_) { + if (isWriting_.exchange(true)) { return; } - isWriting_ = true; std::shared_ptr message; { std::lock_guard lock(mutexOut_); - if (!messagesOut_.empty()) { - message = std::make_shared(messagesOut_.front()); - messagesOut_.pop(); + if (messagesOut_.empty()) { + isWriting_ = false; + return; } - } - - if (!message || message->empty()) { - isWriting_ = false; - return; + message = std::make_shared(messagesOut_.front()); + messagesOut_.pop(); } auto ws = ws_.wlock(); @@ -281,16 +279,17 @@ void WebSocketClient::Impl::write() { boost::beast::error_code ec, std::size_t /*bytes_transferred*/) mutable { auto impl = weakImpl.lock(); - if (!impl || impl->isClosing_) { + if (!impl) { + return; + } + impl->isWriting_ = false; + if (impl->isClosing_) { return; } if (ec) { LOG(ERROR) << "Error writing to websocket: " << ec.message(); - return; } - impl->isWriting_ = false; impl->write(); - message.reset(); // Release the message after it's been sent }); }