diff --git a/Common/Cpp/Sockets/AbstractServerSocket2.h b/Common/Cpp/Sockets/AbstractServerSocket2.h index 1912e50301..0b5b0d8a1e 100644 --- a/Common/Cpp/Sockets/AbstractServerSocket2.h +++ b/Common/Cpp/Sockets/AbstractServerSocket2.h @@ -23,7 +23,7 @@ * This class coexists with AbstractServerSocket while the new shape * is proven out; the two are not interchangeable yet. * - * Thread safety: "start()", "active_connection()", "state()", + * Thread safety: "start()", "active_connection()", * "add_listener()" and "remove_listener()" are safe to call from any * thread. Listener callbacks may be called from any of the backend's * worker threads and may overlap in time; it is always safe to call @@ -37,7 +37,6 @@ #ifndef PokemonAutomation_Sockets_AbstractServerSocket2_H #define PokemonAutomation_Sockets_AbstractServerSocket2_H -#include #include #include #include @@ -52,13 +51,6 @@ class AbstractServerSocket2{ public: using ConnectionPtr = std::shared_ptr; - enum class State{ - NOT_RUNNING, - LISTENING, - CONNECTED, - DESTRUCTING, - }; - struct Listener{ // Called when "start()" finishes. // If successful, "error_message" is empty. @@ -95,20 +87,14 @@ class AbstractServerSocket2{ public: - AbstractServerSocket2() - : m_state(State::NOT_RUNNING) - {} + AbstractServerSocket2() = default; virtual ~AbstractServerSocket2() = default; - State state() const{ - return m_state.load(std::memory_order_relaxed); - } - // Binds to "address" and listens on "port". // Use "0.0.0.0" to accept connections on all interfaces. - // Only acts while "state()" is NOT_RUNNING and no start attempt is + // Only acts while the server is not running and no start attempt is // still in flight; otherwise it is a no-op. A start that fails (for - // example, the port is busy) leaves the server NOT_RUNNING, so it + // example, the port is busy) leaves the server not running, so it // may be retried. The server stops only when it is destroyed. virtual void start(const std::string& address, uint16_t port) = 0; @@ -120,7 +106,6 @@ class AbstractServerSocket2{ protected: - std::atomic m_state; ListenerSet m_listeners; }; diff --git a/Common/Cpp/Sockets/ServerSocket2_POSIX.cpp b/Common/Cpp/Sockets/ServerSocket2_POSIX.cpp index b22f1752b8..739004b30f 100644 --- a/Common/Cpp/Sockets/ServerSocket2_POSIX.cpp +++ b/Common/Cpp/Sockets/ServerSocket2_POSIX.cpp @@ -90,11 +90,22 @@ void ServerSocket2Connection_POSIX::close(){ return; } m_open.store(false, std::memory_order_release); + // Hand the CONNECTED state back before the close becomes visible + // to the peer: a client reconnecting after it observes this close + // must be accepted, not rejected by a state that is still winding + // down. (A no-op unless this connection is still the active one.) + m_server->release_connection(this); // Tells the connection thread to stop; it closes the socket // itself. Safe to call from within a listener callback. (If the // socket is already gone, ::shutdown just fails harmlessly.) ::shutdown(m_client, SHUT_RDWR); } +void ServerSocket2Connection_POSIX::start_thread(ThreadPool& thread_pool){ + m_thread = thread_pool.dispatch([this]{ + thread_loop(); + }); +} + void ServerSocket2Connection_POSIX::thread_loop(){ int socket = m_client; @@ -102,7 +113,7 @@ void ServerSocket2Connection_POSIX::thread_loop(){ // Closed before this thread started (teardown, or the // application via active_connection()): hand the state back and // drop the socket; there is nothing to report. - m_server->release_connection(); + m_server->release_connection(this); ::close(socket); return; } @@ -159,7 +170,7 @@ void ServerSocket2Connection_POSIX::thread_loop(){ // Hand the state back to the server, then make the connection // inert. Either way, the object outlives the server. - m_server->release_connection(); + m_server->release_connection(this); const bool app_close = m_close_requested.load(std::memory_order_relaxed); m_open.store(false, std::memory_order_release); ::close(socket); @@ -500,9 +511,11 @@ void ServerSocket2_POSIX::report_disconnect(const std::string& error){ } } -void ServerSocket2_POSIX::release_connection(){ +void ServerSocket2_POSIX::release_connection(ServerSocket2Connection_POSIX* self){ std::lock_guard lg(m_lock); - if (m_state.load(std::memory_order_relaxed) == State::CONNECTED){ + if (m_state.load(std::memory_order_relaxed) == State::CONNECTED + && m_active.get() == self + ){ m_state.store(State::LISTENING, std::memory_order_release); } // The m_active slot stays: it anchors the connection's lifetime diff --git a/Common/Cpp/Sockets/ServerSocket2_POSIX.h b/Common/Cpp/Sockets/ServerSocket2_POSIX.h index f97e7430c3..b4575b2099 100644 --- a/Common/Cpp/Sockets/ServerSocket2_POSIX.h +++ b/Common/Cpp/Sockets/ServerSocket2_POSIX.h @@ -69,12 +69,9 @@ class ServerSocket2Connection_POSIX final : public AbstractServerSocketConnectio // Dispatches the thread. The server calls this exactly once, right // after it has published the connection in m_active (which is what - // keeps the object alive while thread_loop() runs). - void start_thread(ThreadPool& thread_pool){ - m_thread = thread_pool.dispatch([this]{ - thread_loop(); - }); - } + // keeps the object alive while thread_loop() runs). Defined in the + // .cpp: it needs the complete ThreadPool type. + void start_thread(ThreadPool& thread_pool); // Joins the thread. The server calls this before it drops its // m_active reference - to replace the slot or to tear down - because // m_active is what keeps the object alive while thread_loop() runs. @@ -142,10 +139,16 @@ class ServerSocket2_POSIX final : public AbstractServerSocket2{ void report_connect(); void report_receive(const void* data, size_t bytes); void report_disconnect(const std::string& error); - // Hands the CONNECTED state back to the server so it can accept the - // next client. The m_active slot is kept: it anchors the connection's - // lifetime while its thread runs. - void release_connection(); + // If "self" is still the active connection, hand its CONNECTED + // state back to the server. Called by the connection thread at + // exit, and by close() before the close becomes visible to the + // peer - a client reconnecting after it observes the close must + // not be rejected by a state that is still winding down. No-op + // otherwise (including for a connection that has already been + // replaced). The m_active slot is kept either way: it anchors the + // connection's lifetime while its thread runs, and the server drops + // the slot only after it has joined that thread. + void release_connection(ServerSocket2Connection_POSIX* self); // A start attempt failed (or the server was torn down while the task // was still starting up): mark the task dead so that a retrying @@ -157,6 +160,16 @@ class ServerSocket2_POSIX final : public AbstractServerSocket2{ private: friend class ServerSocket2Connection_POSIX; + // The backend's internal state machine; not part of the application + // API (the listeners and active_connection() are the observables). + enum class State{ + NOT_RUNNING, + LISTENING, + CONNECTED, + DESTRUCTING, + }; + std::atomic m_state{State::NOT_RUNNING}; + ThreadPool& m_thread_pool; Mutex m_lock; ConditionVariable m_cv; diff --git a/Common/Cpp/Sockets/ServerSocket2_Qt.h b/Common/Cpp/Sockets/ServerSocket2_Qt.h index a13f35b118..0f9ac23b42 100644 --- a/Common/Cpp/Sockets/ServerSocket2_Qt.h +++ b/Common/Cpp/Sockets/ServerSocket2_Qt.h @@ -224,6 +224,16 @@ class ServerSocket2_Qt final : public QObject, public AbstractServerSocket2{ private: friend class ServerSocket2Connection_Qt; + // The backend's internal state machine; not part of the application + // API (the listeners and active_connection() are the observables). + enum class State{ + NOT_RUNNING, + LISTENING, + CONNECTED, + DESTRUCTING, + }; + std::atomic m_state{State::NOT_RUNNING}; + // Queues a connection's send on the event thread (or runs it inline // when called from that thread). Called from the shell's "send()" // with the shell's lock held, which the write loop's diff --git a/Common/Cpp/Sockets/ServerSocket2_Tests.cpp b/Common/Cpp/Sockets/ServerSocket2_Tests.cpp index 1fe5976c27..eef77e4371 100644 --- a/Common/Cpp/Sockets/ServerSocket2_Tests.cpp +++ b/Common/Cpp/Sockets/ServerSocket2_Tests.cpp @@ -653,20 +653,20 @@ class Test_ServerSocket2_SendCloseRace : public TestServerSocket2 finished_before; + } + )){ + return "A failed start left the server un-retryable"; + } + if (second_listener.last_listen_error.empty()){ + return "The retried start succeeded on a port that is still in use"; } // The original server must still work. @@ -1093,8 +1107,6 @@ class Test_ServerSocket2_PeerClose : public TestServerSocket2close(); // Idempotent. } - // The server goes back to LISTENING (eventually). + // The active slot clears (eventually; the close lands + // within a few milliseconds on the thread backends and + // as a queued call on the Qt backend). if (error.empty()){ const auto s0 = std::chrono::steady_clock::now(); - while (server.state() != AbstractServerSocket2::State::LISTENING + while (server.active_connection() != nullptr && std::chrono::steady_clock::now() - s0 < SOCKET_TEST_WAIT){ std::this_thread::sleep_for(std::chrono::milliseconds(10)); } - if (server.state() != AbstractServerSocket2::State::LISTENING){ - error = "The server is not LISTENING after the app close"; + if (server.active_connection() != nullptr){ + error = "The active connection was not cleared after the app close"; } } @@ -1819,21 +1833,21 @@ class Test_ServerSocket2_Qt_CloseUnderStall : public UnitTest{ }else{ // close() returns with the abort still queued // behind the (stalled) event thread, so poll - // for the resulting LISTENING state within the + // for the active slot to clear within the // shared budget before judging it. const auto s0 = std::chrono::steady_clock::now(); - while (server.state() != AbstractServerSocket2::State::LISTENING + while (server.active_connection() != nullptr && std::chrono::steady_clock::now() - s0 < SOCKET_TEST_WAIT){ std::this_thread::sleep_for(std::chrono::milliseconds(10)); } - if (server.state() != AbstractServerSocket2::State::LISTENING){ + if (server.active_connection() != nullptr){ if (timing_unreliable()){ return UnitTestResult( UnitTestResult::SKIPPED, - "The server is not LISTENING after close() on a starved machine." + "The active connection was not cleared after close() on a starved machine." ); } - error = "The server is not LISTENING after close()"; + error = "The active connection was not cleared after close()"; } } } @@ -1957,6 +1971,20 @@ class StubServer2 final : public AbstractServerSocket2{ return m_active; } + // Test-only observables of the base's internal state machine: the + // application-facing API has no state() accessor, and the State + // enum is protected (MSVC will not let the tests name it through a + // using-declaration). + bool is_not_running() const{ + return m_state.load(std::memory_order_relaxed) == State::NOT_RUNNING; + } + bool is_listening() const{ + return m_state.load(std::memory_order_relaxed) == State::LISTENING; + } + bool is_connected() const{ + return m_state.load(std::memory_order_relaxed) == State::CONNECTED; + } + // Stub hooks: simulate the backend's event delivery. // A client connected. Rejected (on_connect_rejected) if a @@ -2023,6 +2051,16 @@ class StubServer2 final : public AbstractServerSocket2{ } } + // The stub's own state machine (the backends' is theirs to keep + // and no longer part of the abstract class). + enum class State{ + NOT_RUNNING, + LISTENING, + CONNECTED, + DESTRUCTING, + }; + std::atomic m_state{State::NOT_RUNNING}; + ConnectionPtr m_active; }; @@ -2124,7 +2162,7 @@ class Test_ServerSocket2_StubApi : public UnitTest{ } server.start("127.0.0.1", 0); - if (server.state() != AbstractServerSocket2::State::LISTENING){ + if (!server.is_listening()){ return UnitTestResult("start() should leave the server LISTENING"); } @@ -2134,7 +2172,7 @@ class Test_ServerSocket2_StubApi : public UnitTest{ } first = probe.connection; if (server.active_connection() != first - || server.state() != AbstractServerSocket2::State::CONNECTED + || !server.is_connected() ){ return UnitTestResult("active_connection()/state() disagree with on_connect()"); } @@ -2168,7 +2206,7 @@ class Test_ServerSocket2_StubApi : public UnitTest{ return UnitTestResult("send() on a closed connection should return 0"); } if (server.active_connection() != nullptr - || server.state() != AbstractServerSocket2::State::LISTENING + || !server.is_listening() ){ return UnitTestResult("after the close, the server should be LISTENING with no active connection"); } @@ -2185,7 +2223,7 @@ class Test_ServerSocket2_StubApi : public UnitTest{ return UnitTestResult("a clean peer close should fire on_disconnect() once, with no error"); } if (second->is_open() || server.active_connection() != nullptr - || server.state() != AbstractServerSocket2::State::LISTENING + || !server.is_listening() ){ return UnitTestResult("after the peer close, the server should be LISTENING with no active connection"); } diff --git a/Common/Cpp/Sockets/ServerSocket2_WinSocket.cpp b/Common/Cpp/Sockets/ServerSocket2_WinSocket.cpp index 545b24bcdd..74b0371e45 100644 --- a/Common/Cpp/Sockets/ServerSocket2_WinSocket.cpp +++ b/Common/Cpp/Sockets/ServerSocket2_WinSocket.cpp @@ -87,11 +87,22 @@ void ServerSocket2Connection_WinSocket::close(){ return; } m_open.store(false, std::memory_order_release); + // Hand the CONNECTED state back before the close becomes visible + // to the peer: a client reconnecting after it observes this close + // must be accepted, not rejected by a state that is still winding + // down. (A no-op unless this connection is still the active one.) + m_server->release_connection(this); // Tells the connection thread to stop; it closes the socket // itself. Safe to call from within a listener callback. (If the // socket is already gone, ::shutdown just fails harmlessly.) ::shutdown(m_client, SD_BOTH); } +void ServerSocket2Connection_WinSocket::start_thread(ThreadPool& thread_pool){ + m_thread = thread_pool.dispatch([this]{ + thread_loop(); + }); +} + void ServerSocket2Connection_WinSocket::thread_loop(){ SOCKET socket = m_client; @@ -99,7 +110,7 @@ void ServerSocket2Connection_WinSocket::thread_loop(){ // Closed before this thread started (teardown, or the // application via active_connection()): hand the state back and // drop the socket; there is nothing to report. - m_server->release_connection(); + m_server->release_connection(this); ::closesocket(socket); return; } @@ -150,7 +161,7 @@ void ServerSocket2Connection_WinSocket::thread_loop(){ // Hand the state back to the server, then make the connection // inert. Either way, the object outlives the server. - m_server->release_connection(); + m_server->release_connection(this); const bool app_close = m_close_requested.load(std::memory_order_relaxed); m_open.store(false, std::memory_order_release); ::closesocket(socket); @@ -518,9 +529,11 @@ void ServerSocket2_WinSocket::report_disconnect(const std::string& error){ } } -void ServerSocket2_WinSocket::release_connection(){ +void ServerSocket2_WinSocket::release_connection(ServerSocket2Connection_WinSocket* self){ std::lock_guard lg(m_lock); - if (m_state.load(std::memory_order_relaxed) == State::CONNECTED){ + if (m_state.load(std::memory_order_relaxed) == State::CONNECTED + && m_active.get() == self + ){ m_state.store(State::LISTENING, std::memory_order_release); } // The m_active slot stays: it anchors the connection's lifetime diff --git a/Common/Cpp/Sockets/ServerSocket2_WinSocket.h b/Common/Cpp/Sockets/ServerSocket2_WinSocket.h index 9f68d50c3d..52d88d9f72 100644 --- a/Common/Cpp/Sockets/ServerSocket2_WinSocket.h +++ b/Common/Cpp/Sockets/ServerSocket2_WinSocket.h @@ -71,12 +71,9 @@ class ServerSocket2Connection_WinSocket final : public AbstractServerSocketConne // Dispatches the thread. The server calls this exactly once, right // after it has published the connection in m_active (which is what - // keeps the object alive while thread_loop() runs). - void start_thread(ThreadPool& thread_pool){ - m_thread = thread_pool.dispatch([this]{ - thread_loop(); - }); - } + // keeps the object alive while thread_loop() runs). Defined in the + // .cpp: it needs the complete ThreadPool type. + void start_thread(ThreadPool& thread_pool); // Joins the thread. The server calls this before it drops its // m_active reference - to replace the slot or to tear down - because // m_active is what keeps the object alive while thread_loop() runs. @@ -147,10 +144,16 @@ class ServerSocket2_WinSocket final : public AbstractServerSocket2{ void report_connect(); void report_receive(const void* data, size_t bytes); void report_disconnect(const std::string& error); - // Hands the CONNECTED state back to the server so it can accept the - // next client. The m_active slot is kept: it anchors the connection's - // lifetime while its thread runs. - void release_connection(); + // If "self" is still the active connection, hand its CONNECTED + // state back to the server. Called by the connection thread at + // exit, and by close() before the close becomes visible to the + // peer - a client reconnecting after it observes the close must + // not be rejected by a state that is still winding down. No-op + // otherwise (including for a connection that has already been + // replaced). The m_active slot is kept either way: it anchors the + // connection's lifetime while its thread runs, and the server drops + // the slot only after it has joined that thread. + void release_connection(ServerSocket2Connection_WinSocket* self); // A start attempt failed (or the server was torn down while the task // was still starting up): mark the task dead so that a retrying @@ -162,6 +165,16 @@ class ServerSocket2_WinSocket final : public AbstractServerSocket2{ private: friend class ServerSocket2Connection_WinSocket; + // The backend's internal state machine; not part of the application + // API (the listeners and active_connection() are the observables). + enum class State{ + NOT_RUNNING, + LISTENING, + CONNECTED, + DESTRUCTING, + }; + std::atomic m_state{State::NOT_RUNNING}; + ThreadPool& m_thread_pool; Mutex m_lock; ConditionVariable m_cv;