From cf78eec459b9085d923b337ab7ed4b567eb764b5 Mon Sep 17 00:00:00 2001 From: Ryan Lamb <4955475+kinyoklion@users.noreply.github.com> Date: Tue, 17 Nov 2026 20:15:23 +0000 Subject: [PATCH] fix: Restart the stream when it sends invalid flag data Generated-by: termag --- .../data_sources/streaming_data_source.cpp | 15 +- .../tests/streaming_data_source_test.cpp | 230 ++++++++++++++++++ libs/server-sent-events/src/curl_client.hpp | 10 +- 3 files changed, 249 insertions(+), 6 deletions(-) create mode 100644 libs/client-sdk/tests/streaming_data_source_test.cpp diff --git a/libs/client-sdk/src/data_sources/streaming_data_source.cpp b/libs/client-sdk/src/data_sources/streaming_data_source.cpp index 83494a6a0..da73ab5d7 100644 --- a/libs/client-sdk/src/data_sources/streaming_data_source.cpp +++ b/libs/client-sdk/src/data_sources/streaming_data_source.cpp @@ -129,10 +129,17 @@ void StreamingDataSource::Start() { client_builder.receiver([weak_self](launchdarkly::sse::Event const& event) { if (auto self = weak_self.lock()) { - self->data_source_handler_.HandleMessage(event.type(), - event.data()); - // TODO: Use the result of handle message to restart the - // event source if we got bad data. sc-204387 + auto const status = self->data_source_handler_.HandleMessage( + event.type(), event.data()); + if (status == + DataSourceEventHandler::MessageStatus::kInvalidMessage) { + LD_LOG(self->logger_, LogLevel::kWarn) + << "Received invalid data from stream, restarting " + "connection"; + if (self->client_) { + self->client_->async_restart("invalid data in stream"); + } + } } }); diff --git a/libs/client-sdk/tests/streaming_data_source_test.cpp b/libs/client-sdk/tests/streaming_data_source_test.cpp new file mode 100644 index 000000000..da586aabf --- /dev/null +++ b/libs/client-sdk/tests/streaming_data_source_test.cpp @@ -0,0 +1,230 @@ +#include + +#include "data_sources/data_source_status_manager.hpp" +#include "data_sources/data_source_update_sink.hpp" +#include "data_sources/streaming_data_source.hpp" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace launchdarkly; +using namespace launchdarkly::client_side; +using namespace launchdarkly::client_side::data_sources; +using namespace std::chrono_literals; + +using config::shared::ClientSDK; + +namespace beast = boost::beast; +namespace http = beast::http; +using tcp = boost::asio::ip::tcp; + +namespace { + +Logger MakeNullLogger() { + struct NullBackend : ILogBackend { + bool Enabled(LogLevel) noexcept override { return false; } + void Write(LogLevel, std::string) noexcept override {} + }; + return Logger{std::make_shared()}; +} + +class NoopSink final : public IDataSourceUpdateSink { + public: + void Init(Context const&, + std::unordered_map) override {} + void Upsert(Context const&, std::string, ItemDescriptor) override {} + void Apply(Context const&, FlagChangeSet, bool) override {} +}; + +// Serves a stream that sends a fixed event on the first connection. Every +// connection stays open until the client closes it. Counts the connections. +// +// Thread safety: the accept and session handlers run on the io_context passed +// to the constructor. WaitForConnections is safe from any thread. The +// connection count is protected by mutex_. +class StreamService { + public: + StreamService(boost::asio::io_context& ioc, std::string first_event) + : acceptor_(ioc, + tcp::endpoint(boost::asio::ip::make_address("127.0.0.1"), + /* port= */ 0)), + first_event_(std::move(first_event)) { + Accept(); + } + + [[nodiscard]] std::string Url() const { + return "http://127.0.0.1:" + + std::to_string(acceptor_.local_endpoint().port()); + } + + // Returns true when the service has received at least count stream + // connections before the timeout. + bool WaitForConnections(int count, std::chrono::milliseconds timeout) { + std::unique_lock lock(mutex_); + return cv_.wait_for(lock, timeout, + [this, count] { return connections_ >= count; }); + } + + private: + struct Session : std::enable_shared_from_this { + Session(tcp::socket socket, StreamService* service) + : socket(std::move(socket)), service(service) {} + + void Run() { + http::async_read(socket, buffer, request, + [self = shared_from_this()]( + beast::error_code const& ec, std::size_t) { + if (ec) { + return; + } + self->Stream(self->service->CountConnection()); + }); + } + + // Writes the response head, and the event if this is the first + // connection, but does not end the response. + void Stream(int connection) { + stream_text = + "HTTP/1.1 200 OK\r\n" + "Content-Type: text/event-stream\r\n" + "\r\n"; + if (connection == 1) { + stream_text += service->first_event_; + } + boost::asio::async_write( + socket, boost::asio::buffer(stream_text), + [self = shared_from_this()](beast::error_code const& ec, + std::size_t) { + if (ec) { + return; + } + self->socket.async_wait( + tcp::socket::wait_read, + [self](beast::error_code const&) {}); + }); + } + + tcp::socket socket; + beast::flat_buffer buffer; + http::request request; + std::string stream_text; + StreamService* service; + }; + + void Accept() { + acceptor_.async_accept( + [this](beast::error_code const& ec, tcp::socket socket) { + if (ec) { + return; + } + std::make_shared(std::move(socket), this)->Run(); + Accept(); + }); + } + + // Returns the number of connections, this one included. + int CountConnection() { + std::lock_guard lock(mutex_); + ++connections_; + cv_.notify_all(); + return connections_; + } + + tcp::acceptor acceptor_; + std::string const first_event_; + + // Protected by mutex_. + std::mutex mutex_; + std::condition_variable cv_; + int connections_ = 0; +}; + +struct InvalidEvent { + std::string name; + std::string event; +}; + +} // namespace + +class StreamingDataSourceInvalidDataTest + : public ::testing::TestWithParam {}; + +TEST_P(StreamingDataSourceInvalidDataTest, RestartsTheStream) { + boost::asio::io_context ioc; + auto work = boost::asio::make_work_guard(ioc); + StreamService service(ioc, GetParam().event); + + auto const endpoints = + config::shared::builders::EndpointsBuilder() + .RelayProxyBaseURL(service.Url()) + .Build(); + ASSERT_TRUE(endpoints); + config::shared::built::DataSourceConfig const data_source_config{ + config::shared::built::StreamingConfig{ + /* initial_reconnect_delay= */ 1ms, /* streaming_path= */ "/meval"}, + /* with_reasons= */ false, /* use_report= */ false}; + auto const http_properties = + config::shared::builders::HttpPropertiesBuilder().Build(); + + auto const logger = MakeNullLogger(); + DataSourceStatusManager status_manager; + NoopSink sink; + auto source = std::make_shared( + *endpoints, data_source_config, http_properties, ioc.get_executor(), + ContextBuilder().Kind("user", "user-key").Build(), sink, status_manager, + logger); + + // Start the data source. The stream sends the invalid event and stays + // open. + source->Start(); + std::thread thread([&ioc] { ioc.run(); }); + + // The data source must close the stream and open a new one. + EXPECT_TRUE(service.WaitForConnections(2, 5s)); + + std::promise shutdown; + boost::asio::post(ioc, [&source, &shutdown] { + source->ShutdownAsync([&shutdown] { shutdown.set_value(); }); + }); + shutdown.get_future().wait_for(5s); + work.reset(); + ioc.stop(); + thread.join(); +} + +INSTANTIATE_TEST_SUITE_P( + InvalidEvents, + StreamingDataSourceInvalidDataTest, + ::testing::Values( + InvalidEvent{"MalformedPut", "event: put\ndata: {sorry\n\n"}, + InvalidEvent{"MalformedPatch", "event: patch\ndata: {sorry\n\n"}, + InvalidEvent{"MalformedDelete", "event: delete\ndata: {sorry\n\n"}, + InvalidEvent{"SchemaMismatchPut", + "event: put\ndata: {\"potato\": {}}\n\n"}, + InvalidEvent{"SchemaMismatchPatch", + "event: patch\ndata: {\"potato\": {}}\n\n"}, + InvalidEvent{"SchemaMismatchDelete", + "event: delete\ndata: {\"potato\": {}}\n\n"}), + [](::testing::TestParamInfo const& info) { + return info.param.name; + }); diff --git a/libs/server-sent-events/src/curl_client.hpp b/libs/server-sent-events/src/curl_client.hpp index 276e5131e..5c58bc136 100644 --- a/libs/server-sent-events/src/curl_client.hpp +++ b/libs/server-sent-events/src/curl_client.hpp @@ -8,6 +8,9 @@ #include "parser.hpp" #include +#ifndef _WIN32 +#include +#endif #include #include #include @@ -199,10 +202,13 @@ class CurlClient final : public Client, return; } if (curl_socket_ != CURL_SOCKET_BAD) { + // Shut down the socket, but do not close it. CURL then reads + // the end of the stream and ends the transfer. CURL does not + // get a notification when other code closes its socket. #ifdef _WIN32 - closesocket(curl_socket_); + ::shutdown(curl_socket_, SD_BOTH); #else - close(curl_socket_); + ::shutdown(curl_socket_, SHUT_RDWR); #endif curl_socket_ = CURL_SOCKET_BAD; }