Repository navigation
Do not let processes started by the server keep web terminal and SSH sessions open - #126995
Conversation
…ipes of embedded client sessions `openPty` opened the pseudo-terminal of a web terminal / SSH session without `O_CLOEXEC`, and `Poco::Pipe` (the stdin/stdout/stderr of an SSH `exec` session without a terminal) used plain `pipe()`. `ShellCommand` starts `executable` / `executable_pool` sources with `vfork` + `execv` and closes no descriptors, so a process started while such a session existed kept a copy of its descriptors. Web terminal / SSH with a PTY: closing the session closes the server's master, but the master hangs up the slave only when its last reference goes away. The child still holds one, so nobody reads the terminal any more, the embedded client's next write blocks forever, `~ClientEmbeddedRunner` waits for it forever and the query is never cancelled. This is the hang of `05386_webterminal_close_during_query` on master (5ba211c): `05394` started a pool process inside the lifetime of 05386's terminal. SSH `exec` without a PTY: the child holds the write end of the client's stdin, so the client never sees the end of its input and an `INSERT` fed through `ssh` never finishes. Open both PTY ends with `O_CLOEXEC` (atomic on Linux, FreeBSD and macOS) and create Poco pipes with `pipe2(O_CLOEXEC)`; macOS has no `pipe2`, so set `FD_CLOEXEC` right after `pipe` there, as `LazyPipeFDs::open` does. `dup2` in `ShellCommand` and Poco `Process` clears the flag on the child's stdio copies, so the pipes a child needs are unaffected. New tests: `05427_webterminal_close_with_started_process` and `05428_ssh_insert_with_started_process` start a pool process inside the session, then end the session (close the WebSocket / send EOF) and require the query to finish. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nning 05427 and 05428 both reloaded `executable_pool_simple_implicit_key`. A reload ends the processes of the previous pool, so when the two tests run together (Bugfix validation runs the changed tests in parallel) each one ended the other's witness process and both passed on master. 05428 now uses `executable_pool_complex_implicit_key`, which no other parallel test reloads. 05428 also prints the number of running INSERTs before it starts the process, so a session that never started fails the test instead of passing it vacuously. 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; 2 findings, then 0 on the re-review of the fix) plus my own review (2 nits).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261020-170200 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261020-162400 |
|
cc @alexey-milovidov @george-larionov, could you review this and add the |
|
Workflow [PR], commit [15682ce] Summary: ✅
AI ReviewSummaryReviewed #126995 at Final VerdictStatus: ✅ Approve |
The CI profile (`tests/config/users.d/limits.yaml`) sets `max_rows_to_read` to 20 million for every stateless job, and the web terminal session uses that profile. `numbers(100000000)` was rejected with `TOO_MANY_ROWS` before printing anything, so the test waited for its output until the 60 second timeout and failed in Fast test. Set `max_rows_to_read = 0` for that query. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`clickhouse-test --trace` (as in CI) sends the bash xtrace of the test to fd 3 (`BASH_XTRACEFD=3` in `shell_config.sh`). The test reused fd 3 for the FIFO that feeds the INSERT, so the trace lines went into the INSERT as data, which failed to parse (the table stayed empty), and after closing fd 3 the trace went to stderr. Use a descriptor allocated by bash instead. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units8 translation units recompiled, 5 s compile time in total, 8 of them have a recent master baseline. |
CI finish ledger - 6dda2aaEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261021-110006 |
|
@groeneai 🕵️ Reviewed the change — the core fix is correct. A few points on the non-Linux paths and the tests: 1.
|
macOS has no pipe2, so a process started by another thread between pipe() and fcntl() still inherits the pipe. Say so in the fallback. 05428 checked the SSH port with nc, so a runner without nc skipped the test with a misleading reason. Probe the port with bash's /dev/tcp. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks for the review. Pushed 15682ce.
Validated on a rebuild of the new head, with |
|
@groeneai I am merging this, but can you confirm if there are other places missing O_CLOEXEC flag. Also, for the cases added in this PR, could other callers be negatively impacted by it? |
|
Yes, there are more. I measured them on current master (1df150b): I started a process through
Found by grep, not reached by that workload: the Only the Raft socket has a user-visible effect that I could reproduce, and the sites are spread over contrib (NuRaft/asio, Poco, libc++). One fix that covers all of them is to close every descriptor except 0-2 and the configured extra pipes in the Other callers are not affected. |
Related: #125753
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a web terminal or SSH session that never ended, with its query still running, after the server had started an
executableorexecutable_poolprocess during that session. The process inherited the session's pseudo-terminal or pipes, so closing the web terminal did not stop its query, and anINSERTfed throughsshnever saw the end of its input.Description
On 26.8.28.14, closing a web terminal while its query prints output leaves the query, the embedded client and the HTTP handler thread stuck until a process that the server started during that session exits; for an
executable_poolprocess that means until the dictionary is reloaded or the server restarts. This is the05386_webterminal_close_during_queryfailure on master (2026-10-20), and the05453_webterminal_close_at_binary_format_questionfailure withSome queries hung(2026-10-30):05394started a pool process inside the session.openPtyopened the pseudo-terminal withoutO_CLOEXEC, andPoco::Pipe(stdin/stdout/stderr of an SSHexecsession without a terminal) used plainpipe().ShellCommandstarts processes withvfork+execv, so the child kept copies. Closing the master then does not hang up the slave, the embedded client blocks writing to a terminal nobody reads, and~ClientEmbeddedRunnerwaits for it. Over SSH without a terminal, the child holds the write end of the client's stdin, so the client never sees the end of its input.The change opens both ends of the pseudo-terminal with
O_CLOEXECand creates Poco pipes withpipe2(O_CLOEXEC)(pipe+FD_CLOEXECon macOS).dup2clears the flag on a child's stdio, so the pipes a child needs are unaffected.The new tests start a pool process inside the session, then close the WebSocket (
05427) or end thesshinput (05428). On master both fail 5/5, alone and when run together (the query still runs after 60 s; the pool process holds/dev/ptmxand/dev/pts/N, or the session's stdin pipe),05427also fails on 26.8.28.14, and both pass in about 2 s with the fix. 50 randomized runs of each pass, sequentially and with-j 16, as do05386,05394and the other web terminal, SSH andexecutabletests.Workflow [PR]
Sync PR [sync-upstream/pr/126995]
Version info
26.11.1.1115-master(included in26.11and later)