Skip to content

Fix HttpServer double-close of accepted socket fd - #748

Open
raphaelroshan wants to merge 1 commit into
quickfix:masterfrom
raphaelroshan:fix/http-connection-double-close
Open

Fix HttpServer double-close of accepted socket fd#748
raphaelroshan wants to merge 1 commit into
quickfix:masterfrom
raphaelroshan:fix/http-connection-double-close

Conversation

@raphaelroshan

Copy link
Copy Markdown

What

HttpConnection::disconnect() no longer calls socket_close() directly.

Why

HttpServer::onConnect() closes each accepted socket twice per request: once in
HttpConnection::disconnect() and again via SocketMonitor::drop(s) (the accepted
fd is monitor-tracked through SocketServer::accept()addConnect()). If the fd
number is recycled between the two closes, the second close silently tears down an
unrelated socket — including one owned by another thread.

This mirrors SocketConnection::disconnect(), which already delegates the close to
SocketMonitor::drop() rather than closing the descriptor itself. HttpConnection
is only ever instantiated in onConnect(), which always calls drop(s) after the
read loop, so the socket is still closed exactly once — no leak.

Test

Added HttpConnectionTestCase.cpp, which reproduces the double close via fd reuse:
after the read loop, a freshly opened socket reclaims the just-freed descriptor and
the subsequent drop() must not close it. Fails on master, passes with this fix.

Fixes #747

HttpConnection::disconnect() closed the accepted socket directly, and
HttpServer::onConnect() then closed it again via SocketMonitor::drop()
(the fd is monitor-tracked through SocketServer::accept()). If the fd
number was recycled between the two closes, the second close silently
tore down an unrelated socket.

Delegate the close to the monitor, matching SocketConnection::disconnect().
onConnect() always drops after the read loop, so the socket is still closed
exactly once. Adds a Catch2 test that reproduces the double close via fd reuse.

Fixes quickfix#747
@raphaelroshan
raphaelroshan marked this pull request as ready for review July 11, 2026 14:51
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.

[Bug] HttpServer double-closes accepted socket fd per request, causing unintended close of another thread's fd

1 participant