Repository navigation
Sockets: make ServerSocket::close() private; the destructor performs the teardown - #1859
Merged
Merged
Conversation
Mirror the ClientSocket redesign on the server side: - close() now only requests the teardown: it marks the server DESTRUCTING, cancels the in-flight send() calls, and closes the active connection, without waiting for the worker threads to exit. It is safe to call from within a listener callback (the blocking teardown there would deadlock). The server is left in State::DESTRUCTING. - close_and_wait() (private) performs the old blocking teardown: it joins the accept and connection threads (POSIX/WinSocket) or destroys the QTcpServer on the event thread (Qt), and returns the state to NOT_RUNNING. The destructor and the start() prelude call it, so a server can be re-listened on the same object after close(). - close_connection() is unchanged. - Qt's on_close_connection() no longer clobbers DESTRUCTING back to LISTENING when a close() is in progress. - Regression test: the server can now be torn down from within its own on_receive_data() callback (the server-side counterpart of Test_Sockets_CloseFromReceiveCallback), including a re-listen on the same object afterwards. - Docs: document the new close() contract; fix the stale client doc that claimed a closed socket cannot be connect()ed again.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Making the server-side
close()API match what users actually need (per review of the first revision): onlyclose_connection()stays public. A listener that wants to disconnect the client callsclose_connection(); completely shutting down the server only needs to happen in the destructor.Note on history: the first commit (
d91b49c07) tried the client-style public non-blockingclose()+ privateclose_and_wait()split; the second commit (01ba173d9) applies this review and collapses it to a private destructor-onlyclose(). Squashing is welcome.Final API
start(),close_connection(),send()— public, as before.close()— private in each backend (non-virtual), body unchanged frommainfor POSIX/WinSocket (the blocking teardown); theQTcpServer-destroying body for Qt. Only the destructor calls it.close_and_wait(), nostart()prelude: a server can never be leftDESTRUCTINGwhile the object is alive, since the only teardown is destruction.Details
AbstractServerSocketno longer declaresclose();close_connection()'s doc now contrasts with "the full teardown the destructor performs" instead of with a publicclose().on_close_connection()DESTRUCTING-preservation: a queued connection-close can still race the destructor'sclose()(the event thread runs it before the queued destruction), and without it the state could flicker back toLISTENINGmid-teardown and accept a connection.Sockets_Tests.cpp: allserver.close()/second_server.close()cleanup calls removed (the destructor now tears down at scope exit, same teardown, same timing); the server-callback regression test from the first commit is dropped.AbstractClientSocket.hdoc fix (aclose()d client socket can beconnect()ed again —connect()runsclose_and_wait()first).Verification
Full local run on this revision: 0 failures — all 27 socket tests pass (Qt 15 + POSIX 12). POSIX/WinSocket
.cppfiles are byte-identical tomain; the net diff is 7 files, +35/−83.