Repository navigation
Do not write ErrorResponse into a canceled PostgreSQL socket buffer - #118369
Conversation
When a PostgreSQL protocol client goes away while the server is still streaming the result, the next write to the socket fails with `NETWORK_ERROR` (`Broken pipe`), and `WriteBuffer::next` cancels the socket buffer. The error handlers of `processQuery` and `processExecuteQuery` in `PostgreSQLHandler` then tried to send `ErrorResponse` through the same buffer, which is a logical error: `Cannot write to canceled buffer`. Tear the connection down instead when the output buffer is already canceled: there is nobody left to deliver the error to. Seen in the stress test of a PR that does not touch this code: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=111327&sha=ff11b640e3ff97e92863685fdba4882a09597508&name_0=PR&name_1=Stress%20test%20%28arm_debug%29 (#111327), and on master on 2026-09-05. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Workflow [PR], commit [f001115] Summary: ✅
AI ReviewSummaryThis PR fixes the PostgreSQL-wire disconnect path by routing statement-level error reporting through Final Verdict
LLVM Coverage ReportMeasured on commit f001115.
Changed lines: Changed C/C++ lines covered: 28/28 (100.00%) · Uncovered code |
|
The same unguarded |
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, 17 s compile time in total, 8 of them have a recent master baseline. |
Address review from `clickhouse-gh` and @zlareb1: the guard was only in `processQuery` and `processExecuteQuery`, while `processParseQuery`, `processBindQuery`, `processDescribeQuery`, `processCloseQuery` and `processSyncQuery` still sent `ErrorResponse` unconditionally from their `catch` blocks. Factor the send into `sendErrorResponseOrRethrow`, which rethrows the exception being handled when `out` is already canceled, and use it in every place that reports a failed statement to the client. Also stop `05099_postgresql_protocol_client_disconnect_mid_result` from failing with `having stderror`: `bash` reports the death of the background `psql` job as `Killed` on the stderr of the shell itself, not on the stderr of `wait`, so the shell's stderr has to be redirected around the `kill`/`wait` pair. Add `05100_postgresql_protocol_extended_client_disconnect_mid_result`, which covers the same disconnect on the extended-query path with a minimal `Parse`/`Bind`/`Execute`/`Sync` client that aborts the connection in the middle of the result. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…r-response-on-canceled-socket Conflict in `src/Server/PostgreSQLHandler.h`: master added `recoverFromRejectedMessage` next to where this branch added `sendErrorResponseOrRethrow`. Kept both declarations. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Merging master brought in other tests with the `05100` prefix. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 @zlareb1 thank you — you were right, and the list of call sites was exactly what was needed. Fixed in 409d62d: the Also added |
…2` as `no-fasttest` The `Cannot write to canceled buffer` count in `05099` and `05182` was global over the whole retained `system.text_log`, so a row left by an earlier PostgreSQL test on the same server would have failed it. Both checks now use a `RUN_START` anchor on `event_time_microseconds` and set `max_rows_to_read = 0`. `05182` opens `CLICKHOUSE_PORT_POSTGRESQL`, which fasttest does not enable, so it gets the same `no-fasttest` tag as the other PostgreSQL wire-protocol tests. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…r-response-on-canceled-socket
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118369&sha=409d62d37d6526ec76d3f0fa442b895144fbf738&name_0=PR&name_1=Upgrade%20check%20%28amd_release%29 ( The other two reds on that run were the fleet-wide |
|
Not the PostgreSQL handler, agreed. On this run the abort is the parallel-replicas symptom of #112891, and my fix for that is already open: #114955. The failing statement is Two notes on where this is visible. The abort is on the previous release ( One thing to expect in this check regardless: the same STID also reaches it by the join route, |
…r-response-on-canceled-socket
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118369&sha=0db9b39955866061975e2881f884582961c915de&name_0=PR&name_1=Stress%20test%20%28arm_msan%29 ( |
|
Investigated. Not caused by this PR, and the frame quoted above is not the stuck thread.
The single entry under Two of its 14 threads hold the wedge, both inside the per-granule loop of
with A fix is already in progress and it is not mine: #108357. Its Fix-presence probe: So there is no separate PR from me here; I am tracking #108357 and will report if it stops being the owner. |
|
🕵 Thanks @groeneai, that makes sense: the stress hang is the uncancellable per-granule write loop tracked in #108357, not this PR. The only red on the last run ( I merged the latest master ( |
Related: #111327
When a PostgreSQL protocol client goes away while the server is still streaming the result, the next write to the socket fails with
NETWORK_ERROR(Broken pipe) andWriteBuffer::nextcancels the socket buffer. The error handlers inPostgreSQLHandlerthen tried to sendErrorResponsethrough the same buffer, which is a logical error:Now the handler tears the connection down instead when the output buffer is already canceled: there is nobody left to deliver the error to. The check lives in a single helper,
sendErrorResponseOrRethrow, which is used by every place that reports a failed statement to the client -processQuery,processParseQuery,processBindQuery,processDescribeQuery,processExecuteQuery,processCloseQueryandprocessSyncQuery- so the invariant "onceoutis canceled, no handler recovers by sending another message" holds for the extended-query protocol as well.Tests:
05099_postgresql_protocol_client_disconnect_mid_resultkillspsqlwithSIGKILLin the middle of a streamed simple query, and05100_postgresql_protocol_extended_client_disconnect_mid_resultdoes the same for the extended-query protocol with a minimalParse/Bind/Execute/Syncclient that aborts the connection withRST. Both check that the statement disappears and that no such message reachessystem.text_log.Seen in
Stress test (arm_debug)of a PR that does not touch this code: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=111327&sha=ff11b640e3ff97e92863685fdba4882a09597508&name_0=PR&name_1=Stress%20test%20%28arm_debug%29, and on master on 2026-09-05 (the test05056_postgresql_protocol_cancel_requestexitspsqlwhile the result is still streaming, so under load the server hits the broken pipe before the cancellation is processed).Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a logical error
Cannot write to canceled bufferin the PostgreSQL wire protocol handler when a client disconnects while the server is still sending the result of a query.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/118369]
Version info
26.10.1.1452-master(included in26.10and later)