Repository navigation
Do not let processes started by the server keep accepted client connections open - #125753
Conversation
…ctions open `SocketImpl::acceptConnection` used plain `accept`, so accepted sockets were inheritable, unlike sockets created by `socket()` (SOCK_CLOEXEC since ClickHouse#21695). `ShellCommand` starts `executable` / `executable_pool` sources with `vfork` + `execv` and does not close descriptors, so the child kept every client connection open at that moment. When the server then closed such a connection (`idle_connection_timeout`, HTTP keep-alive timeout) no FIN was sent, and the client sent its next query into a socket nobody read and hung. Accept with `accept4(SOCK_CLOEXEC)`; where `SOCK_CLOEXEC` does not exist (macOS), set `FD_CLOEXEC` right after `accept`, closing the descriptor and throwing if that fails. All Poco listeners, TLS included, go through this function. The SSH endpoint passes libssh a `dup` of the accepted socket (the original is closed in `SSHPtyHandler::run`), and `dup` never copies the flag, so use `F_DUPFD_CLOEXEC` there. This is the `04647_use_query_parameter_survives_reconnect` hang seen on ClickHouse#110015, where `01747`'s pool processes started while its session was open. `04647` additionally runs its setup over a separate connection: its 2 s idle timeout once closed the session between `CREATE` and `INSERT`. New test `05394_executable_pool_does_not_keep_closed_connection_open`: the dictionary reload makes the pool process start while the session is open, then the server closes the idle connection and the client must reconnect. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding) plus my own cold review (3 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261014-091900 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261014-082600 |
|
Workflow [PR], commit [0c4756c] Summary: ✅
AI ReviewSummaryThe PR prevents subprocesses from keeping closed client connections alive by making accepted sockets and SSH duplicates close-on-exec. No actionable findings at Missing context / blind spots
Final VerdictStatus: ✅ Approve. |
|
cc @CheSema @george-larionov, could you review this? Poco |
…nnect` The test relies on the server closing its idle connection. When it runs next to a test whose server-started processes inherit the accepted socket (`01747_executable_pool_dictionary_implicit_key`, now in the parallel run), the connection stays open, the client never reconnects and the `SELECT` waits until the 60 s timeout. Run it alone until accepted sockets are close-on-exec. Related: #125753 PR: #110015
alexey-milovidov
left a comment
There was a problem hiding this comment.
Wow! Amazing finding, thank you!
Related: #110015
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a client hang after the server closed an idle connection. A process started by the server, for example an
executable_pooldictionary source, inherited the client connections that were open at that moment and kept them open, so the client did not see the close and its next query or HTTP keep-alive request never got an answer.Description
On 26.8.18.2, after a query starts an
executable_poolprocess, a client whose connection was open at that moment hangs on its next query once the server closes the connection (idle_connection_timeout, or HTTPkeep_alive_timeout; the HTTP case also hangs on 26.3.42.3). This is the04647_use_query_parameter_survives_reconnectfailure on #110015 (2026-10-14, 2026-09-03):01747's pool processes started while its 2 s session was open.SocketImpl::acceptConnectionused plainaccept, so accepted sockets were not close-on-exec, unlike sockets fromsocket()(#21695).ShellCommandstarts the process withvfork+execv, so the child kept the connection, and closing it on the server sent no FIN.The change accepts with
accept4(SOCK_CLOEXEC), with anfcntlfallback whereSOCK_CLOEXECdoes not exist (macOS). This covers every Poco listener, TLS included. The SSH endpoint hands libssh adupof the accepted socket, which drops the flag, so it now usesF_DUPFD_CLOEXEC. NuRaft's own acceptor for the Raft port is not changed.04647also runs its setup over a separate connection now, because its 2 s idle timeout once closed the session betweenCREATEandINSERT(2026-09-10).The new test reloads the dictionary, so its process starts while the session is open, and then waits for the reconnect. It fails 6/6 on master (60 s timeout) and on 26.8.18.2, and passes in 4 s with the fix. 50 randomized runs of it and of
04647pass, sequentially and with-j 10.Workflow [PR]
Sync PR [sync-upstream/pr/125753]
Version info
26.11.1.200-master(included in26.11and later)