Repository navigation
Fix a data race on the gRPC output buffer with the Pretty squash thread - #128292
Conversation
With a `Pretty` format and `output_format_pretty_squash_consecutive_ms != 0`, `PrettyBlockOutputFormat` renders the squashed blocks from a background thread into the gRPC call's output buffer, under `IOutputFormat::writing_mutex`. The call thread read that buffer between the format calls without the mutex (`has_output` in `Call::generateOutput`, the copy and `restart()` in `Call::sendResult` for streaming calls), and finalized it on the exception path while the writer could still render a pending chunk (TSan STID 2046-35c1 in `Stress test (amd_tsan)`, where ClickHouse#125517 runs `.sql` tests over gRPC). - `IOutputFormat::lockWriting()` exposes the writing lock to the owner of the output buffer. - `Call::generateOutput` no longer reads the buffer: `has_output` only duplicated the "nothing to send" check of `sendResult`. - `Call::sendResult` holds the lock while it copies a non-final message. - `Call::onException` destroys the format (joining its writer) before the buffer is finalized. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The exception case of 05473 used to arrive ~50 ms after the writer had rendered the last row, so there was no pending chunk to race with. Now row 2 throws right after row 1, and the squash delay sweeps 1..5 ms, so the writer's timer can fire while the call handles the exception. 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; 0 findings) plus my own cold review (2 nits, neither blocking).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261030-212400 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261030-205600 |
|
Workflow [PR], commit [bed1b38] Summary: ✅
AI ReviewSummaryReviewed #128292 at Missing context / blind spots
Final VerdictStatus: ✅ Approve. No code changes requested; required CI checks still need to complete. |
|
cc @CheSema @vitlibar, could you review this and add the |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units99 translation units recompiled, 597 s compile time in total, 99 of them have a recent master baseline. |
CI finish ledger - 9266e61Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The first run of this head stopped at Session id: cron:our-pr-ci-monitor:20261031-140009 |
|
Fixing PR for the |
CheSema
left a comment
There was a problem hiding this comment.
#128018 is merged, so this PR now conflicts in generateOutput. Please merge master and resolve both has_output() sites:
- pulling loop:
call_responder->takeLogsFromQueue(); sendResult(); - completed branch:
if (result.has_progress() || call_responder->hasUnsentLogs()) sendResult();
After #128018, onFatalError no longer calls sendResult, so a final message is sent only after the format is finalized or destroyed. Please state that rule in the comment above the lock in sendResult: the lock skip depends on it.
Nits: remove the unused {} placeholder in the test query; add Related: https://github.com/ClickHouse/ClickHouse/pull/128018 to the description.
…y-squash-race # Conflicts: # src/Server/GRPCServer.cpp
…st placeholder `Call::sendResult` sends a final message only from `finishQuery`, after `generateOutput` has finalized the output format, and from `onException`, after the format is destroyed. The fatal error callback sends its own result and does not touch the output buffer. So no writing thread can be running when the final message finalizes the buffer. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks, done:
On a debug build of the merged head, |
CheSema
left a comment
There was a problem hiding this comment.
Readability suggestion for sendResult, since you will touch it for the merge anyway:
- Take the lock whenever
output_format_processoris set, not only for intermediate messages. It is safe:sendResultruns only on the call thread and never underwriting_mutex, and for a final message the writer is already stopped. Then the code does not depend on the final-message rule, and the comment I asked for is not needed. - Move the copy into a helper, e.g.
addOutputToResult(bool final). The helper can return early on!write_buffer || write_buffer->isCanceled(), the lock lives for the whole helper, and the 30-line re-indent goes away. Keep the lock before theisCanceledcheck:WriteBuffer::nextcancels the buffer whennextImplthrows, and that can happen on the writer thread.
The helper takes the format's writing lock whenever the output format exists, for intermediate and final messages alike, so the copy no longer depends on how the final message is ordered against the format's finalization. The lock is taken before the `isCanceled` check because `WriteBuffer::next` cancels the buffer when `nextImpl` throws, which can happen on the format's writing thread. `sendResult` runs only on the call thread and never under the writing mutex, so the lock cannot deadlock. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks, done in bed1b38:
On a TSan build of this head, |
Related: #128018
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed lost, duplicated or corrupted rows when a
Pretty*result was streamed over gRPC (ExecuteQueryWithStreamOutput) while consecutive blocks were squashed (output_format_pretty_squash_consecutive_ms, enabled by default).Description
On 26.3 and 26.8, streaming a
Pretty*result over gRPC (ExecuteQueryWithStreamOutput) can lose, duplicate or corrupt rows, because the call thread copied and reset its output buffer while thePrettysquash thread was writing into it.Found by
Stress test (amd_tsan), STID 2046-35c1, on an unrelated PR (the stress test runs.sqltests over gRPC since #125517): report.With
output_format_pretty_squash_consecutive_ms != 0,PrettyBlockOutputFormatrenders squashed blocks from a background thread into the call's output buffer, underIOutputFormat::writing_mutex. Between blocks,Call::generateOutputread that buffer (has_output), andCall::sendResultcopied it and calledrestart()without the mutex. On the exception path the buffer was finalized while the writer could still render a pending chunk.IOutputFormat::lockWriting()exposes the writing lock to the owner of the output buffer.generateOutputno longer reads the buffer:has_outputonly duplicated the "nothing to send" check ofsendResult.sendResultcopies the output inaddOutputToResult, which holds the lock whenever the format exists.onExceptiondestroys the format, which joins the writer, before the buffer is finalized.Validation: the new test
05473_grpc_pretty_squash_output(unary, streamed and failing queries) produces data race reports in 5/5 runs on the TSan binary of the failing CI run, and none with the fix; it passes 50/50 on a debug build. Streaming 100PrettyCompactqueries of 30 rows, one row per block, with a 1 ms squash delay, returned wrong rows for up to 31 of them on 26.3.60.2 and 9 on 26.8.34.13, and was correct 100/100 with the fix.test_grpc_protocolpasses.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/128292]
Version info
26.11.1.1506-master(included in26.11and later)