Skip to content

Do not let processes started by the server keep accepted client connections open - #125753

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-accepted-sockets-cloexec
Oct 19, 2026
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-accepted-sockets-cloexec

Conversation

@groeneai

@groeneai groeneai commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #110015

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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_pool dictionary 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_pool process, a client whose connection was open at that moment hangs on its next query once the server closes the connection (idle_connection_timeout, or HTTP keep_alive_timeout; the HTTP case also hangs on 26.3.42.3). This is the 04647_use_query_parameter_survives_reconnect failure on #110015 (2026-10-14, 2026-09-03): 01747's pool processes started while its 2 s session was open.

SocketImpl::acceptConnection used plain accept, so accepted sockets were not close-on-exec, unlike sockets from socket() (#21695). ShellCommand starts the process with vfork + execv, so the child kept the connection, and closing it on the server sent no FIN.

The change accepts with accept4(SOCK_CLOEXEC), with an fcntl fallback where SOCK_CLOEXEC does not exist (macOS). This covers every Poco listener, TLS included. The SSH endpoint hands libssh a dup of the accepted socket, which drops the flag, so it now uses F_DUPFD_CLOEXEC. NuRaft's own acceptor for the Raft port is not changed.

04647 also runs its setup over a separate connection now, because its 2 s idle timeout once closed the session between CREATE and INSERT (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 04647 pass, sequentially and with -j 10.


Workflow [PR]
Sync PR [sync-upstream/pr/125753]

Version info

  • Merged into: 26.11.1.200-master (included in 26.11 and later)

…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]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
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).

# Sev Finding Verdict Evidence / action
1 ⚠️ The macOS fallback (accept, then fcntl(F_SETFD)) is not atomic, so a process started between the two calls can still inherit the connection (base/poco/Net/src/SocketImpl.cpp) DISAGREE macOS has no accept4/SOCK_CLOEXEC, so nothing in acceptConnection can be atomic there. The tree accepts the same window on macOS for every pipe, including the ones ShellCommand gives its children (src/Common/PipeFDs.cpp, pipe + fcntl under OS_DARWIN). Before the change every connection open at spawn time was inherited; now only one accepted inside that window, and Linux/FreeBSD use the atomic path.
2 💡 The outbound PostgreSQLSource dup is not mentioned in the description AGREE, not changed Outbound connection, outside what this PR changes.
3 💡 04647 setup via exec fails the test on any stderr output AGREE, not changed Same contract as every .sh test; intended to fail loudly.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-9:20261014-091900

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The new 05394_executable_pool_does_not_keep_closed_connection_open.expect fails 6 of 6 runs on a master debug binary (expect timeout at ~64 s, no reconnect) and on the released 26.8.18.2 server.
b Root cause explained? SocketImpl::acceptConnection used plain accept, so accepted connections were inheritable. ShellCommand (vfork + execv) starts pool processes without closing descriptors, the child keeps every open client connection, and the server's close sends no FIN. The client then sends its next query into a socket nobody reads.
c Fix matches root cause? Yes. Accepted sockets are created close-on-exec (accept4(SOCK_CLOEXEC), fcntl fallback that closes the fd and throws on failure), and the SSH endpoint's dup keeps the flag (F_DUPFD_CLOEXEC). No timeout or tag changes.
d Test intent preserved / new tests added? New .expect test: SYSTEM RELOAD DICTIONARY makes the process start while the session is open, and expect "Connected to" requires a real reconnect. Without the reload it passes on master too, so the reload is required. 04647 keeps all its assertions; only its setup moved to a separate connection.
e Both directions demonstrated? Unfixed: RC=1 at ~64 s (6/6), ss shows the server socket also owned by sh. Fixed: RC=0 in ~4 s, the socket is owned by clickhouse only and the pool process has no socket: descriptors. HTTP keep-alive and SSH arms behave the same way.
f Fix is general across code paths? One accept site serves every Poco listener (native, HTTP, interserver, MySQL, PostgreSQL, Keeper client, Prometheus, TLS through SecureSocketImpl, ExchangeServer). The only copy of an accepted socket (SSH dup) is fixed. A SocketImpl-only build still leaked the SSH connection. gRPC sets the flag itself. NuRaft's asio acceptor (Raft port) is not changed.
g Fix generalizes across inputs (params/datatypes/wrappers)? Not a type or format change. Protocols covered: native TCP, HTTP keep-alive, SSH. The darwin fallback branch was built forced on Linux: the accepted fd has O_CLOEXEC and the new test passes.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, protocol or format change. Processes started by the server no longer see client connections, and nothing used them.
i Invariants and contracts preserved? acceptConnection still returns a StreamSocketImpl or throws through error(). The fallback never returns an inheritable fd. F_DUPFD_CLOEXEC has the semantics of dup plus the flag, with the same -1 error path. 50 randomized runs of the new test and 04647 pass, sequentially and with -j 10.

Session id: cron:clickhouse-impl-slot-4:20261014-082600

@clickhouse-gh

clickhouse-gh Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0c4756c]

Summary: ✅


AI Review

Summary

The PR prevents subprocesses from keeping closed client connections alive by making accepted sockets and SSH duplicates close-on-exec. No actionable findings at 0c4756caf565fc1592c0afcf9188d48c57dd2fca. The new regression test and the existing reconnect test pass in the Fast test report.

Missing context / blind spots
  • ⚠️ No local build or runtime tests were run. Separate CI bugfix-validation jobs were dropped; unfixed-versus-fixed reproduction evidence comes from the author's recorded validation.
Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 14, 2026
@groeneai

groeneai commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator Author

cc @CheSema @george-larionov, could you review this? Poco SocketImpl::acceptConnection used plain accept, so processes the server starts (for example executable_pool children via vfork + execv) inherited open client connections and the client hung after an idle close; accepted sockets are now close-on-exec (accept4(SOCK_CLOEXEC), fcntl fallback on macOS), and the SSH endpoint keeps the flag on its dup. CI has not started because the PR lacks the can be tested label, which I cannot add; could one of you add it?

@clickhouse-gh clickhouse-gh Bot added the comp-protocols Server wire protocols + request/response handling (native TCP, HTTP, MySQL/PostgreSQL wire protoc... label Oct 14, 2026
fm4v added a commit that referenced this pull request Oct 14, 2026
…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 alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 15, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow! Amazing finding, thank you!

@alexey-milovidov alexey-milovidov self-assigned this Oct 19, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 19, 2026
Merged via the queue into ClickHouse:master with commit 0faaf23 Oct 19, 2026
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-protocols Server wire protocols + request/response handling (native TCP, HTTP, MySQL/PostgreSQL wire protoc... pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants