Skip to content

Sockets: release the Qt server's QTcpSocket objects when they are no longer used - #1868

Merged
Mysticial merged 1 commit into
mainfrom
server-socket-leak
Nov 8, 2026
Merged

Mysticial merged 1 commit into
mainfrom
server-socket-leak

Conversation

@pa-devbot

Copy link
Copy Markdown
Collaborator

Summary

Closes audit finding #4 from the TCP security audit: the Qt server retained every disconnected or rejected socket.

Every QTcpSocket from nextPendingConnection() is parented to the long-lived QTcpServer, but:

  • rejected sockets were abort()ed and never deleted,
  • accepted sockets were never deleted after disconnecting,
  • close_connection()'s aborted socket was never deleted either.

So a peer could grow the server's memory without bound — one QTcpSocket (plus its internal socket engine and buffers) per connect attempt, released only when the whole server was destroyed. In the audit's demonstration, 1000 connect attempts left 1000 socket objects alive.

Fix

Release each socket on the event thread as soon as it stops being used:

path change
rejected connection in on_new_connection() sock->deleteLater() right after sock->abort()
accepted connection in connection_lost() sock->deleteLater() after the state is detached and on_disconnect() has run
close_connection() in on_close_connection() sock->deleteLater() after sock->abort()

Safety notes (all verified in a standalone Qt harness under ASan):

  • deleteLater() is idempotent, so the paths that can schedule it twice for one socket are safe: on_close_connection()'s abort() may still emit disconnected, whose handler returns early because m_socket is already null; and connection_lost() is first-callback-wins.
  • A deferred delete for a socket whose parent is destroyed first (the close_and_wait() race) simply never fires — the child goes with the parent; ASan confirms no crash and no double delete.
  • The close_and_wait() path is unchanged: the server's own destruction removes its children.

Testing

New regression test Test_Sockets_Qt_ConnectionChurn ("Sockets (Qt) - connection churn releases the QTcpSocket objects") drives all three paths through the real ServerSocket_Qt:

  • Phase 1: 20 connect/echo/disconnect cycles (accepted-socket release),
  • Phase 2: 10 rejected connections while a client occupies the server (rejected-socket release),
  • Phase 3: close_connection() then reconnect and echo again (active-socket release + the server keeps working).

Full suite: 20,448 passed / 0 failed (the 5 skips are the usual audio-file environment skips).

The ASan harness also confirmed the leak itself: with the old pattern, 6 connect attempts left 6 QTcpSocket children; with the fix, children() returns to baseline.

…longer used

A remote peer could accumulate one QTcpSocket per connect attempt: the
server parents every accepted socket to the long-lived QTcpServer, but
rejected sockets were aborted and never deleted, and accepted sockets
were never deleted after disconnecting. The objects (socket object,
internal socket engine and its buffers) only went away when the whole
server was destroyed, so a peer that connects - and is rejected because
a client already occupies the server - as fast as it can grows the
server's memory without bound.

Release each socket as soon as it stops being used:

- Rejected connections: deleteLater() right after abort() in
  on_new_connection().
- Accepted connections: deleteLater() at the end of connection_lost(),
  after the state has been detached and on_disconnect() has run.
  connection_lost() is first-callback-wins, so each socket is released
  by exactly one path.
- close_connection(): deleteLater() after abort() in
  on_close_connection(); the subsequent disconnected signal returns
  early in connection_lost() because m_socket is already null.

All calls run on the server's event thread, so the deletion happens on
that thread at the next event-loop turn; deleteLater() is idempotent,
so the paths that can schedule it for the same socket (the abort in
on_close_connection() also emits disconnected, whose handler returns
early) are safe. A socket scheduled for deletion but destroyed with
its parent first (close_and_wait()) is simply deleted with it. The
close_and_wait() path is unchanged: the server's own destruction
removes its children.

Verified with a standalone Qt harness under ASan: the old pattern
leaves one QTcpSocket child per connect attempt; with the fix,
children() returns to baseline after every churn. The new regression
test Test_Sockets_Qt_ConnectionChurn exercises all three paths through
the real ServerSocket_Qt: 20 connect/echo/disconnect cycles, 10
rejections while a client occupies the server, and
close_connection() followed by a reconnect.
@Mysticial
Mysticial merged commit 122f3ef into main Nov 8, 2026
8 of 10 checks passed
@Mysticial
Mysticial deleted the server-socket-leak branch November 8, 2026 21:39
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