diff --git a/Common/Cpp/Sockets/AbstractClientSocket.h b/Common/Cpp/Sockets/AbstractClientSocket.h index 393d616f81..37f3ddb0d4 100644 --- a/Common/Cpp/Sockets/AbstractClientSocket.h +++ b/Common/Cpp/Sockets/AbstractClientSocket.h @@ -76,10 +76,10 @@ class AbstractClientSocket{ // 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. + // teardown (joins the thread and returns it to NOT_RUNNING). A later + // "connect()" on the same socket completes that teardown first, so + // the socket can be reused. 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; diff --git a/Common/Cpp/Sockets/AbstractServerSocket.h b/Common/Cpp/Sockets/AbstractServerSocket.h index 175b48384f..7a0c1a28a8 100644 --- a/Common/Cpp/Sockets/AbstractServerSocket.h +++ b/Common/Cpp/Sockets/AbstractServerSocket.h @@ -85,26 +85,28 @@ class AbstractServerSocket{ // State::LISTENING on success or State::NOT_RUNNING on failure. virtual void start(const std::string& address, uint16_t port) = 0; - // Closes the active connection, if any, and keeps listening. Unlike - // "close()", this does not wait for the worker threads, so it is safe - // to call from within a listener callback. "Listener::on_disconnect" - // is not called for the connection closed this way. + // Closes the active connection, if any, and keeps listening. It does + // not wait for the worker threads, so it is safe to call from within + // a listener callback. "Listener::on_disconnect" is not called for + // the connection closed this way. // // It cancels any in-flight "send()" the same way "close()" does: the // send returns promptly with the number of bytes actually written so // far (best effort). virtual void close_connection() = 0; - // Stops the server: it stops accepting new connections, closes the - // active connection, and returns the socket to State::NOT_RUNNING so - // "start()" can be called again. This is safe to call multiple times. - // - // It cancels any in-flight "send()": the send returns promptly with - // the number of bytes actually written so far (best effort). - // - // This blocks until the worker threads have exited, so it must not be - // called from within a listener callback (which runs on one of those - // threads). + // Requests immediate teardown of the server, without waiting for the + // worker threads to exit. This is safe to call from within a listener + // callback (which runs on one of those threads; a blocking teardown + // there would deadlock). It marks the server DESTRUCTING (no new + // connections are accepted), closes the active connection, and + // 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 server is left in + // State::DESTRUCTING; its destructor completes the teardown (joins + // the threads and returns it to NOT_RUNNING), and so does a later + // "start()" on the same server, so it can be reused or destroyed. It + // is safe to call multiple times. virtual void close() noexcept = 0; // Sends data to the active connection. This blocks until every byte diff --git a/Common/Cpp/Sockets/ServerSocket_POSIX.cpp b/Common/Cpp/Sockets/ServerSocket_POSIX.cpp index 47736e751b..c17fb5d75a 100644 --- a/Common/Cpp/Sockets/ServerSocket_POSIX.cpp +++ b/Common/Cpp/Sockets/ServerSocket_POSIX.cpp @@ -30,6 +30,14 @@ ServerSocket_POSIX::ServerSocket_POSIX(ThreadPool& thread_pool) } void ServerSocket_POSIX::start(const std::string& address, uint16_t port){ + // Complete a requested-but-unfinished close() before starting a new + // listener: close() is non-blocking (it leaves the server DESTRUCTING + // with the threads still exiting), so re-listening on the same server + // joins them here. No-op if the server is already NOT_RUNNING. + // start() is always user-initiated, never a callback, so blocking is + // safe. + close_and_wait(); + uint64_t generation = 0; { std::lock_guard lg(m_lock); @@ -59,7 +67,7 @@ void ServerSocket_POSIX::start(const std::string& address, uint16_t port){ // caller's wait timed out while the pool was starved). It // notices its stale generation when it gets scheduled and // exits without listening, so it only has to outlive this - // call; close() joins the whole list. + // call; close_and_wait() joins the whole list. std::lock_guard lg(m_task_lock); m_accept_threads.push_back(std::move(task)); } @@ -102,6 +110,25 @@ void ServerSocket_POSIX::close_connection(){ } void ServerSocket_POSIX::close() noexcept{ + // Non-blocking teardown, safe to call from within a listener callback + // (which runs on one of the threads this would otherwise have to + // join). The accept loop and the connection loop observe + // State::DESTRUCTING on their next iteration (a few milliseconds) and + // exit on their own, closing their sockets; the server is left in + // State::DESTRUCTING. The blocking "close_and_wait()" - driven by the + // destructor and by a later "start()" on the same object - joins the + // threads and returns it to NOT_RUNNING. + { + std::lock_guard lg(m_lock); + if (m_state.load(std::memory_order_relaxed) != State::NOT_RUNNING){ + m_close_requested = true; + m_state.store(State::DESTRUCTING, std::memory_order_release); + } + } + m_cv.notify_all(); +} + +void ServerSocket_POSIX::close_and_wait() noexcept{ { std::unique_lock lg(m_lock); // Wait for an in-progress start() to publish the listener task @@ -121,7 +148,7 @@ void ServerSocket_POSIX::close() noexcept{ } if (!drain){ // Either nothing was running (m_closing == 0), or another - // close() is already draining the threads (m_closing > 1). + // close_and_wait() is already draining the threads (m_closing > 1). // In the second case, wait for it to finish. std::unique_lock lg(m_lock); m_cv.wait(lg, [&]{ diff --git a/Common/Cpp/Sockets/ServerSocket_POSIX.h b/Common/Cpp/Sockets/ServerSocket_POSIX.h index 00f0549cd6..8cd8ec7746 100644 --- a/Common/Cpp/Sockets/ServerSocket_POSIX.h +++ b/Common/Cpp/Sockets/ServerSocket_POSIX.h @@ -27,7 +27,7 @@ class ServerSocket_POSIX final : public AbstractServerSocket{ ServerSocket_POSIX(ThreadPool& thread_pool); virtual ~ServerSocket_POSIX(){ - close(); + close_and_wait(); } virtual void start(const std::string& address, uint16_t port) override; @@ -39,9 +39,14 @@ class ServerSocket_POSIX final : public AbstractServerSocket{ // within a listener callback. virtual void close_connection() override; - // Stops the server. It cancels any in-flight "send()" the same way - // "close_connection()" does, and blocks until the worker threads have - // exited, so it must not be called from within a listener callback. + // Requests immediate teardown of the server, without waiting for the + // worker threads to exit. It marks the server DESTRUCTING and closes + // the active connection; the accept and connection threads observe + // the new state on their next iteration and exit on their own, closing + // their sockets. It cancels any in-flight "send()" the same way + // "close_connection()" does. Safe to call from within a listener + // callback. The blocking "close_and_wait()" (the destructor, and the + // "start()" prelude) joins the threads and returns NOT_RUNNING. virtual void close() noexcept override; virtual size_t send( @@ -53,6 +58,12 @@ class ServerSocket_POSIX final : public AbstractServerSocket{ private: + // The blocking teardown: waits for an in-progress start() to publish + // its task, then joins the accept and connection threads and returns + // the server to NOT_RUNNING. Driven by the destructor and by start() + // (which re-listens on the same object after a close()). + void close_and_wait() noexcept; + void accept_thread_loop(std::string address, uint16_t port, uint64_t generation); void connection_thread_loop(); @@ -89,8 +100,8 @@ class ServerSocket_POSIX final : public AbstractServerSocket{ // One task per start() call. start() must never destroy the previous // task's AsyncTask here: its destructor blocks until the task has // finished, and a task from an earlier attempt may still be running - // (the caller's wait timed out while the pool was starved). close() - // joins the whole list. + // (the caller's wait timed out while the pool was starved). + // close_and_wait() joins the whole list. std::vector m_accept_threads; AsyncTask m_client_thread; }; diff --git a/Common/Cpp/Sockets/ServerSocket_Qt.cpp b/Common/Cpp/Sockets/ServerSocket_Qt.cpp index ff3ac18120..76d8312157 100644 --- a/Common/Cpp/Sockets/ServerSocket_Qt.cpp +++ b/Common/Cpp/Sockets/ServerSocket_Qt.cpp @@ -16,6 +16,14 @@ namespace PokemonAutomation{ void ServerSocket_Qt::start(const std::string& address, uint16_t port){ + // Complete a requested-but-unfinished close() before starting a new + // listener: close() is non-blocking (it leaves the server DESTRUCTING + // with the QTcpServer still alive), so re-listening on the same server + // destroys the old one here. No-op if the server is already + // NOT_RUNNING. start() is always user-initiated, never a callback, so + // blocking is safe. + close_and_wait(); + bool starting; { std::lock_guard lg(m_lock); @@ -25,7 +33,7 @@ void ServerSocket_Qt::start(const std::string& address, uint16_t port){ && !m_starting; // Only claim the "starting" flag when we actually start: a // rejected call (server already running or already started) must - // not leave "m_starting" true - "close()" waits on + // not leave "m_starting" true - "close_and_wait()" waits on // "!m_starting", so a stale flag would deadlock the destructor. // (This happens when a test's start-retry loop calls start() on // a server whose first listen failed: "m_server" is set even on @@ -66,7 +74,8 @@ void ServerSocket_Qt::start(const std::string& address, uint16_t port){ (m_state.load(std::memory_order_relaxed) == State::DESTRUCTING); m_server = server; if (destructing){ - // "close()" is already in progress: it will destroy the + // "close_and_wait()" is already in progress (from the + // destructor or a concurrent start()): it will destroy the // server we just created. Leave the state for it. }else{ listening = m_listen_error.empty(); @@ -83,9 +92,9 @@ void ServerSocket_Qt::start(const std::string& address, uint16_t port){ // exactly as it was before this start() (no "m_server", state // NOT_RUNNING), so the caller's start-retry loop can immediately // try another port. The other backends behave the same: a failed - // listen leaves no listener behind. If "close()" raced us, it has - // already claimed "m_server", and the pool's lookup makes the - // second destruction a no-op. + // listen leaves no listener behind. If "close_and_wait()" raced + // us, it has already claimed "m_server", and the pool's lookup + // makes the second destruction a no-op. GlobalThreadPools::qt_event_threadpool().remove_object(server); std::lock_guard lg(m_lock); m_server = nullptr; @@ -131,6 +140,41 @@ void ServerSocket_Qt::close_connection(){ } } void ServerSocket_Qt::close() noexcept{ + // Non-blocking teardown, safe to call from within a listener callback + // (which runs on the event thread; the blocking teardown there would + // deadlock). It marks the server DESTRUCTING (on_new_connection() + // stops accepting, and connection_lost() no longer reports), cancels + // the in-flight "send()" calls so their callers return promptly, and + // aborts the active connection on the event thread the same way + // "close_connection()" does - queued when called from another + // thread, so it runs as soon as the event loop next turns. It does + // not wait for any of that. The QTcpServer itself (the connection + // socket is its child) is destroyed by the blocking + // "close_and_wait()" - the destructor, and the "start()" prelude - + // which also returns the state to NOT_RUNNING. + { + std::lock_guard lg(m_lock); + if (m_server == nullptr && !m_starting){ + // Never started, or already closed. + return; + } + m_close_requested = true; + m_state.store(State::DESTRUCTING, std::memory_order_release); + } + + // Cancel the in-flight "send()" calls so their callers return + // promptly with the best-effort byte count. + cancel_inflight_sends(); + + // Abort the active connection, same event as "close_connection()". + // Deliberately no wait: close() must never block. If we are not on + // the event thread, the abort runs when the event loop next turns + // (or is discarded with the server, which is harmless: it only ever + // touches state under m_lock). + emit internal_close_connection(QSharedPointer::create()); +} + +void ServerSocket_Qt::close_and_wait() noexcept{ QTcpServer* server; { std::lock_guard lg(m_lock); @@ -289,8 +333,8 @@ void ServerSocket_Qt::on_new_connection(){ if (sock == nullptr){ break; } - // The connection socket lives with the server, so "close()" - // destroys it on the event thread. + // The connection socket lives with the server, so + // "close_and_wait()" destroys it on the event thread. sock->setParent(server); bool accepted; @@ -391,7 +435,12 @@ void ServerSocket_Qt::on_close_connection(QSharedPointer comple std::lock_guard lg(m_lock); if (m_socket != nullptr){ m_close_requested = true; - m_state.store(State::LISTENING, std::memory_order_release); + if (m_state.load(std::memory_order_relaxed) != State::DESTRUCTING){ + // A plain close_connection(): the server keeps running, + // so it goes back to LISTENING. A close() in progress + // already set DESTRUCTING: leave the state for it. + m_state.store(State::LISTENING, std::memory_order_release); + } sock = m_socket; m_socket = nullptr; }else{ @@ -430,7 +479,8 @@ void ServerSocket_Qt::on_send(QSharedPointer send_data){ const char* ptr = send_data->packet.data(); size_t remaining = send_data->packet.size(); // The deadline keeps a client that never reads from pinning this - // event thread forever (which would also block "close()"). + // event thread forever (which would also block + // "close_and_wait()"). while ( remaining > 0 && sock->state() == QAbstractSocket::ConnectedState diff --git a/Common/Cpp/Sockets/ServerSocket_Qt.h b/Common/Cpp/Sockets/ServerSocket_Qt.h index 0fab4731ee..7c8b66c8dd 100644 --- a/Common/Cpp/Sockets/ServerSocket_Qt.h +++ b/Common/Cpp/Sockets/ServerSocket_Qt.h @@ -39,8 +39,9 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{ std::string packet; // The event-thread write loop stops at this deadline, so a send // that can never complete (a client that never reads) cannot pin - // the server's event thread forever and block "close()". "max()" - // means no deadline (the infinite default). + // the server'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()"/"close_connection()" cancelling the send mid-write @@ -69,7 +70,7 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{ ServerSocket_Qt(ThreadPool&){} virtual ~ServerSocket_Qt(){ - close(); + close_and_wait(); } // Binds to "address" and listens on "port" on the Qt event thread. This @@ -82,19 +83,19 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{ // effort). It is safe to call from within a listener callback. virtual void close_connection() override; - // Stops the server, destroys the QTcpServer (and its connection - // socket), and returns the socket to State::NOT_RUNNING so "start()" - // can be called again. This blocks until the QTcpServer 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 the same way - // "close_connection()" does: their callers return promptly with the - // number of bytes actually written so far (best effort). - // - // This must not be called from within a listener callback, since the - // callbacks run on the QTcpServer's event thread and this destroys the - // server that is running them. + // Requests immediate teardown of the server, without waiting for the + // event thread. It marks the server DESTRUCTING (no new connections + // are accepted), cancels the in-flight "send()" calls so their + // callers return promptly with the bytes written so far (best + // effort), and aborts the active connection on the event thread the + // same way "close_connection()" does. It does not wait for any of + // that, so it is safe to call from within a listener callback (which + // runs on the event thread). No "on_disconnect" callback is fired. + // The QTcpServer itself (and the connection socket, its child) is + // destroyed by the blocking "close_and_wait()" - the destructor, and + // the "start()" prelude - which also returns the state to + // NOT_RUNNING. Calling "close()" when the server is not running is a + // no-op. It is safe to call multiple times. virtual void close() noexcept override; virtual size_t send( @@ -135,6 +136,14 @@ class ServerSocket_Qt final : public QObject, public AbstractServerSocket{ // runs (or is discarded with the server). void cancel_inflight_sends(); + // The blocking teardown: cancels the in-flight "send()" calls the + // same way close() does, waits for an in-progress start() to publish + // the server, destroys the QTcpServer (and its connection socket) on + // the event thread, and returns the state to NOT_RUNNING. Driven by + // the destructor and by start() (which re-listens on the same object + // after a close()). + void close_and_wait() noexcept; + std::unique_ptr make_server(const std::string& address, uint16_t port); private: diff --git a/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp b/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp index b269dc69a7..43d3635338 100644 --- a/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp +++ b/Common/Cpp/Sockets/ServerSocket_WinSocket.cpp @@ -34,6 +34,14 @@ void ServerSocket_WinSocket::ensure_wsa_started(){ } void ServerSocket_WinSocket::start(const std::string& address, uint16_t port){ + // Complete a requested-but-unfinished close() before starting a new + // listener: close() is non-blocking (it leaves the server DESTRUCTING + // with the threads still exiting), so re-listening on the same server + // joins them here. No-op if the server is already NOT_RUNNING. + // start() is always user-initiated, never a callback, so blocking is + // safe. + close_and_wait(); + // Winsock has to be started before any of its functions can be // used. Call it here, on the calling thread, so it is done before // any socket is created (a client created right after "start()" @@ -70,7 +78,7 @@ void ServerSocket_WinSocket::start(const std::string& address, uint16_t port){ // caller's wait timed out while the pool was starved). It // notices its stale generation when it gets scheduled and // exits without listening, so it only has to outlive this - // call; close() joins the whole list. + // call; close_and_wait() joins the whole list. std::lock_guard lg(m_task_lock); m_accept_threads.push_back(std::move(task)); } @@ -113,6 +121,25 @@ void ServerSocket_WinSocket::close_connection(){ } void ServerSocket_WinSocket::close() noexcept{ + // Non-blocking teardown, safe to call from within a listener callback + // (which runs on one of the threads this would otherwise have to + // join). The accept loop and the connection loop observe + // State::DESTRUCTING on their next iteration (a few milliseconds) and + // exit on their own, closing their sockets; the server is left in + // State::DESTRUCTING. The blocking "close_and_wait()" - driven by the + // destructor and by a later "start()" on the same object - joins the + // threads and returns it to NOT_RUNNING. + { + std::lock_guard lg(m_lock); + if (m_state.load(std::memory_order_relaxed) != State::NOT_RUNNING){ + m_close_requested = true; + m_state.store(State::DESTRUCTING, std::memory_order_release); + } + } + m_cv.notify_all(); +} + +void ServerSocket_WinSocket::close_and_wait() noexcept{ { std::unique_lock lg(m_lock); // Wait for an in-progress start() to publish the listener task @@ -132,7 +159,7 @@ void ServerSocket_WinSocket::close() noexcept{ } if (!drain){ // Either nothing was running (m_closing == 0), or another - // close() is already draining the threads (m_closing > 1). + // close_and_wait() is already draining the threads (m_closing > 1). // In the second case, wait for it to finish. std::unique_lock lg(m_lock); m_cv.wait(lg, [&]{ diff --git a/Common/Cpp/Sockets/ServerSocket_WinSocket.h b/Common/Cpp/Sockets/ServerSocket_WinSocket.h index e12074c2af..adbdc9e9df 100644 --- a/Common/Cpp/Sockets/ServerSocket_WinSocket.h +++ b/Common/Cpp/Sockets/ServerSocket_WinSocket.h @@ -30,7 +30,7 @@ class ServerSocket_WinSocket final : public AbstractServerSocket{ : m_thread_pool(thread_pool) {} virtual ~ServerSocket_WinSocket(){ - close(); + close_and_wait(); } virtual void start(const std::string& address, uint16_t port) override; @@ -42,9 +42,14 @@ class ServerSocket_WinSocket final : public AbstractServerSocket{ // within a listener callback. virtual void close_connection() override; - // Stops the server. It cancels any in-flight "send()" the same way - // "close_connection()" does, and blocks until the worker threads have - // exited, so it must not be called from within a listener callback. + // Requests immediate teardown of the server, without waiting for the + // worker threads to exit. It marks the server DESTRUCTING and closes + // the active connection; the accept and connection threads observe + // the new state on their next iteration and exit on their own, closing + // their sockets. It cancels any in-flight "send()" the same way + // "close_connection()" does. Safe to call from within a listener + // callback. The blocking "close_and_wait()" (the destructor, and the + // "start()" prelude) joins the threads and returns NOT_RUNNING. virtual void close() noexcept override; virtual size_t send( @@ -62,6 +67,12 @@ class ServerSocket_WinSocket final : public AbstractServerSocket{ // other parts of the process (Qt) may be using Winsock as well. static void ensure_wsa_started(); + // The blocking teardown: waits for an in-progress start() to publish + // its task, then joins the accept and connection threads and returns + // the server to NOT_RUNNING. Driven by the destructor and by start() + // (which re-listens on the same object after a close()). + void close_and_wait() noexcept; + void accept_thread_loop(std::string address, uint16_t port, uint64_t generation); void connection_thread_loop(); @@ -98,8 +109,8 @@ class ServerSocket_WinSocket final : public AbstractServerSocket{ // One task per start() call. start() must never destroy the previous // task's AsyncTask here: its destructor blocks until the task has // finished, and a task from an earlier attempt may still be running - // (the caller's wait timed out while the pool was starved). close() - // joins the whole list. + // (the caller's wait timed out while the pool was starved). + // close_and_wait() joins the whole list. std::vector m_accept_threads; AsyncTask m_client_thread; }; diff --git a/Common/Cpp/Sockets/Sockets_Tests.cpp b/Common/Cpp/Sockets/Sockets_Tests.cpp index ba1dc66ec8..061f1e379b 100644 --- a/Common/Cpp/Sockets/Sockets_Tests.cpp +++ b/Common/Cpp/Sockets/Sockets_Tests.cpp @@ -161,6 +161,23 @@ struct ServerListener : AbstractServerSocket::Listener{ }; +// A server listener that tears the server down from within its own +// on_receive_data() callback via close() - the server-side counterpart of +// ClosingClientListener. close() must return promptly here: on POSIX and +// Windows the callback runs on the connection thread that the blocking +// close_and_wait() would join, and on Qt it runs on the event thread that +// close_and_wait() would block. +struct ClosingServerListener : ServerListener{ + std::atomic closed_in_callback{0}; + + virtual void on_receive_data(const void*, size_t) override{ + server->close(); + closed_in_callback++; + waiter.notify(); + } +}; + + // The parallel test runner can starve threads for long stretches on // loaded CI machines, where wall-clock assertions then fail tests that // would pass on a healthy machine. A small fixed workload (a 32 MB @@ -2398,6 +2415,105 @@ class Test_Sockets_CloseFromReceiveCallback : public TestSockets +class Test_Sockets_ServerCloseFromReceiveCallback : 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())." + ); + } + ClosingServerListener server_listener; + ServerSocketType server(this->m_thread_pool); + server.add_listener(server_listener); + server_listener.server = &server; + + 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; + { + ClientListener client_listener; + ClientSocketType client(this->m_thread_pool); + 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 (client.send("ping", 4) != 4){ + error = "The client did not send the trigger packet"; + }else if (!server_listener.waiter.wait_for( + SOCKET_TEST_WAIT, + [&server_listener]{ return server_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 (server.state() == AbstractServerSocket::State::CONNECTED + || server.state() == AbstractServerSocket::State::LISTENING + ){ + error = "After close() the server is still connected or listening"; + }else{ + // close() (called in the callback) only requested the + // teardown. The blocking close_and_wait() runs when + // start() is called again below and when "server" is + // destroyed - neither may hang. + ok = true; + } + } + + // Re-listen on the same server: start() must complete the + // close() requested in the callback (its close_and_wait() + // prelude) and bind again. + if (ok && !this->start_server(server, server_listener, port)){ + if (timing_unreliable()){ + return UnitTestResult( + UnitTestResult::SKIPPED, + "The server did not re-start listening on a starved machine." + ); + } + error = "The server did not re-start listening after close()"; + } + + 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) + ") - "; @@ -2438,6 +2554,9 @@ void add_socket_tests(UnitTestDatabase& database, ThreadPool& thread_pool, const database.add>( thread_pool, prefix + "close() from the receive callback" ); + database.add>( + thread_pool, prefix + "server close() from the receive callback" + ); }