Repository navigation
Terminate query quietly on EPIPE signal - #126638
Conversation
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
left a comment
There was a problem hiding this comment.
This is interesting because ignoring the pipe signal might interfere with other cases like sockets.
|
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. |
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]>
|
@davidrz15, please sign the CLA. |
|
Workflow [PR], commit [4fe64d4] Summary: ✅
AI ReviewSummaryThe PR makes Final VerdictStatus: ✅ Approve |
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.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units21 translation units recompiled, 131 s compile time in total, 21 of them have a recent master baseline. |
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.
|
🕵 About the concern that ignoring the pipe signal could interfere with sockets: the current version of the PR no longer touches the signal disposition.
The bot's finding about |
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]>
When the reader of the output of
clickhouse-clientorclickhouse-localgoes away, as inclickhouse-local -q '...' | head -1, the client printed aBroken pipeexception (CANNOT_WRITE_TO_FILE_DESCRIPTOR) and exited with code 75. Now it stops quietly, likecatand 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.WriteBufferFromFileDescriptorand 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, andSIG_IGNwould be inherited by child processes such as the pager.Closes: #126443
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
clickhouse-clientandclickhouse-localstop quietly with exit code 0, without aBroken pipeexception, 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
26.11.1.485-master(included in26.11and later)