diff --git a/Common/Cpp/Sockets/AbstractClientSocket.h b/Common/Cpp/Sockets/AbstractClientSocket.h index f7555db105..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(){} @@ -61,20 +67,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; + 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/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/ClientSocket_POSIX.cpp b/Common/Cpp/Sockets/ClientSocket_POSIX.cpp index 95472483af..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,8 +50,33 @@ void ClientSocket_POSIX::close() noexcept{ m_cv.notify_all(); } } +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){ + 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){ + // 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); @@ -91,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 d88beaed21..2c9ce20bb1 100644 --- a/Common/Cpp/Sockets/ClientSocket_POSIX.h +++ b/Common/Cpp/Sockets/ClientSocket_POSIX.h @@ -25,23 +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 connect(const std::string& address, uint16_t port) 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; + + void connect(const std::string& address, uint16_t port) override; virtual size_t send( const void* data, @@ -51,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 768d2cfec5..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,43 @@ void ClientSocket_Qt::close() noexcept{ std::lock_guard lg(m_lock); m_state.store(State::NOT_RUNNING, std::memory_order_release); } +void ClientSocket_Qt::close() 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; + } + + // 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(); + 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 2e7c0f6918..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,26 +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 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, @@ -87,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 a815d7f5b6..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,7 +40,32 @@ void ClientSocket_WinSocket::close() noexcept{ m_cv.notify_all(); } } +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){ + 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){ + // 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); @@ -82,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 a67f01e683..53006000d1 100644 --- a/Common/Cpp/Sockets/ClientSocket_WinSocket.h +++ b/Common/Cpp/Sockets/ClientSocket_WinSocket.h @@ -29,23 +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 connect(const std::string& address, uint16_t port) 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; + + void connect(const std::string& address, uint16_t port) override; virtual size_t send( const void* data, @@ -55,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 d4e2c014e9..ba1dc66ec8 100644 --- a/Common/Cpp/Sockets/Sockets_Tests.cpp +++ b/Common/Cpp/Sockets/Sockets_Tests.cpp @@ -88,6 +88,34 @@ struct ClientListener : AbstractClientSocket::Listener{ }; +// A client listener that tears the connection down from within its own +// 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 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; + + 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->close(); + closed_in_callback++; + waiter.notify(); + } +}; + + struct ServerListener : AbstractServerSocket::Listener{ Waiter waiter; std::atomic listen_finished{0}; @@ -238,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); @@ -259,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)){ @@ -324,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); @@ -345,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)){ @@ -409,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); @@ -430,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)){ @@ -488,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); @@ -510,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++){ @@ -592,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); @@ -613,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)){ @@ -723,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); @@ -744,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)){ @@ -820,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); @@ -838,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( @@ -863,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)){ @@ -924,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; @@ -953,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( @@ -1048,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); @@ -1081,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); @@ -1262,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; @@ -1296,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( @@ -1519,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; @@ -1539,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)){ @@ -1627,7 +1655,7 @@ class Test_Sockets_ClientSendTimeout : public TestSockets( @@ -1652,9 +1685,9 @@ class Test_Sockets_ClientSendTimeout : public TestSocketsm_thread_pool); ServerListener server_listener; + ServerSocketType server(this->m_thread_pool); server.add_listener(server_listener); uint16_t port; @@ -1766,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)){ @@ -1975,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; @@ -1995,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)){ @@ -2145,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; @@ -2165,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)){ @@ -2285,6 +2318,86 @@ class Test_Sockets_CloseConnectionCancelsSend : public TestSockets +class Test_Sockets_CloseFromReceiveCallback : 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())." + ); + } + ServerListener server_listener; + ServerSocketType server(this->m_thread_pool); + 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; + { + ClosingClientListener client_listener; + ClientSocketType client(this->m_thread_pool); + 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.closed_in_callback.load() >= 1; } + )){ + 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 close() the client is still CONNECTED"; + }else{ + // 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; + } + } + + 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 +2435,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 + "close() 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..7cefbb5ea8 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" @@ -28,6 +29,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 @@ -174,36 +182,91 @@ void TcpSysbotBase_Connection::on_disconnect(const std::string& error_message){ set_status_line1("Disconnected: " + error_message, COLOR_RED); }catch (...){} } +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, + 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_data(const void* data, size_t bytes){ // cout << "on_receive_data(): " << std::string((const char*)data, bytes - 2) << endl; 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 (...){} } +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: 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()){ m_logger.log("Received: " + message, COLOR_DARKGREEN); @@ -369,6 +432,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; + 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..cdcae5c6c3 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h @@ -64,6 +64,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 +94,11 @@ class TcpSysbotBase_Connection : public ControllerConnection, private ClientSock // void add_tests_SysbotBasePing(UnitTestDatabase& database); +// +// Registers unit tests for the receive-line length bound. +// +void add_tests_SysbotBaseReceiveLine(UnitTestDatabase& database); + }