Repository navigation
Sockets: release the Qt server's QTcpSocket objects when they are no longer used - #1868
Merged
Merged
Conversation
…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.
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.
Summary
Closes audit finding #4 from the TCP security audit: the Qt server retained every disconnected or rejected socket.
Every
QTcpSocketfromnextPendingConnection()is parented to the long-livedQTcpServer, but:abort()ed and never deleted,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:
on_new_connection()sock->deleteLater()right aftersock->abort()connection_lost()sock->deleteLater()after the state is detached andon_disconnect()has runclose_connection()inon_close_connection()sock->deleteLater()aftersock->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()'sabort()may still emitdisconnected, whose handler returns early becausem_socketis already null; andconnection_lost()is first-callback-wins.close_and_wait()race) simply never fires — the child goes with the parent; ASan confirms no crash and no double delete.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 realServerSocket_Qt: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
QTcpSocketchildren; with the fix,children()returns to baseline.