Skip to content

Terminate query quietly on EPIPE signal - #126638

Merged
alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
davidrz15:epipe_fix
Oct 22, 2026
Merged

alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
davidrz15:epipe_fix

Conversation

@davidrz15

@davidrz15 davidrz15 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

When the reader of the output of clickhouse-client or clickhouse-local goes away, as in clickhouse-local -q '...' | head -1, the client printed a Broken pipe exception (CANNOT_WRITE_TO_FILE_DESCRIPTOR) and exited with code 75. Now it stops quietly, like cat and other tools do: no error message, no further queries (even with --ignore-error), exit code 0. The query is still cancelled on the server.

Only the client's own stdout is handled this way: its output buffer remembers that a write failed with EPIPE. WriteBufferFromFileDescriptor and the signal disposition are not changed, because a broken pipe anywhere else (e.g. a pipe to a child process of an executable UDF) must stay an error, and SIG_IGN would be inherited by child processes such as the pager.

Closes: #126443

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

clickhouse-client and clickhouse-local stop quietly with exit code 0, without a Broken pipe exception, when the reader of their output goes away, e.g. clickhouse-local -q '...' | head -1.

🤖 Generated with Claude Code


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

Version info

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

WriteBufferFromFileDescriptor throws if any non-EINTR signal is
received from the write function, which differs  from standard
UNIX behavior. Ignore the signal at the application level and
cancel the query on EPIPE received by write().

@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.

This is interesting because ignoring the pipe signal might interfere with other cases like sockets.

@alexey-milovidov alexey-milovidov self-assigned this Oct 19, 2026
@clickhouse-gh clickhouse-gh Bot added external Author is not a member of the ClickHouse organization comp-cli clickhouse-client (interactive CLI) and client library. labels Oct 19, 2026
@davidrz15

davidrz15 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Fair point, I took a deeper look to see if there are write paths outside WriteBufferFromFileDescriptor that are writing to sockets and don't handle write failures properly. WriteBufferFromPocoSocket looks like the primary socket interface between the client and server, and it unconditionally throws an exception if the socket write returns an error code. Other callers of WriteBufferFromFileDescriptor do appear to be to non-socket writes (stdout/stderr/pipes between threads).

Given the server ignores EPIPE and so does the embedded client, I think extending this behavior to the client should be safe but will take another pass since I'm new to the codebase.

alexey-milovidov and others added 2 commits October 19, 2026 03:36
The previous approach was unsafe:

- Swallowing `EPIPE` in `WriteBufferFromFileDescriptor::nextImpl` affected every
  pipe, also on the server: data written to a child process that exited early was
  silently dropped, and `UserDefinedExecutableFunctionDriverInvoker` and
  `clickhouse-disks sed`, which catch the `EPIPE` `ErrnoException`, would get a
  `LOGICAL_ERROR` exception from `finalize` of the silently canceled buffer.
- Ignoring `SIGPIPE` in the client was redundant: Poco already blocks it in all
  threads, which is why the write fails with `EPIPE` at all. And an ignored signal
  is inherited through `exec`, while `ShellCommand` resets only the signal mask, so
  the pager and executable UDFs started by `clickhouse-local` would inherit it.

Instead, the console output buffer of the client remembers that its write failed
with `EPIPE`. Then the client does not print the error, does not run further
queries (even with `--ignore-error`), and exits with code 0.

Closes: #126443

Co-Authored-By: Claude Opus 5.5 <[email protected]>
With `2>&1 | head -1` the error message went into the same pipe and was cut off
by `head`, so the test passed without the fix. Capture stderr separately, check
the exit code, and cover `--ignore-error` and non-parallel formatting.

`05238_pretty_squash_broken_pipe` pinned exit code 75 for a broken output pipe;
it is 0 now, intentionally. The test still checks that the query stops and is
cancelled on the server.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CLAassistant

CLAassistant commented Oct 19, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@alexey-milovidov

Copy link
Copy Markdown
Member

@davidrz15, please sign the CLA.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 19, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4fe64d4]

Summary: ✅


AI Review

Summary

The PR makes clickhouse-client and clickhouse-local stop quietly with exit code 0 after EPIPE on their stdout, skipping subsequent batch queries while preserving other write errors. The previous finding about INTO OUTFILE … AND STDOUT is addressed. No unresolved findings remain at commit 4fe64d4d7c79647a0ee592a3173915fc7939b0a3; both regression tests pass in Fast test, and focused checks with the PR binary confirm the batch-stop behavior and preservation of non-EPIPE errors.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Oct 19, 2026
The test commands in 05401_epipe_stdout_head were using 100m rows
when max_rows_to_read is set to 20m in the CI env, downsize to 1m
rows so the queries actually run.
Comment thread src/Client/ClientBase.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 4fe64d4d7 with master c238db144 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

21 translation units recompiled, 131 s compile time in total, 21 of them have a recent master baseline.

Job report

When the client is writing to a file and stdout we were still using
the old WriteBufferFromFileDescriptor instead of the epipe handling
WriteBufferToStdout. Replace it and add test coverage to validate.
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 About the concern that ignoring the pipe signal could interfere with sockets: the current version of the PR no longer touches the signal disposition.

  • SIGPIPE handling is unchanged. The PR does not set SIG_IGN, which child processes such as the pager would inherit.
  • WriteBufferFromFileDescriptor is also unchanged, so sockets, pipes to executable UDF child processes and every other file descriptor still raise an error on EPIPE.
  • Only the client's own stdout writers (std_out, plus the stdout branch of ForkWriteBuffer for INTO OUTFILE ... AND STDOUT) use a small subclass, WriteBufferToStdout. When a write fails with EPIPE, it sets stdout_is_broken and rethrows. processError and the top-level catch blocks in Client/LocalServer then exit quietly with code 0 and skip the remaining queries.

The bot's finding about INTO OUTFILE ... AND STDOUT was addressed in 4fe64d4 and has a regression case. CI is green. Please re-review when you have time.

@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.

Good.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 21, 2026
Merged via the queue into ClickHouse:master with commit 9ce1640 Oct 22, 2026
179 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 22, 2026
@davidrz15
davidrz15 deleted the epipe_fix branch October 22, 2026 04:49
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 29, 2026
Since ClickHouse#126638 the client stops quietly with exit code 0 when the reader
of its own stdout goes away, also for the stdout leg of
`INTO OUTFILE ... AND STDOUT`, so the `closed_stdout` arm no longer sees
exit code 75 and `CANNOT_WRITE_TO_FILE_DESCRIPTOR`. The arm still
catches the regression: without the fix the debug client aborts in
`ClientBase::resetOutput` -> `IOutputFormat::finalize` ->
`JSONUtils::writeArrayEnd`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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-cli clickhouse-client (interactive CLI) and client library. external Author is not a member of the ClickHouse organization pr-improvement Pull request with some product improvements 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.

clickhouse-local prints Broken pipe exceptions when the output is piped into head

4 participants