Repository navigation
Do not share the output buffer of a gRPC call with its fatal error callback - #128018
Conversation
…llback `SignalListener::onFault` runs the query's fatal error callback on the signal listener thread while the gRPC call thread can still be running (a fault in a pipeline thread, or a SIGTSTP stack-trace request). `Call::onFatalError` then mutated `result` and finalized the call's write buffer concurrently with the call thread writing output into it: a data race (TSan, STID 0250-04e2 in `Stress test (amd_tsan)`), and the call thread's next write threw `Cannot write to finalized buffer` (an abort in debug/sanitizer builds). The callback now sends its own `FatalError` result and never touches `result` or the output buffers. Writes to the responder are serialized by a mutex held in a `shared_ptr` the callback captures weakly (the callback can outlive the call, as in TCPHandler); the callback uses the call only while it is not closed and gives up after 1 s instead of waiting for a call thread that may be the faulting one. It also cancels the query, which nobody can receive the rest of. Logs taken from the logs queue but not sent yet wait under the same mutex, so the fatal message still carries them for every call type (a non-streaming call sends its logs only with the final message). The logs queue is set up before the callback is installed, and transport compression is applied under the mutex unless the callback has already finished the call. When the callback finished the call first, the call thread's final result is not sent and its output buffer is never finalized, so `close()` cancels it (a compressing output buffer would otherwise be destroyed neither finalized nor canceled). The new integration test `test_grpc_fatal_error` sends SIGTSTP to a pipeline thread of a gRPC query in three cases: `ExecuteQueryWithStreamOutput` without and with output compression, and `ExecuteQuery`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the ResponderLock comment to one line and drop four comments that restate the code next to them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review (3 rounds before publication; the fixes are in this PR)
|
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261028-003400 |
|
cc @vitlibar @CheSema, could you review this and add the |
|
Workflow [PR], commit [a71b7ec] Summary: ✅
AI ReviewSummaryThe PR isolates the gRPC fatal-error callback from the call's output buffers and serializes responder access through Final VerdictStatus: ✅ Approve |
CI finish ledger - 4e387adEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
The new integration test and Session id: cron:our-pr-ci-monitor:20261028-180006 |
…ponder The fatal error callback now captures only a weak_ptr to CallResponder, which owns the responder, the logs queue, the unsent logs and the write state, all guarded by one mutex. The raw Call pointer, call_closed and the second addLogsToResult overload are gone. After close() the responder is null, so a late callback does nothing. 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 Object file sizes3 object files changed (+18.94 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units7 translation units recompiled, 15 s compile time in total, 7 of them have a recent master baseline. |
CI finish ledger - 7f1b9c6Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261029-173003 |
|
@groeneai check Fast tests, is it related? Update branch and trigger CI if it is not related. |
…atal error - `close` cancels a pending write and waits for it, instead of destroying the responder that owns its completion tag. The final `sendResult` can throw from `finalize` while an intermediate write is in flight. - If starting a write throws, `sending` is reset and the call stays unfinished with its logs unsent, so `close` and `waitWritten` cannot wait forever. - `read` refuses to start a read once the call is finished. - `processInput` checks `isQueryCancelled` before pulling each block, so an inline `INSERT` stops after `FatalError`, and a call with streaming input stops reading. The new test case sends SIGTSTP to the call thread of an inline `INSERT` over `ExecuteQuery`; without the check the insert keeps running. Co-Authored-By: Claude Opus 5.5 <[email protected]>
08d8752
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Description
When any thread of a gRPC query hits a fatal error, or receives
SIGTSTP(the "print a stack trace" signal),SignalListener::onFaultruns the query's fatal error callback on the signal listener thread while the gRPC call thread is still running.Call::onFatalErrorthen wroteresultand finalized the call's output buffer concurrently with the call thread writing into them.Stress test (amd_tsan)reported it asThreadSanitizer: data race(GRPCServer.cpp:1374vsCall::sendResult<-Call::onFatalError) instead of the job's actual crash: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=103402&sha=1aba14a95c8dff9125f973707ff65c4911c8839a&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29. In debug and sanitizer builds the call thread's next write also aborts withLogical error: 'Cannot write to finalized buffer'.The callback now sends its own
FatalErrorresult and never touchesresultor the output buffers. Everything it uses (the responder, the logs queue, the logs taken from it but not sent yet, the write state) lives inCallResponder, guarded by one mutex, so the fatal message still carries every log line. The callback capturesCallResponderbyweak_ptr, because it can outlive the call (as in TCPHandler, #94292). It waits at most 1 s for the mutex, since the faulting thread can be the call thread itself, and it cancels the query; an inlineINSERTchecks for that before each block.CallResponder::close()cancels and waits for a pending write, and no read is started after the call is finished.The new integration test
test_grpc_fatal_errorsendsSIGTSTPto a pipeline thread of anExecuteQueryWithStreamOutputcall, with and without output compression, and of anExecuteQuerycall, and to the call thread of an inlineINSERT. On master the server aborts in the streaming cases, the non-streaming query and the insert keep running, and the TSan build reports the race. With the fix all four cases pass in debug and TSan builds with no TSan report, as dotest_grpc_protocolandtest_grpc_protocol_ssl.Workflow [PR]
Sync PR [sync-upstream/pr/128018]
Version info
26.11.1.1349-master(included in26.11and later)