Skip to content

Sockets: make ServerSocket::close() private; the destructor performs the teardown - #1859

Merged
Mysticial merged 1 commit into
mainfrom
server-close-nonblocking
Nov 7, 2026
Merged

Mysticial merged 1 commit into
mainfrom
server-close-nonblocking

Conversation

@pa-devbot

@pa-devbot pa-devbot commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

Making the server-side close() API match what users actually need (per review of the first revision): only close_connection() stays public. A listener that wants to disconnect the client calls close_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-blocking close() + private close_and_wait() split; the second commit (01ba173d9) applies this review and collapses it to a private destructor-only close(). Squashing is welcome.

Final API

  • start(), close_connection(), send() — public, as before.
  • close() — private in each backend (non-virtual), body unchanged from main for POSIX/WinSocket (the blocking teardown); the QTcpServer-destroying body for Qt. Only the destructor calls it.
  • No close_and_wait(), no start() prelude: a server can never be left DESTRUCTING while the object is alive, since the only teardown is destruction.

Details

  • AbstractServerSocket no longer declares close(); close_connection()'s doc now contrasts with "the full teardown the destructor performs" instead of with a public close().
  • Kept the Qt on_close_connection() DESTRUCTING-preservation: a queued connection-close can still race the destructor's close() (the event thread runs it before the queued destruction), and without it the state could flicker back to LISTENING mid-teardown and accept a connection.
  • Sockets_Tests.cpp: all server.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.
  • Carried over from the first commit: the AbstractClientSocket.h doc fix (a close()d client socket can be connect()ed again — connect() runs close_and_wait() first).

Verification

Full local run on this revision: 0 failures — all 27 socket tests pass (Qt 15 + POSIX 12). POSIX/WinSocket .cpp files are byte-identical to main; the net diff is 7 files, +35/−83.

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.
@Mysticial
Mysticial merged commit c4b66b8 into main Nov 7, 2026
8 checks passed
@Mysticial
Mysticial deleted the server-close-nonblocking branch November 7, 2026 19:44
@pa-devbot pa-devbot changed the title Sockets: make ServerSocket::close() non-blocking; add close_and_wait() Sockets: make ServerSocket::close() private; the destructor performs the teardown Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants