From fce0c688c3318623c1e104a79473f16f922bfa2f Mon Sep 17 00:00:00 2001 From: PA Dev Bot <344001113+pa-devbot@users.noreply.github.com> Date: Fri, 6 Nov 2026 10:12:46 +0000 Subject: [PATCH 1/5] Sockets: bound the sys-botbase receive buffer; add non-blocking ClientSocket::abort() A configured sys-botbase server (or an active attacker on the unauthenticated plaintext connection) could stream bytes without a '\n' forever. on_receive_data() appended every byte to m_receive_buffer, a std::deque, with no line-length limit, so process memory grew until allocation failure. The surrounding catch swallowed the std::bad_alloc without clearing the buffer or disconnecting, so the peer kept applying pressure. Fix: - Cap a single line at SBB_MAX_RECEIVE_LINE_BYTES (64 KiB, generous for the protocol). The accumulation is extracted into the testable static TcpSysbotBase_Connection::accumulate_receive_line(), which stops consuming, clears the partial line, and flags overflow once a line would exceed the cap. - On overflow, on_receive_data() logs a protocol error, updates the status line, clears the buffer, and aborts the connection. Aborting from a receive callback is the hard part: ClientSocket::close() blocks until the receive thread exits and is documented as unsafe from a listener callback (which runs on that thread - calling it would deadlock). Add a non-blocking ClientSocket::abort() to every backend: it marks the socket DESTRUCTING and releases the underlying socket, then returns without waiting. The receive thread observes this and exits on its own; a later close() completes the teardown to NOT_RUNNING. No on_disconnect is fired, matching close(). Tests: - 8 ReceiveLine cases pin the bound (normal, multi-line, partial, exactly max, max+1, overflow with/without a trailing newline, CR handling). - A new "abort() from the receive callback" socket test connects a client whose on_receive_data() calls abort(), and verifies it returns without deadlocking and the socket is no longer CONNECTED. Runs for Qt, POSIX, and WinSocket. Full suite: 20,365 passed / 0 failed / 3 skipped. --- Common/Cpp/Sockets/AbstractClientSocket.h | 12 ++ Common/Cpp/Sockets/ClientSocket_POSIX.cpp | 17 ++ Common/Cpp/Sockets/ClientSocket_POSIX.h | 2 + Common/Cpp/Sockets/ClientSocket_Qt.cpp | 25 +++ Common/Cpp/Sockets/ClientSocket_Qt.h | 2 + Common/Cpp/Sockets/ClientSocket_WinSocket.cpp | 17 ++ Common/Cpp/Sockets/ClientSocket_WinSocket.h | 2 + Common/Cpp/Sockets/Sockets_Tests.cpp | 110 +++++++++++ .../ComputerPrograms_UnitTestRunner.cpp | 1 + .../SysbotBase/SysbotBase_Connection.cpp | 173 ++++++++++++++++-- .../SysbotBase/SysbotBase_Connection.h | 26 +++ 11 files changed, 367 insertions(+), 20 deletions(-) diff --git a/Common/Cpp/Sockets/AbstractClientSocket.h b/Common/Cpp/Sockets/AbstractClientSocket.h index f7555db105..c23dcc83e4 100644 --- a/Common/Cpp/Sockets/AbstractClientSocket.h +++ b/Common/Cpp/Sockets/AbstractClientSocket.h @@ -75,6 +75,18 @@ class AbstractClientSocket{ // This blocks until the receive thread has exited, so it must not be // called from within a listener callback. virtual void close() noexcept = 0; + + // Requests immediate teardown of the active connection. Unlike + // "close()", this does NOT wait for the receive thread to exit, so it is + // safe to call from within a listener callback (which runs on that + // thread; calling "close()" there would deadlock). It marks the socket + // DESTRUCTING and releases the underlying socket; the receive thread + // observes this on its next iteration and exits. The socket is left in + // State::DESTRUCTING; a later "close()" completes the teardown (returns + // it to NOT_RUNNING). As with "close()", no "on_disconnect" callback is + // fired. Calling "abort()" when there is no active connection is a + // no-op. It is safe to call multiple times. + virtual void abort() noexcept = 0; virtual void connect(const std::string& address, uint16_t port) = 0; // Sends data on the active connection. This blocks until every byte diff --git a/Common/Cpp/Sockets/ClientSocket_POSIX.cpp b/Common/Cpp/Sockets/ClientSocket_POSIX.cpp index 95472483af..928426f65c 100644 --- a/Common/Cpp/Sockets/ClientSocket_POSIX.cpp +++ b/Common/Cpp/Sockets/ClientSocket_POSIX.cpp @@ -50,6 +50,23 @@ void ClientSocket_POSIX::close() noexcept{ m_cv.notify_all(); } } +void ClientSocket_POSIX::abort() noexcept{ + // Non-blocking teardown, safe to call from the receive thread. "close()" + // waits for the receive thread to exit and so must not run from a + // listener callback; this instead just flips the state and releases the + // socket. The receive thread's next iteration sees DESTRUCTING (or the + // missing socket) and exits on its own. The socket is left DESTRUCTING; + // a later close() returns it to NOT_RUNNING. + { + std::lock_guard lg(m_lock); + if (m_state.load(std::memory_order_relaxed) == State::NOT_RUNNING && !m_thread){ + return; + } + m_state.store(State::DESTRUCTING, std::memory_order_release); + close_socket_locked(); + } + m_cv.notify_all(); +} void ClientSocket_POSIX::connect(const std::string& address, uint16_t port){ std::string error; diff --git a/Common/Cpp/Sockets/ClientSocket_POSIX.h b/Common/Cpp/Sockets/ClientSocket_POSIX.h index d88beaed21..0df8862459 100644 --- a/Common/Cpp/Sockets/ClientSocket_POSIX.h +++ b/Common/Cpp/Sockets/ClientSocket_POSIX.h @@ -41,6 +41,8 @@ class ClientSocket_POSIX final : public AbstractClientSocket{ // called from within a listener callback (which runs on that thread). virtual void close() noexcept override; + virtual void abort() noexcept override; + virtual void connect(const std::string& address, uint16_t port) override; virtual size_t send( diff --git a/Common/Cpp/Sockets/ClientSocket_Qt.cpp b/Common/Cpp/Sockets/ClientSocket_Qt.cpp index 768d2cfec5..350c8e3366 100644 --- a/Common/Cpp/Sockets/ClientSocket_Qt.cpp +++ b/Common/Cpp/Sockets/ClientSocket_Qt.cpp @@ -49,6 +49,31 @@ void ClientSocket_Qt::close() noexcept{ std::lock_guard lg(m_lock); m_state.store(State::NOT_RUNNING, std::memory_order_release); } +void ClientSocket_Qt::abort() noexcept{ + QTcpSocket* socket; + { + std::lock_guard lg(m_lock); + if (m_socket == nullptr){ + return; + } + m_state.store(State::DESTRUCTING, std::memory_order_release); + m_success_pending = false; + socket = m_socket; + m_socket = nullptr; + } + cancel_inflight_sends(); + + // Destroy the socket without blocking, so this is safe to call from a + // listener callback (which runs on the QTcpSocket's event thread). + // remove_object() waits for that event thread to delete the socket, so it + // is dispatched to a worker thread instead of run here. The socket is + // left in State::DESTRUCTING. + try{ + GlobalThreadPools::qt_worker_threadpool().dispatch([socket]{ + GlobalThreadPools::qt_event_threadpool().remove_object(socket); + }); + }catch (...){} +} void ClientSocket_Qt::connect(const std::string& address, uint16_t port){ QTcpSocket* old_socket; { diff --git a/Common/Cpp/Sockets/ClientSocket_Qt.h b/Common/Cpp/Sockets/ClientSocket_Qt.h index 2e7c0f6918..3df6ccb202 100644 --- a/Common/Cpp/Sockets/ClientSocket_Qt.h +++ b/Common/Cpp/Sockets/ClientSocket_Qt.h @@ -72,6 +72,8 @@ class ClientSocket_Qt final : public QObject, public AbstractClientSocket{ // socket that is running them. virtual void close() noexcept override; + virtual void abort() noexcept override; + virtual void connect(const std::string& address, uint16_t port) override; virtual size_t send( diff --git a/Common/Cpp/Sockets/ClientSocket_WinSocket.cpp b/Common/Cpp/Sockets/ClientSocket_WinSocket.cpp index a815d7f5b6..9490020c57 100644 --- a/Common/Cpp/Sockets/ClientSocket_WinSocket.cpp +++ b/Common/Cpp/Sockets/ClientSocket_WinSocket.cpp @@ -40,6 +40,23 @@ void ClientSocket_WinSocket::close() noexcept{ m_cv.notify_all(); } } +void ClientSocket_WinSocket::abort() noexcept{ + // Non-blocking teardown, safe to call from the receive thread. "close()" + // waits for the receive thread to exit and so must not run from a + // listener callback; this instead just flips the state and releases the + // socket. The receive thread's next iteration sees DESTRUCTING (or the + // missing socket) and exits on its own. The socket is left DESTRUCTING; + // a later close() returns it to NOT_RUNNING. + { + std::lock_guard lg(m_lock); + if (m_state.load(std::memory_order_relaxed) == State::NOT_RUNNING && !m_thread){ + return; + } + m_state.store(State::DESTRUCTING, std::memory_order_release); + close_socket_locked(); + } + m_cv.notify_all(); +} void ClientSocket_WinSocket::connect(const std::string& address, uint16_t port){ std::string error; { diff --git a/Common/Cpp/Sockets/ClientSocket_WinSocket.h b/Common/Cpp/Sockets/ClientSocket_WinSocket.h index a67f01e683..9de0d6bccf 100644 --- a/Common/Cpp/Sockets/ClientSocket_WinSocket.h +++ b/Common/Cpp/Sockets/ClientSocket_WinSocket.h @@ -45,6 +45,8 @@ class ClientSocket_WinSocket final : public AbstractClientSocket{ // called from within a listener callback (which runs on that thread). virtual void close() noexcept override; + virtual void abort() noexcept override; + virtual void connect(const std::string& address, uint16_t port) override; virtual size_t send( diff --git a/Common/Cpp/Sockets/Sockets_Tests.cpp b/Common/Cpp/Sockets/Sockets_Tests.cpp index d4e2c014e9..41a197a6c3 100644 --- a/Common/Cpp/Sockets/Sockets_Tests.cpp +++ b/Common/Cpp/Sockets/Sockets_Tests.cpp @@ -88,6 +88,33 @@ struct ClientListener : AbstractClientSocket::Listener{ }; +// A client listener that tears the connection down from within its own +// on_receive_data() callback via abort() - the exact scenario sys-botbase +// hits when it detects an oversized line. close() would deadlock here (it +// waits for this very receive thread to exit); abort() must return promptly. +struct AbortingClientListener : AbstractClientSocket::Listener{ + Waiter waiter; + std::atomic connect_finished{0}; + std::string last_connect_error; + std::atomic aborted_in_callback{0}; + // Set to the owning client after construction; on_receive_data only + // fires once connected and receiving, so it is valid by then. + AbstractClientSocket* client = nullptr; + + virtual void on_connect_finished(const std::string& error_message) override{ + connect_finished++; + last_connect_error = error_message; + waiter.notify(); + } + + virtual void on_receive_data(const void*, size_t) override{ + client->abort(); + aborted_in_callback++; + waiter.notify(); + } +}; + + struct ServerListener : AbstractServerSocket::Listener{ Waiter waiter; std::atomic listen_finished{0}; @@ -2285,6 +2312,86 @@ class Test_Sockets_CloseConnectionCancelsSend : public TestSockets +class Test_Sockets_AbortFromReceiveCallback : public TestSockets{ +public: + using TestSockets::TestSockets; + + virtual UnitTestResult run(Logger& logger, CancellableScope& scope) const override{ + if (timing_unreliable()){ + return UnitTestResult( + UnitTestResult::SKIPPED, + "Machine too starved for a timing-sensitive socket test (timing_unreliable())." + ); + } + ServerSocketType server(this->m_thread_pool); + ServerListener server_listener; + server.add_listener(server_listener); + + uint16_t port; + if (!this->start_server(server, server_listener, port)){ + if (timing_unreliable()){ + return UnitTestResult( + UnitTestResult::SKIPPED, + "The server did not start listening on a starved machine." + ); + } + return "Failed to start the server"; + } + + bool ok = false; + std::string error; + { + ClientSocketType client(this->m_thread_pool); + AbortingClientListener client_listener; + client_listener.client = &client; + client.add_listener(client_listener); + + client.connect("127.0.0.1", port); + if (!client_listener.waiter.wait_for( + SOCKET_TEST_WAIT, + [&client_listener]{ return client_listener.connect_finished.load() >= 1; } + )){ + error = "The client did not finish connecting"; + }else if (!client_listener.last_connect_error.empty()){ + error = "The client failed to connect: " + client_listener.last_connect_error; + }else if (!server_listener.waiter.wait_for( + SOCKET_TEST_WAIT, + [&server_listener]{ return server_listener.connected.load() >= 1; } + )){ + error = "The server did not call on_connect"; + }else if (server.send("ping", 4) != 4){ + error = "The server did not send the trigger packet"; + }else if (!client_listener.waiter.wait_for( + SOCKET_TEST_WAIT, + [&client_listener]{ return client_listener.aborted_in_callback.load() >= 1; } + )){ + error = "abort() did not return from the on_receive_data() callback (it blocked, like close() would)"; + }else if (client.state() == AbstractClientSocket::State::CONNECTED){ + error = "After abort() the client is still CONNECTED"; + }else{ + // Now that abort() has stopped the receive thread, close() must + // complete without hanging. + client.close(); + ok = true; + } + } + + server.close(); + + if (!ok && !error.empty()){ + return error; + } + return true; + } +}; + + template void add_socket_tests(UnitTestDatabase& database, ThreadPool& thread_pool, const char* backend_name){ const std::string prefix = "Sockets (" + std::string(backend_name) + ") - "; @@ -2322,6 +2429,9 @@ void add_socket_tests(UnitTestDatabase& database, ThreadPool& thread_pool, const database.add>( thread_pool, prefix + "close_connection() cancels an in-flight send" ); + database.add>( + thread_pool, prefix + "abort() from the receive callback" + ); } diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index b97f283106..1eaae86b3a 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -57,6 +57,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ add_tests_ListenerSet(ret); add_tests_MoveOnlyFunction(ret); SysbotBase::add_tests_SysbotBasePing(ret); + SysbotBase::add_tests_SysbotBaseReceiveLine(ret); add_tests_AudioInfo(ret); add_tests_ChannelMixerMatrix(ret); add_tests_FFTStreamer(ret); diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp b/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp index 3f069de33e..bd0a390e6c 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp @@ -28,6 +28,13 @@ namespace SysbotBase{ using namespace std::chrono_literals; +// Maximum number of bytes in a single sys-botbase line (the bytes between +// two '\n's, '\r' excluded). A peer that streams bytes without a '\n' can +// otherwise grow the receive buffer without bound; a line longer than this +// is discarded and the connection is aborted. +constexpr size_t SBB_MAX_RECEIVE_LINE_BYTES = 65536; // 64 KiB + + bool parse_ip_and_port(const std::string& str, QHostAddress& address, int& port){ // IPv4 @@ -180,30 +187,74 @@ void TcpSysbotBase_Connection::on_receive_data(const void* data, size_t bytes){ WallClock now = current_time(); try{ - const char* ptr = (const char*)data; - for (size_t c = 0; c < bytes; c++){ - char ch = ptr[c]; - if (ch == '\r'){ - continue; - } - if (ch != '\n'){ - m_receive_buffer.emplace_back(ch); - continue; - } - process_message( - std::string( - m_receive_buffer.begin(), - m_receive_buffer.end() - ), - now - ); + std::vector messages; + bool overflow = false; + accumulate_receive_line( + m_receive_buffer, + messages, + (const char*)data, + bytes, + SBB_MAX_RECEIVE_LINE_BYTES, + overflow + ); + if (overflow){ + // A peer streamed a line longer than the maximum without a + // terminating '\n'. Drop the partial line and abort the + // connection; the buffer can no longer grow. m_receive_buffer.clear(); + on_receive_overflow(); + return; + } + for (const auto& message : messages){ + process_message(message, now); } - -// m_listeners.run_method(&Listener::on_receive_data, data, bytes); - }catch (...){} } +size_t TcpSysbotBase_Connection::accumulate_receive_line( + std::deque& line, + std::vector& messages, + const char* data, + size_t bytes, + size_t max_line_bytes, + bool& overflow +){ + size_t consumed = 0; + for (size_t c = 0; c < bytes; c++){ + char ch = data[c]; + if (ch == '\r'){ + consumed++; + continue; + } + if (ch != '\n'){ + if (line.size() >= max_line_bytes){ + line.clear(); + overflow = true; + return consumed; + } + line.emplace_back(ch); + consumed++; + continue; + } + // '\n': emit the completed line and start a new one. + messages.emplace_back(line.begin(), line.end()); + line.clear(); + consumed++; + } + return consumed; +} +void TcpSysbotBase_Connection::on_receive_overflow(){ + m_logger.log( + "sys-botbase sent a line longer than " + std::to_string(SBB_MAX_RECEIVE_LINE_BYTES) + + " bytes with no newline; aborting the connection.", + COLOR_RED + ); + set_status_line1("Disconnected: sys-botbase sent an oversized line.", COLOR_RED); + // Safe to call from this receive-thread callback: unlike close(), it does + // not wait for the receive thread to exit (which would deadlock). The + // heartbeat thread_loop, if running, observes the DESTRUCTING state and + // stops on its next iteration. + m_socket.abort(); +} void TcpSysbotBase_Connection::process_message(const std::string& message, WallClock timestamp){ if (LOG_EVERYTHING()){ m_logger.log("Received: " + message, COLOR_DARKGREEN); @@ -369,6 +420,88 @@ void add_tests_SysbotBasePing(UnitTestDatabase& database){ } +namespace{ +class ReceiveLineBoundTest : public UnitTest{ +public: + ReceiveLineBoundTest( + const std::string& name, + const std::string& input, + size_t max_line, + std::vector expected_messages, + bool expected_overflow, + std::string expected_final_buffer + ); + + virtual UnitTestResult run(Logger&, CancellableScope&) const override; + +private: + std::string m_input; + size_t m_max_line; + std::vector m_expected_messages; + bool m_expected_overflow; + std::string m_expected_final_buffer; +}; +} + + +ReceiveLineBoundTest::ReceiveLineBoundTest( + const std::string& name, + const std::string& input, + size_t max_line, + std::vector expected_messages, + bool expected_overflow, + std::string expected_final_buffer +) + : UnitTest("NintendoSwitch::SysbotBase - " + name) + , m_input(input) + , m_max_line(max_line) + , m_expected_messages(std::move(expected_messages)) + , m_expected_overflow(expected_overflow) + , m_expected_final_buffer(std::move(expected_final_buffer)) +{} + +UnitTestResult ReceiveLineBoundTest::run(Logger&, CancellableScope&) const{ + std::deque buffer; + std::vector messages; + bool overflow = false; + TcpSysbotBase_Connection::accumulate_receive_line( + buffer, + messages, + m_input.data(), + m_input.size(), + m_max_line, + overflow + ); + if (overflow != m_expected_overflow){ + return "overflow=" + std::string(overflow ? "true" : "false") + + ", expected " + std::string(m_expected_overflow ? "true" : "false") + "."; + } + if (messages != m_expected_messages){ + return "emitted " + std::to_string(messages.size()) + " message(s), expected " + + std::to_string(m_expected_messages.size()) + "."; + } + std::string final_buffer(buffer.begin(), buffer.end()); + if (final_buffer != m_expected_final_buffer){ + return "final buffer size " + std::to_string(final_buffer.size()) + + ", expected " + std::to_string(m_expected_final_buffer.size()) + "."; + } + return true; +} + + + +void add_tests_SysbotBaseReceiveLine(UnitTestDatabase& database){ + database.add("ReceiveLine(normal)", "hello\n", 16, std::vector{"hello"}, false, ""); + database.add("ReceiveLine(multiple)", "a\nb\nc\n", 16, std::vector{"a", "b", "c"}, false, ""); + database.add("ReceiveLine(partial)", "abc", 16, std::vector{}, false, "abc"); + database.add("ReceiveLine(oversized no newline)", std::string(40, 'A'), 16, std::vector{}, true, ""); + database.add("ReceiveLine(exactly max)", std::string(16, 'A') + "\n", 16, std::vector{std::string(16, 'A')}, false, ""); + database.add("ReceiveLine(max+1 no newline)", std::string(17, 'A'), 16, std::vector{}, true, ""); + database.add("ReceiveLine(CR ignored)", "a\rb\n", 16, std::vector{"ab"}, false, ""); + database.add("ReceiveLine(overflow before newline)", std::string(100, 'A') + "\n", 16, std::vector{}, true, ""); +} + + diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h b/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h index 36bdff0750..2f4879d478 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h @@ -8,6 +8,7 @@ #define PokemonAutomation_Controllers_SysbotBase_Connection_H #include +#include #include #include #include @@ -56,6 +57,25 @@ class TcpSysbotBase_Connection : public ControllerConnection, private ClientSock // static bool parse_ping_response(const std::string& str, uint64_t& seqnum); + // + // Accumulates the next "bytes" of "data" into the current receive line + // "line", emitting each complete line (terminated by '\n') into + // "messages" and clearing "line" for the next. '\r' bytes are ignored. + // A single line is capped at "max_line_bytes": if it would grow past + // that before its '\n', "overflow" is set, "line" is cleared, and no + // further bytes are consumed. Returns the number of bytes consumed from + // "data". This is what stops a peer that streams bytes without a '\n' + // from growing the receive buffer without bound. + // + static size_t accumulate_receive_line( + std::deque& line, + std::vector& messages, + const char* data, + size_t bytes, + size_t max_line_bytes, + bool& overflow + ); + private: void thread_loop(); @@ -64,6 +84,7 @@ class TcpSysbotBase_Connection : public ControllerConnection, private ClientSock virtual void on_disconnect(const std::string& error_message) override; void process_message(const std::string& message, WallClock timestamp); + void on_receive_overflow(); void set_mode(const std::string& sbb_version); private: @@ -93,6 +114,11 @@ class TcpSysbotBase_Connection : public ControllerConnection, private ClientSock // void add_tests_SysbotBasePing(UnitTestDatabase& database); +// +// Registers unit tests for accumulate_receive_line(). +// +void add_tests_SysbotBaseReceiveLine(UnitTestDatabase& database); + } From 66dae80014ab4af8cef090bb9a5c917aca0cef01 Mon Sep 17 00:00:00 2001 From: PA Dev Bot <344001113+pa-devbot@users.noreply.github.com> Date: Fri, 6 Nov 2026 11:13:09 +0000 Subject: [PATCH 2/5] SysbotBase: make accumulate_receive_line() file-local accumulate_receive_line() only needs to be visible to on_receive_data() and its unit tests (which live in the same translation unit), so it is no longer part of TcpSysbotBase_Connection's public API. It moves to an anonymous-namespace free function in SysbotBase_Connection.cpp; the now-unneeded include and the public declaration leave the header. --- .../SysbotBase/SysbotBase_Connection.cpp | 73 +++++++++++-------- .../SysbotBase/SysbotBase_Connection.h | 22 +----- 2 files changed, 43 insertions(+), 52 deletions(-) diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp b/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp index bd0a390e6c..c95229b9a1 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp @@ -13,6 +13,7 @@ #include #include +#include #include "Common/Cpp/TestRunners/UnitTest.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" @@ -181,36 +182,16 @@ void TcpSysbotBase_Connection::on_disconnect(const std::string& error_message){ set_status_line1("Disconnected: " + error_message, COLOR_RED); }catch (...){} } -void TcpSysbotBase_Connection::on_receive_data(const void* data, size_t bytes){ -// cout << "on_receive_data(): " << std::string((const char*)data, bytes - 2) << endl; - - WallClock now = current_time(); - - try{ - std::vector messages; - bool overflow = false; - accumulate_receive_line( - m_receive_buffer, - messages, - (const char*)data, - bytes, - SBB_MAX_RECEIVE_LINE_BYTES, - overflow - ); - if (overflow){ - // A peer streamed a line longer than the maximum without a - // terminating '\n'. Drop the partial line and abort the - // connection; the buffer can no longer grow. - m_receive_buffer.clear(); - on_receive_overflow(); - return; - } - for (const auto& message : messages){ - process_message(message, now); - } - }catch (...){} -} -size_t TcpSysbotBase_Connection::accumulate_receive_line( +namespace{ +// Accumulates the next "bytes" of "data" into the current receive line +// "line", emitting each complete line (terminated by '\n') into +// "messages" and clearing "line" for the next. '\r' bytes are ignored. +// A single line is capped at "max_line_bytes": if it would grow past that +// before its '\n', "overflow" is set, "line" is cleared, and no further +// bytes are consumed. Returns the number of bytes consumed from "data". +// File-local: this is what stops a peer that streams bytes without a '\n' +// from growing the receive buffer without bound. +size_t accumulate_receive_line( std::deque& line, std::vector& messages, const char* data, @@ -242,6 +223,36 @@ size_t TcpSysbotBase_Connection::accumulate_receive_line( } return consumed; } +} +void TcpSysbotBase_Connection::on_receive_data(const void* data, size_t bytes){ +// cout << "on_receive_data(): " << std::string((const char*)data, bytes - 2) << endl; + + WallClock now = current_time(); + + try{ + std::vector messages; + bool overflow = false; + accumulate_receive_line( + m_receive_buffer, + messages, + (const char*)data, + bytes, + SBB_MAX_RECEIVE_LINE_BYTES, + overflow + ); + if (overflow){ + // A peer streamed a line longer than the maximum without a + // terminating '\n'. Drop the partial line and abort the + // connection; the buffer can no longer grow. + m_receive_buffer.clear(); + on_receive_overflow(); + return; + } + for (const auto& message : messages){ + process_message(message, now); + } + }catch (...){} +} void TcpSysbotBase_Connection::on_receive_overflow(){ m_logger.log( "sys-botbase sent a line longer than " + std::to_string(SBB_MAX_RECEIVE_LINE_BYTES) @@ -464,7 +475,7 @@ UnitTestResult ReceiveLineBoundTest::run(Logger&, CancellableScope&) const{ std::deque buffer; std::vector messages; bool overflow = false; - TcpSysbotBase_Connection::accumulate_receive_line( + accumulate_receive_line( buffer, messages, m_input.data(), diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h b/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h index 2f4879d478..cdcae5c6c3 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h @@ -8,7 +8,6 @@ #define PokemonAutomation_Controllers_SysbotBase_Connection_H #include -#include #include #include #include @@ -57,25 +56,6 @@ class TcpSysbotBase_Connection : public ControllerConnection, private ClientSock // static bool parse_ping_response(const std::string& str, uint64_t& seqnum); - // - // Accumulates the next "bytes" of "data" into the current receive line - // "line", emitting each complete line (terminated by '\n') into - // "messages" and clearing "line" for the next. '\r' bytes are ignored. - // A single line is capped at "max_line_bytes": if it would grow past - // that before its '\n', "overflow" is set, "line" is cleared, and no - // further bytes are consumed. Returns the number of bytes consumed from - // "data". This is what stops a peer that streams bytes without a '\n' - // from growing the receive buffer without bound. - // - static size_t accumulate_receive_line( - std::deque& line, - std::vector& messages, - const char* data, - size_t bytes, - size_t max_line_bytes, - bool& overflow - ); - private: void thread_loop(); @@ -115,7 +95,7 @@ class TcpSysbotBase_Connection : public ControllerConnection, private ClientSock void add_tests_SysbotBasePing(UnitTestDatabase& database); // -// Registers unit tests for accumulate_receive_line(). +// Registers unit tests for the receive-line length bound. // void add_tests_SysbotBaseReceiveLine(UnitTestDatabase& database); From 13b69531de5323b0b2556b1126f411d5aa15c8a8 Mon Sep 17 00:00:00 2001 From: PA Dev Bot <344001113+pa-devbot@users.noreply.github.com> Date: Fri, 6 Nov 2026 11:13:09 +0000 Subject: [PATCH 3/5] Sockets (Qt): implement abort() with QTcpSocket::abort() The first implementation deferred pool removal of the socket to a worker thread, which could crash the Qt event loop under load (an event handler throwing std::system_error). Instead mark the socket DESTRUCTING, cancel in-flight sends, and call the socket's own non-blocking abort(); the resulting disconnect/error signals see DESTRUCTING and are no-ops, and a later close() removes the socket and returns it to NOT_RUNNING. --- Common/Cpp/Sockets/ClientSocket_Qt.cpp | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/Common/Cpp/Sockets/ClientSocket_Qt.cpp b/Common/Cpp/Sockets/ClientSocket_Qt.cpp index 350c8e3366..5b91804690 100644 --- a/Common/Cpp/Sockets/ClientSocket_Qt.cpp +++ b/Common/Cpp/Sockets/ClientSocket_Qt.cpp @@ -59,20 +59,17 @@ void ClientSocket_Qt::abort() noexcept{ m_state.store(State::DESTRUCTING, std::memory_order_release); m_success_pending = false; socket = m_socket; - m_socket = nullptr; } - cancel_inflight_sends(); - // Destroy the socket without blocking, so this is safe to call from a - // listener callback (which runs on the QTcpSocket's event thread). - // remove_object() waits for that event thread to delete the socket, so it - // is dispatched to a worker thread instead of run here. The socket is - // left in State::DESTRUCTING. - try{ - GlobalThreadPools::qt_worker_threadpool().dispatch([socket]{ - GlobalThreadPools::qt_event_threadpool().remove_object(socket); - }); - }catch (...){} + // Immediately break the connection. QTcpSocket::abort() is safe to call + // from the socket's event thread (where a listener callback runs): it + // disconnects without waiting, so this never blocks. The resulting + // "disconnected"/"errorOccurred" signals reach connection_failed(), see + // State::DESTRUCTING, and are no-ops. The socket is left in + // State::DESTRUCTING; a later close() removes it and returns it to + // NOT_RUNNING. + cancel_inflight_sends(); + socket->abort(); } void ClientSocket_Qt::connect(const std::string& address, uint16_t port){ QTcpSocket* old_socket; From ce0abf103992fca6c731c3b544deb03ebba16cc9 Mon Sep 17 00:00:00 2001 From: PA Dev Bot <344001113+pa-devbot@users.noreply.github.com> Date: Fri, 6 Nov 2026 19:42:32 +0000 Subject: [PATCH 4/5] Sockets: rename close() -> close_and_wait() (private); abort() -> close() Per review, the two client-socket teardown methods are one primitive (stop, optionally wait for the receive thread), so the naming now matches the call sites: - close() is the non-blocking stop (the former abort()): it marks the socket DESTRUCTING and releases the underlying socket; safe to call from a listener callback. It no longer joins the thread or returns NOT_RUNNING. - close_and_wait() is the blocking teardown (the former close()): joins the receive thread and returns NOT_RUNNING. It is now private, driven by the destructor and by connect(). connect() now calls close_and_wait() up front: since close() leaves the socket DESTRUCTING, reconnecting on the same socket joins the old thread there (connect is always user-initiated, never a callback, so blocking is safe). This keeps same-object reconnect working. Qt: close() posts QTcpSocket::abort() to the socket's event thread (QMetaObject::invokeMethod, queued) instead of calling it cross-thread, which tripped Qt's QSocketNotifier thread check and crashed the event loop when the tests call close() off the event thread. Tests: the "close() from the receive callback" test (formerly "abort() ...") drives the public close(); the send-timeout test asserts DESTRUCTING after the (now non-blocking) close() instead of NOT_RUNNING. --- Common/Cpp/Sockets/AbstractClientSocket.h | 37 +++++------- Common/Cpp/Sockets/ClientSocket_POSIX.cpp | 26 ++++++--- Common/Cpp/Sockets/ClientSocket_POSIX.h | 37 ++++++------ Common/Cpp/Sockets/ClientSocket_Qt.cpp | 35 ++++++++---- Common/Cpp/Sockets/ClientSocket_Qt.h | 45 +++++++-------- Common/Cpp/Sockets/ClientSocket_WinSocket.cpp | 26 ++++++--- Common/Cpp/Sockets/ClientSocket_WinSocket.h | 37 ++++++------ Common/Cpp/Sockets/Sockets_Tests.cpp | 56 ++++++++++--------- .../SysbotBase/SysbotBase_Connection.cpp | 11 ++-- 9 files changed, 174 insertions(+), 136 deletions(-) diff --git a/Common/Cpp/Sockets/AbstractClientSocket.h b/Common/Cpp/Sockets/AbstractClientSocket.h index c23dcc83e4..fd7fb408e3 100644 --- a/Common/Cpp/Sockets/AbstractClientSocket.h +++ b/Common/Cpp/Sockets/AbstractClientSocket.h @@ -61,32 +61,21 @@ class AbstractClientSocket{ return m_state.load(std::memory_order_relaxed); } - // Stops the connection and returns the socket to State::NOT_RUNNING - // so "connect()" can be called again. This is safe to call multiple - // times. - // - // It cancels any in-flight operations: no further - // "Listener::on_receive_data" callbacks are started (one already - // running is allowed to finish), and an in-flight "send()" returns - // promptly with the number of bytes actually written so far (best - // effort). A "send()" that is already racing "close()" may still - // complete. - // - // This blocks until the receive thread has exited, so it must not be - // called from within a listener callback. + // Requests immediate teardown of the active connection, without waiting + // for the receive thread to exit. This is safe to call from within a + // listener callback (which runs on that thread; the blocking teardown + // there would deadlock). It marks the socket DESTRUCTING and releases + // the underlying socket; the receive thread observes this on its next + // iteration and exits. It cancels any in-flight "send()": its caller + // returns promptly with the number of bytes actually written so far + // (best effort). No "on_disconnect" callback is fired. The socket is + // left in State::DESTRUCTING; the socket's destructor completes the + // teardown (joins the thread and returns it to NOT_RUNNING), so the + // socket cannot be "connect()"ed again - destroy it and use a new one. + // Calling "close()" when there is no active connection is a no-op. It + // is safe to call multiple times. virtual void close() noexcept = 0; - // Requests immediate teardown of the active connection. Unlike - // "close()", this does NOT wait for the receive thread to exit, so it is - // safe to call from within a listener callback (which runs on that - // thread; calling "close()" there would deadlock). It marks the socket - // DESTRUCTING and releases the underlying socket; the receive thread - // observes this on its next iteration and exits. The socket is left in - // State::DESTRUCTING; a later "close()" completes the teardown (returns - // it to NOT_RUNNING). As with "close()", no "on_disconnect" callback is - // fired. Calling "abort()" when there is no active connection is a - // no-op. It is safe to call multiple times. - virtual void abort() noexcept = 0; virtual void connect(const std::string& address, uint16_t port) = 0; // Sends data on the active connection. This blocks until every byte diff --git a/Common/Cpp/Sockets/ClientSocket_POSIX.cpp b/Common/Cpp/Sockets/ClientSocket_POSIX.cpp index 928426f65c..166b1a553a 100644 --- a/Common/Cpp/Sockets/ClientSocket_POSIX.cpp +++ b/Common/Cpp/Sockets/ClientSocket_POSIX.cpp @@ -28,7 +28,7 @@ ClientSocket_POSIX::ClientSocket_POSIX(ThreadPool& thread_pool) // Ignore SIGPIPE. Handle errors via errno instead signal(SIGPIPE, SIG_IGN); } -void ClientSocket_POSIX::close() noexcept{ +void ClientSocket_POSIX::close_and_wait() noexcept{ AsyncTask thread; { std::lock_guard lg(m_lock); @@ -50,13 +50,14 @@ void ClientSocket_POSIX::close() noexcept{ m_cv.notify_all(); } } -void ClientSocket_POSIX::abort() noexcept{ - // Non-blocking teardown, safe to call from the receive thread. "close()" - // waits for the receive thread to exit and so must not run from a - // listener callback; this instead just flips the state and releases the - // socket. The receive thread's next iteration sees DESTRUCTING (or the - // missing socket) and exits on its own. The socket is left DESTRUCTING; - // a later close() returns it to NOT_RUNNING. +void ClientSocket_POSIX::close() noexcept{ + // Non-blocking teardown, safe to call from the receive thread. The + // blocking "close_and_wait()" waits for the receive thread to exit and so + // must not run from a listener callback; this instead just flips the + // state and releases the socket. The receive thread's next iteration sees + // DESTRUCTING (or the missing socket) and exits on its own. The socket is + // left DESTRUCTING; the destructor's close_and_wait() returns it to + // NOT_RUNNING. { std::lock_guard lg(m_lock); if (m_state.load(std::memory_order_relaxed) == State::NOT_RUNNING && !m_thread){ @@ -69,6 +70,13 @@ void ClientSocket_POSIX::abort() noexcept{ } void ClientSocket_POSIX::connect(const std::string& address, uint16_t port){ + // Complete any in-progress connection or a requested-but-unfinished + // close() before starting a new one. close() is non-blocking (it leaves + // the socket DESTRUCTING), so reconnecting on the same socket joins the + // old receive thread here to get back to NOT_RUNNING. No-op if the socket + // is already NOT_RUNNING. + close_and_wait(); + std::string error; { std::lock_guard lg(m_lock); @@ -108,7 +116,7 @@ void ClientSocket_POSIX::connect(const std::string& address, uint16_t port){ // connect() may be called from a thread that is itself running on // this pool, and blocking here until a pool thread is free could // deadlock a capped pool. The returned AsyncTask still lets - // close() join the receive thread. + // close_and_wait() join the receive thread. try{ m_thread = m_thread_pool.dispatch([=, this]{ thread_loop(address, port); diff --git a/Common/Cpp/Sockets/ClientSocket_POSIX.h b/Common/Cpp/Sockets/ClientSocket_POSIX.h index 0df8862459..2c9ce20bb1 100644 --- a/Common/Cpp/Sockets/ClientSocket_POSIX.h +++ b/Common/Cpp/Sockets/ClientSocket_POSIX.h @@ -25,25 +25,23 @@ class ClientSocket_POSIX final : public AbstractClientSocket{ public: ClientSocket_POSIX(ThreadPool& thread_pool); - virtual ~ClientSocket_POSIX(){ - close(); + ~ClientSocket_POSIX() override{ + close_and_wait(); } - // Stops the receive thread, closes the socket, and returns the socket to - // State::NOT_RUNNING so "connect()" can be called again. This is safe to - // call multiple times. - // - // It cancels an in-flight "send()": the send loop re-checks the state - // every iteration, and "close()" notifies it, so the send returns within - // a millisecond with the bytes written so far. - // - // This blocks until the receive thread has exited, so it must not be - // called from within a listener callback (which runs on that thread). - virtual void close() noexcept override; - - virtual void abort() noexcept override; + // Requests immediate teardown of the active connection, without waiting + // for the receive thread to exit. Safe to call from within a listener + // callback (which runs on that thread). It marks the socket DESTRUCTING + // and releases the underlying socket; the receive thread exits on its + // next iteration. It cancels an in-flight "send()" (the send loop + // re-checks the state and returns within a millisecond with the bytes + // written so far). The socket is left in State::DESTRUCTING; the + // destructor's close_and_wait() joins the thread and returns it to + // NOT_RUNNING. No "on_disconnect" callback is fired. Safe to call + // multiple times. + void close() noexcept override; - virtual void connect(const std::string& address, uint16_t port) override; + void connect(const std::string& address, uint16_t port) override; virtual size_t send( const void* data, @@ -53,6 +51,13 @@ class ClientSocket_POSIX final : public AbstractClientSocket{ private: + // Stops the receive thread, closes the socket, and returns the socket to + // State::NOT_RUNNING. It blocks until the receive thread has exited, so + // it must not be called from within a listener callback. The public + // "close()" only requests the teardown; this actually joins the thread, + // and is called by the destructor. + void close_and_wait() noexcept; + // Closes the underlying socket, if any. Requires "m_lock" to be held. void close_socket_locked(); diff --git a/Common/Cpp/Sockets/ClientSocket_Qt.cpp b/Common/Cpp/Sockets/ClientSocket_Qt.cpp index 5b91804690..125483b1cd 100644 --- a/Common/Cpp/Sockets/ClientSocket_Qt.cpp +++ b/Common/Cpp/Sockets/ClientSocket_Qt.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "Common/Qt/QtThreadPool.h" #include "Common/Qt/GlobalThreadPoolsQt.h" #include "ClientSocket_Qt.h" @@ -21,7 +22,7 @@ namespace PokemonAutomation{ -void ClientSocket_Qt::close() noexcept{ +void ClientSocket_Qt::close_and_wait() noexcept{ QTcpSocket* socket; { std::lock_guard lg(m_lock); @@ -49,7 +50,7 @@ void ClientSocket_Qt::close() noexcept{ std::lock_guard lg(m_lock); m_state.store(State::NOT_RUNNING, std::memory_order_release); } -void ClientSocket_Qt::abort() noexcept{ +void ClientSocket_Qt::close() noexcept{ QTcpSocket* socket; { std::lock_guard lg(m_lock); @@ -61,17 +62,31 @@ void ClientSocket_Qt::abort() noexcept{ socket = m_socket; } - // Immediately break the connection. QTcpSocket::abort() is safe to call - // from the socket's event thread (where a listener callback runs): it - // disconnects without waiting, so this never blocks. The resulting - // "disconnected"/"errorOccurred" signals reach connection_failed(), see - // State::DESTRUCTING, and are no-ops. The socket is left in - // State::DESTRUCTING; a later close() removes it and returns it to - // NOT_RUNNING. + // Immediately break the connection. QTcpSocket::abort() must run on the + // socket's event thread, but "close()" may be called from any thread + // (including a listener callback, which runs on that event thread), so + // post the abort to the socket's event thread instead of calling it here + // - a cross-thread call would trip Qt's QSocketNotifier thread check. + // This stays non-blocking: close() returns immediately, and the socket is + // aborted as soon as the event thread reaches the queued call. The + // resulting "disconnected"/"errorOccurred" signals see State::DESTRUCTING + // and are no-ops. The destructor's close_and_wait() destroys the socket + // and returns it to NOT_RUNNING. cancel_inflight_sends(); - socket->abort(); + QMetaObject::invokeMethod( + socket, + [socket]{ socket->abort(); }, + Qt::QueuedConnection + ); } void ClientSocket_Qt::connect(const std::string& address, uint16_t port){ + // Complete any in-progress connection or a requested-but-unfinished + // close() before starting a new one. close() is non-blocking (it leaves + // the socket DESTRUCTING), so reconnecting on the same socket destroys the + // old QTcpSocket here (on its event thread) to get back to NOT_RUNNING. + // No-op if the socket is already NOT_RUNNING. + close_and_wait(); + QTcpSocket* old_socket; { std::lock_guard lg(m_lock); diff --git a/Common/Cpp/Sockets/ClientSocket_Qt.h b/Common/Cpp/Sockets/ClientSocket_Qt.h index 3df6ccb202..bd5c5085c0 100644 --- a/Common/Cpp/Sockets/ClientSocket_Qt.h +++ b/Common/Cpp/Sockets/ClientSocket_Qt.h @@ -31,8 +31,8 @@ class ClientSocket_Qt final : public QObject, public AbstractClientSocket{ std::string packet; // The event-thread write loop stops at this deadline, so a send // that can never complete (a peer that never reads) cannot pin - // the socket's event thread forever and block "close()". "max()" - // means no deadline (the infinite default). + // the socket's event thread forever and block "close_and_wait()". + // "max()" means no deadline (the infinite default). std::chrono::steady_clock::time_point deadline = std::chrono::steady_clock::time_point::max(); // The running byte count: the write loop updates it as it goes, // so "close()" cancelling the send mid-write can report the @@ -53,28 +53,22 @@ class ClientSocket_Qt final : public QObject, public AbstractClientSocket{ public: ClientSocket_Qt(ThreadPool&){} - virtual ~ClientSocket_Qt(){ - close(); + ~ClientSocket_Qt() override{ + close_and_wait(); } - // Stops the connection, destroys the QTcpSocket, and returns the socket - // to State::NOT_RUNNING so "connect()" can be called again. This blocks - // until the QTcpSocket has been destroyed, so any in-flight listener - // callback is finished by the time it returns. It is safe to call - // multiple times. - // - // It cancels the in-flight "send()" calls: their callers return - // promptly with the number of bytes actually written so far (best - // effort), instead of waiting for the event thread to finish them. - // - // This must not be called from within a listener callback, since the - // callbacks run on the QTcpSocket's event thread and this destroys the - // socket that is running them. - virtual void close() noexcept override; - - virtual void abort() noexcept override; - - virtual void connect(const std::string& address, uint16_t port) override; + // Requests immediate teardown of the active connection, without waiting + // for the QTcpSocket's event thread to destroy it. Safe to call from + // within a listener callback (which runs on that event thread). It marks + // the socket DESTRUCTING and aborts the underlying QTcpSocket; the + // resulting disconnect signals are no-ops. It cancels the in-flight + // "send()" calls (their callers return promptly with the bytes written + // so far). The socket is left in State::DESTRUCTING; the destructor's + // close_and_wait() destroys the QTcpSocket and returns it to NOT_RUNNING. + // No "on_disconnect" callback is fired. Safe to call multiple times. + void close() noexcept override; + + void connect(const std::string& address, uint16_t port) override; virtual size_t send( const void* data, @@ -89,6 +83,13 @@ class ClientSocket_Qt final : public QObject, public AbstractClientSocket{ private: + // Stops the connection, destroys the QTcpSocket on its event thread, and + // returns the socket to State::NOT_RUNNING. It blocks until the + // QTcpSocket has been destroyed, so it must not be called from within a + // listener callback. The public "close()" only requests the teardown; + // this actually destroys the socket, and is called by the destructor. + void close_and_wait() noexcept; + // The connection attempt or the connection itself ended in a failure. // This fires the appropriate callback and returns the socket to // State::NOT_RUNNING so "connect()" can be called again. If the diff --git a/Common/Cpp/Sockets/ClientSocket_WinSocket.cpp b/Common/Cpp/Sockets/ClientSocket_WinSocket.cpp index 9490020c57..13582dac62 100644 --- a/Common/Cpp/Sockets/ClientSocket_WinSocket.cpp +++ b/Common/Cpp/Sockets/ClientSocket_WinSocket.cpp @@ -18,7 +18,7 @@ namespace PokemonAutomation{ -void ClientSocket_WinSocket::close() noexcept{ +void ClientSocket_WinSocket::close_and_wait() noexcept{ AsyncTask thread; { std::lock_guard lg(m_lock); @@ -40,13 +40,14 @@ void ClientSocket_WinSocket::close() noexcept{ m_cv.notify_all(); } } -void ClientSocket_WinSocket::abort() noexcept{ - // Non-blocking teardown, safe to call from the receive thread. "close()" - // waits for the receive thread to exit and so must not run from a - // listener callback; this instead just flips the state and releases the - // socket. The receive thread's next iteration sees DESTRUCTING (or the - // missing socket) and exits on its own. The socket is left DESTRUCTING; - // a later close() returns it to NOT_RUNNING. +void ClientSocket_WinSocket::close() noexcept{ + // Non-blocking teardown, safe to call from the receive thread. The + // blocking "close_and_wait()" waits for the receive thread to exit and so + // must not run from a listener callback; this instead just flips the + // state and releases the socket. The receive thread's next iteration sees + // DESTRUCTING (or the missing socket) and exits on its own. The socket is + // left DESTRUCTING; the destructor's close_and_wait() returns it to + // NOT_RUNNING. { std::lock_guard lg(m_lock); if (m_state.load(std::memory_order_relaxed) == State::NOT_RUNNING && !m_thread){ @@ -58,6 +59,13 @@ void ClientSocket_WinSocket::abort() noexcept{ m_cv.notify_all(); } void ClientSocket_WinSocket::connect(const std::string& address, uint16_t port){ + // Complete any in-progress connection or a requested-but-unfinished + // close() before starting a new one. close() is non-blocking (it leaves + // the socket DESTRUCTING), so reconnecting on the same socket joins the + // old receive thread here to get back to NOT_RUNNING. No-op if the socket + // is already NOT_RUNNING. + close_and_wait(); + std::string error; { std::lock_guard lg(m_lock); @@ -99,7 +107,7 @@ void ClientSocket_WinSocket::connect(const std::string& address, uint16_t port){ // connect() may be called from a thread that is itself running on // this pool, and blocking here until a pool thread is free could // deadlock a capped pool. The returned AsyncTask still lets - // close() join the receive thread. + // close_and_wait() join the receive thread. try{ m_thread = m_thread_pool.dispatch([=, this]{ thread_loop(address, port); diff --git a/Common/Cpp/Sockets/ClientSocket_WinSocket.h b/Common/Cpp/Sockets/ClientSocket_WinSocket.h index 9de0d6bccf..53006000d1 100644 --- a/Common/Cpp/Sockets/ClientSocket_WinSocket.h +++ b/Common/Cpp/Sockets/ClientSocket_WinSocket.h @@ -29,25 +29,23 @@ class ClientSocket_WinSocket final : public AbstractClientSocket{ : m_thread_pool(thread_pool) {} - virtual ~ClientSocket_WinSocket(){ - close(); + ~ClientSocket_WinSocket() override{ + close_and_wait(); } - // Stops the receive thread, closes the socket, and returns the socket to - // State::NOT_RUNNING so "connect()" can be called again. This is safe to - // call multiple times. - // - // It cancels an in-flight "send()": the send loop re-checks the state - // every iteration, and "close()" notifies it, so the send returns within - // a millisecond with the bytes written so far. - // - // This blocks until the receive thread has exited, so it must not be - // called from within a listener callback (which runs on that thread). - virtual void close() noexcept override; - - virtual void abort() noexcept override; + // Requests immediate teardown of the active connection, without waiting + // for the receive thread to exit. Safe to call from within a listener + // callback (which runs on that thread). It marks the socket DESTRUCTING + // and releases the underlying socket; the receive thread exits on its + // next iteration. It cancels an in-flight "send()" (the send loop + // re-checks the state and returns within a millisecond with the bytes + // written so far). The socket is left in State::DESTRUCTING; the + // destructor's close_and_wait() joins the thread and returns it to + // NOT_RUNNING. No "on_disconnect" callback is fired. Safe to call + // multiple times. + void close() noexcept override; - virtual void connect(const std::string& address, uint16_t port) override; + void connect(const std::string& address, uint16_t port) override; virtual size_t send( const void* data, @@ -57,6 +55,13 @@ class ClientSocket_WinSocket final : public AbstractClientSocket{ private: + // Stops the receive thread, closes the socket, and returns the socket to + // State::NOT_RUNNING. It blocks until the receive thread has exited, so + // it must not be called from within a listener callback. The public + // "close()" only requests the teardown; this actually joins the thread, + // and is called by the destructor. + void close_and_wait() noexcept; + // Winsock has to be started before any of its functions can be used, and // nothing else in the program calls WSAStartup, so call it exactly once // before the first socket is created. We deliberately never call diff --git a/Common/Cpp/Sockets/Sockets_Tests.cpp b/Common/Cpp/Sockets/Sockets_Tests.cpp index 41a197a6c3..382110ebc7 100644 --- a/Common/Cpp/Sockets/Sockets_Tests.cpp +++ b/Common/Cpp/Sockets/Sockets_Tests.cpp @@ -89,14 +89,15 @@ struct ClientListener : AbstractClientSocket::Listener{ // A client listener that tears the connection down from within its own -// on_receive_data() callback via abort() - the exact scenario sys-botbase -// hits when it detects an oversized line. close() would deadlock here (it -// waits for this very receive thread to exit); abort() must return promptly. -struct AbortingClientListener : AbstractClientSocket::Listener{ +// on_receive_data() callback via close() - the exact scenario sys-botbase +// hits when it detects an oversized line. close() must return promptly here +// (it does not wait for this very receive thread to exit; the blocking +// close_and_wait() would deadlock). +struct ClosingClientListener : AbstractClientSocket::Listener{ Waiter waiter; std::atomic connect_finished{0}; std::string last_connect_error; - std::atomic aborted_in_callback{0}; + std::atomic closed_in_callback{0}; // Set to the owning client after construction; on_receive_data only // fires once connected and receiving, so it is valid by then. AbstractClientSocket* client = nullptr; @@ -108,8 +109,8 @@ struct AbortingClientListener : AbstractClientSocket::Listener{ } virtual void on_receive_data(const void*, size_t) override{ - client->abort(); - aborted_in_callback++; + client->close(); + closed_in_callback++; waiter.notify(); } }; @@ -1654,7 +1655,7 @@ class Test_Sockets_ClientSendTimeout : public TestSockets( @@ -1679,9 +1685,9 @@ class Test_Sockets_ClientSendTimeout : public TestSockets -class Test_Sockets_AbortFromReceiveCallback : public TestSockets{ +class Test_Sockets_CloseFromReceiveCallback : public TestSockets{ public: using TestSockets::TestSockets; @@ -2348,7 +2354,7 @@ class Test_Sockets_AbortFromReceiveCallback : public TestSocketsm_thread_pool); - AbortingClientListener client_listener; + ClosingClientListener client_listener; client_listener.client = &client; client.add_listener(client_listener); @@ -2369,15 +2375,15 @@ class Test_Sockets_AbortFromReceiveCallback : public TestSockets= 1; } + [&client_listener]{ return client_listener.closed_in_callback.load() >= 1; } )){ - error = "abort() did not return from the on_receive_data() callback (it blocked, like close() would)"; + error = "close() did not return from the on_receive_data() callback (it would have blocked, like close_and_wait() would)"; }else if (client.state() == AbstractClientSocket::State::CONNECTED){ - error = "After abort() the client is still CONNECTED"; + error = "After close() the client is still CONNECTED"; }else{ - // Now that abort() has stopped the receive thread, close() must - // complete without hanging. - client.close(); + // close() (called in the callback) only requested the + // teardown. The blocking close_and_wait() runs when "client" + // is destroyed at the end of this scope - it must not hang. ok = true; } } @@ -2429,8 +2435,8 @@ void add_socket_tests(UnitTestDatabase& database, ThreadPool& thread_pool, const database.add>( thread_pool, prefix + "close_connection() cancels an in-flight send" ); - database.add>( - thread_pool, prefix + "abort() from the receive callback" + database.add>( + thread_pool, prefix + "close() from the receive callback" ); } diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp b/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp index c95229b9a1..7cefbb5ea8 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.cpp @@ -260,11 +260,12 @@ void TcpSysbotBase_Connection::on_receive_overflow(){ COLOR_RED ); set_status_line1("Disconnected: sys-botbase sent an oversized line.", COLOR_RED); - // Safe to call from this receive-thread callback: unlike close(), it does - // not wait for the receive thread to exit (which would deadlock). The - // heartbeat thread_loop, if running, observes the DESTRUCTING state and - // stops on its next iteration. - m_socket.abort(); + // Safe to call from this receive-thread callback: close() requests the + // teardown without waiting for the receive thread to exit (the blocking + // close_and_wait() would deadlock here). The heartbeat thread_loop, if + // running, observes the DESTRUCTING state and stops on its next + // iteration. + m_socket.close(); } void TcpSysbotBase_Connection::process_message(const std::string& message, WallClock timestamp){ if (LOG_EVERYTHING()){ From 55dab711d7d35232a50ea41d1a5f0bd5d3a09174 Mon Sep 17 00:00:00 2001 From: PA Dev Bot <344001113+pa-devbot@users.noreply.github.com> Date: Sat, 7 Nov 2026 08:25:20 +0000 Subject: [PATCH 5/5] Sockets (tests): destroy listeners after their sockets The Mac CI crash on the rename commit (std::system_error "mutex lock failed: Invalid argument" thrown from a socket listener callback, on both Mac architectures) was a use-after-free of the test listeners: with close() now non-blocking, the socket keeps delivering callbacks until the socket's destructor runs, but every socket test declared its listeners after the sockets, so at scope exit each listener was destroyed first - with the socket's thread still able to call back into it (pthread_mutex_lock on the freed Waiter's mutex returns EINVAL on macOS, which Qt turns into std::terminate). Declare every listener before the socket that adds it in all 15 socket tests, so listeners are destroyed after their sockets, and document the lifetime requirement on the Listener structs. --- Common/Cpp/Sockets/AbstractClientSocket.h | 6 ++ Common/Cpp/Sockets/AbstractServerSocket.h | 6 ++ Common/Cpp/Sockets/Sockets_Tests.cpp | 68 +++++++++++------------ 3 files changed, 46 insertions(+), 34 deletions(-) diff --git a/Common/Cpp/Sockets/AbstractClientSocket.h b/Common/Cpp/Sockets/AbstractClientSocket.h index fd7fb408e3..393d616f81 100644 --- a/Common/Cpp/Sockets/AbstractClientSocket.h +++ b/Common/Cpp/Sockets/AbstractClientSocket.h @@ -25,6 +25,12 @@ class AbstractClientSocket{ }; struct Listener{ + // A listener must outlive the socket that calls it. Callbacks can + // still be delivered after a non-blocking "close()" returns - the + // socket's threads only stop when the socket itself is destroyed - + // so a listener must not be destroyed before the socket that added + // it. Declare the listener before the socket, so that it is + // destroyed after the socket. // Called at the start of the receiver thread. This can be used to set // the thread priority of the thread. virtual void on_thread_start(){} diff --git a/Common/Cpp/Sockets/AbstractServerSocket.h b/Common/Cpp/Sockets/AbstractServerSocket.h index bedb12560e..175b48384f 100644 --- a/Common/Cpp/Sockets/AbstractServerSocket.h +++ b/Common/Cpp/Sockets/AbstractServerSocket.h @@ -30,6 +30,12 @@ class AbstractServerSocket{ }; struct Listener{ + // A listener must outlive the socket that calls it. Callbacks can + // still be delivered after "close_connection()" returns - the + // connection's threads only stop when the connection (or the + // socket) is destroyed - so a listener must not be destroyed + // before the socket that added it. Declare the listener before + // the socket, so that it is destroyed after the socket. // Called at the start of the thread that will handle a connection. // This can be used to set the thread priority of the thread. virtual void on_thread_start(){} diff --git a/Common/Cpp/Sockets/Sockets_Tests.cpp b/Common/Cpp/Sockets/Sockets_Tests.cpp index 382110ebc7..ba1dc66ec8 100644 --- a/Common/Cpp/Sockets/Sockets_Tests.cpp +++ b/Common/Cpp/Sockets/Sockets_Tests.cpp @@ -266,8 +266,8 @@ class Test_Sockets_Echo : public TestSockets "Machine too starved for a timing-sensitive socket test (timing_unreliable())." ); } - ServerSocketType server(this->m_thread_pool); ServerListener server_listener; + ServerSocketType server(this->m_thread_pool); server_listener.server = &server; server.add_listener(server_listener); @@ -287,8 +287,8 @@ class Test_Sockets_Echo : public TestSockets bool ok = false; std::string error; { - ClientSocketType client(this->m_thread_pool); ClientListener client_listener; + ClientSocketType client(this->m_thread_pool); client.add_listener(client_listener); if (!this->connect_client(client, client_listener, port, 1)){ @@ -352,8 +352,8 @@ class Test_Sockets_ZeroLengthSend : public TestSocketsm_thread_pool); ServerListener server_listener; + ServerSocketType server(this->m_thread_pool); server_listener.server = &server; server.add_listener(server_listener); @@ -373,8 +373,8 @@ class Test_Sockets_ZeroLengthSend : public TestSocketsm_thread_pool); ClientListener client_listener; + ClientSocketType client(this->m_thread_pool); client.add_listener(client_listener); if (!this->connect_client(client, client_listener, port, 1)){ @@ -437,8 +437,8 @@ class Test_Sockets_LargeTransfer : public TestSocketsm_thread_pool); ServerListener server_listener; + ServerSocketType server(this->m_thread_pool); server_listener.server = &server; server.add_listener(server_listener); @@ -458,8 +458,8 @@ class Test_Sockets_LargeTransfer : public TestSocketsm_thread_pool); ClientListener client_listener; + ClientSocketType client(this->m_thread_pool); client.add_listener(client_listener); if (!this->connect_client(client, client_listener, port, 1)){ @@ -516,8 +516,8 @@ class Test_Sockets_RapidReconnect : public TestSocketsm_thread_pool); ServerListener server_listener; + ServerSocketType server(this->m_thread_pool); server_listener.server = &server; server.add_listener(server_listener); @@ -538,8 +538,8 @@ class Test_Sockets_RapidReconnect : public TestSocketsm_thread_pool); ClientListener client_listener; + ClientSocketType client(this->m_thread_pool); client.add_listener(client_listener); for (int cycle = 0; cycle < CYCLES; cycle++){ @@ -620,8 +620,8 @@ class Test_Sockets_SendCloseRace : public TestSockets payload(SIZE, 0xAB); - ServerSocketType server(this->m_thread_pool); ServerListener server_listener; + ServerSocketType server(this->m_thread_pool); server_listener.server = &server; server.add_listener(server_listener); @@ -641,8 +641,8 @@ class Test_Sockets_SendCloseRace : public TestSocketsm_thread_pool); ClientListener client_listener; + ClientSocketType client(this->m_thread_pool); client.add_listener(client_listener); if (!this->connect_client(client, client_listener, port, 1)){ @@ -751,8 +751,8 @@ class Test_Sockets_RejectSecondClient : public TestSocketsm_thread_pool); ServerListener server_listener; + ServerSocketType server(this->m_thread_pool); server_listener.server = &server; server.add_listener(server_listener); @@ -772,12 +772,12 @@ class Test_Sockets_RejectSecondClient : public TestSocketsm_thread_pool); ClientListener client_a_listener; + ClientSocketType client_a(this->m_thread_pool); client_a.add_listener(client_a_listener); - ClientSocketType client_b(this->m_thread_pool); ClientListener client_b_listener; + ClientSocketType client_b(this->m_thread_pool); client_b.add_listener(client_b_listener); if (!this->connect_client(client_a, client_a_listener, port, 1)){ @@ -848,8 +848,8 @@ class Test_Sockets_PortInUse : public TestSocketsm_thread_pool); ServerListener server_listener; + ServerSocketType server(this->m_thread_pool); server_listener.server = &server; server.add_listener(server_listener); @@ -866,8 +866,8 @@ class Test_Sockets_PortInUse : public TestSocketsm_thread_pool); ServerListener second_listener; + ServerSocketType second_server(this->m_thread_pool); second_server.add_listener(second_listener); second_server.start("127.0.0.1", port); if (!second_listener.waiter.wait_for( @@ -891,8 +891,8 @@ class Test_Sockets_PortInUse : public TestSocketsm_thread_pool); ClientListener client_listener; + ClientSocketType client(this->m_thread_pool); client.add_listener(client_listener); if (!this->connect_client(client, client_listener, port, 1)){ @@ -952,11 +952,11 @@ class Test_Sockets_Qt_CompletionLifetime : public UnitTest{ } }; - ServerSocket_Qt server(m_thread_pool); ServerListener server_listener; + StallListener stall_listener; + ServerSocket_Qt server(m_thread_pool); server_listener.server = &server; server.add_listener(server_listener); - StallListener stall_listener; server.add_listener(stall_listener); uint16_t port = 0; @@ -981,8 +981,8 @@ class Test_Sockets_Qt_CompletionLifetime : public UnitTest{ bool ok = false; std::string error; { - ClientSocket_Qt client(m_thread_pool); ClientListener client_listener; + ClientSocket_Qt client(m_thread_pool); client.add_listener(client_listener); client.connect("127.0.0.1", port); if (!client_listener.waiter.wait_for( @@ -1076,8 +1076,8 @@ class Test_Sockets_Qt_CloseCancelsSend : public UnitTest{ } }; - ServerSocket_Qt server(m_thread_pool); ServerListener server_listener; + ServerSocket_Qt server(m_thread_pool); server_listener.server = &server; server.add_listener(server_listener); @@ -1109,9 +1109,9 @@ class Test_Sockets_Qt_CloseCancelsSend : public UnitTest{ bool ok = false; std::string error; { - ClientSocket_Qt client(m_thread_pool); ClientListener client_listener; StallingClientListener stall_listener; + ClientSocket_Qt client(m_thread_pool); client.add_listener(client_listener); client.add_listener(stall_listener); client.connect("127.0.0.1", port); @@ -1290,10 +1290,10 @@ class Test_Sockets_Qt_CloseConnectionCancelsSend : public UnitTest{ } }; - ServerSocket_Qt server(m_thread_pool); ServerListener server_listener; - server.add_listener(server_listener); StallingServerListener stall_listener; + ServerSocket_Qt server(m_thread_pool); + server.add_listener(server_listener); server.add_listener(stall_listener); uint16_t port = 0; @@ -1324,8 +1324,8 @@ class Test_Sockets_Qt_CloseConnectionCancelsSend : public UnitTest{ bool ok = false; std::string error; { - ClientSocket_Qt client(m_thread_pool); ClientListener client_listener; + ClientSocket_Qt client(m_thread_pool); client.add_listener(client_listener); client.connect("127.0.0.1", port); if (!client_listener.waiter.wait_for( @@ -1547,8 +1547,8 @@ class Test_Sockets_ClientSendTimeout : public TestSocketsm_thread_pool); StallingServerListener server_listener; + ServerSocketType server(this->m_thread_pool); server.add_listener(server_listener); uint16_t port; @@ -1567,8 +1567,8 @@ class Test_Sockets_ClientSendTimeout : public TestSocketsm_thread_pool); ClientListener client_listener; + ClientSocketType client(this->m_thread_pool); client.add_listener(client_listener); if (!this->connect_client(client, client_listener, port, 1)){ @@ -1779,8 +1779,8 @@ class Test_Sockets_ServerSendTimeout : public TestSocketsm_thread_pool); ServerListener server_listener; + ServerSocketType server(this->m_thread_pool); server.add_listener(server_listener); uint16_t port; @@ -1799,8 +1799,8 @@ class Test_Sockets_ServerSendTimeout : public TestSocketsm_thread_pool); StallingClientListener client_listener; + ClientSocketType client(this->m_thread_pool); client.add_listener(client_listener); if (!this->connect_client(client, client_listener, port, 1)){ @@ -2008,8 +2008,8 @@ class Test_Sockets_CloseCancelsSend : public TestSocketsm_thread_pool); StallingServerListener server_listener; + ServerSocketType server(this->m_thread_pool); server.add_listener(server_listener); uint16_t port; @@ -2028,8 +2028,8 @@ class Test_Sockets_CloseCancelsSend : public TestSocketsm_thread_pool); ClientListener client_listener; + ClientSocketType client(this->m_thread_pool); client.add_listener(client_listener); if (!this->connect_client(client, client_listener, port, 1)){ @@ -2178,8 +2178,8 @@ class Test_Sockets_CloseConnectionCancelsSend : public TestSocketsm_thread_pool); ServerListener server_listener; + ServerSocketType server(this->m_thread_pool); server.add_listener(server_listener); uint16_t port; @@ -2198,8 +2198,8 @@ class Test_Sockets_CloseConnectionCancelsSend : public TestSocketsm_thread_pool); StallingClientListener client_listener; + ClientSocketType client(this->m_thread_pool); client.add_listener(client_listener); if (!this->connect_client(client, client_listener, port, 1)){ @@ -2335,8 +2335,8 @@ class Test_Sockets_CloseFromReceiveCallback : public TestSocketsm_thread_pool); ServerListener server_listener; + ServerSocketType server(this->m_thread_pool); server.add_listener(server_listener); uint16_t port; @@ -2353,8 +2353,8 @@ class Test_Sockets_CloseFromReceiveCallback : public TestSocketsm_thread_pool); ClosingClientListener client_listener; + ClientSocketType client(this->m_thread_pool); client_listener.client = &client; client.add_listener(client_listener);