Repository navigation
Fix data race in ClientBase::onProgress with the embedded client - #128395
alexey-milovidov wants to merge 6 commits into
Conversation
The interactive cancel callback set by `LocalConnection::sendQuery` also sends accumulated progress to the client by calling `ClientBase::onProgress`. The callback is invoked not only by the client thread, but also by pipeline threads, e.g. when a materialized view with a scalar subquery is executed during `INSERT ... SELECT` (`PullingAsyncPipelineExecutor::pull` inside `ExecutingInnerQueryFromViewTransform`), so `ClientBase::onProgress` was called concurrently, which is not thread-safe. Send the progress only from the thread that sent the query; progress from other threads stays in `state->progress` and is sent later by the client thread. Also make `LocalQueryState::is_cancelled` atomic, as the same callback sets it from arbitrary threads. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=977ed4e7292b3c64b89d31e5d2b0399e6589eb86&name_0=MasterCI&name_1=Stress%20test%20%28azure%2C%20amd_tsan%29 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Workflow [PR], commit [e224404] Summary: ❌
AI ReviewSummaryThe PR confines interactive progress delivery to the query's client thread, makes cancellation state atomic, and adds a regression test. The earlier coverage request has been addressed, but the new test's progress interval can prevent query completion under instrumentation. Findings
Tests
Final VerdictStatus: |
Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov
left a comment
There was a problem hiding this comment.
A little bit weird, but ok.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units22 translation units recompiled, 132 s compile time in total, 22 of them have a recent master baseline. Median compile-time ratio to the baselines is ×0.95 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
…e-local` An `INSERT ... SELECT` into a table with a materialized view containing a scalar subquery makes pipeline threads invoke the interactive cancel callback of `LocalConnection` concurrently with the client thread. The test checks that the query works and that the progress of a scalar subquery reaches the final statistics. Address review: #128395 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
The test timed out (600 s) in every run of the TSan and MSan flaky checks and the targeted TSan job. Each inserted block runs the materialized view and its scalar subquery in separate pipelines. The thread fuzzer sleeps and migrates threads on mutex operations, which makes each of them very slow, so 100 blocks of a `MergeTree` insert with 8 threads did not finish. Locally with the thread fuzzer settings of the flaky check, even one block sometimes took minutes. Use 8 blocks, 4 threads and a `Memory` table. A temporary debug print confirmed that pipeline threads still invoke the interactive cancel callback (4-5 times per run). The test now takes about 13 s under the thread fuzzer locally. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=128395&sha=97f4162d4f58ffc182752bb216a1487e01eb4674&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20flaky%20check%29 PR: #128395 Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
|
||
| INSERT INTO src SELECT number FROM numbers(4000) | ||
| SETTINGS max_block_size = 500, min_insert_block_size_rows = 500, min_insert_block_size_bytes = 0, | ||
| max_threads = 4, max_insert_threads = 4, parallel_view_processing = 1, interactive_delay = 1; |
There was a problem hiding this comment.
[P2] Avoid starving query completion with a one-microsecond progress interval
interactive_delay is measured in microseconds. After the completed INSERT returns from sendQuery, state->is_finished remains false until LocalConnection::pollImpl runs. However, LocalConnection::poll first calls needSendProgressOrMetrics, which restarts the timer and returns a Progress packet whenever this interval has elapsed, even when the counters are empty. If processing each packet and returning to poll takes at least one microsecond, every iteration takes this branch and never reaches pollImpl or EndOfStream. Sanitizer instrumentation can therefore make the test time out even after all eight blocks have finished; reducing the input does not remove this condition.
Please remove this override or use the normal interval, then validate the reduced test under TSan. The scalar-subquery executor invokes its cancel callback after pulling a chunk, so a one-microsecond interval is unnecessary to exercise the worker-thread callback.
There was a problem hiding this comment.
🕵 Not addressed here: the PR is being closed as superseded by #128019, which landed the same fix in master with its own test 05454_local_progress_from_pipeline_threads (it uses interactive_delay = 10000, so this concern does not apply there).
|
@alexey-milovidov do we need this after #128019 gets merged? |
|
🕵 @PedroTadim, no, it is not needed: #128019 was merged on 2026-11-03 and made the same change. Its commit 0c46cee is an ancestor of |
Related: #100230
The interactive cancel callback set by
LocalConnection::sendQuery(introduced in #100230) also sends accumulated progress by callingClientBase::onProgress. The callback is called not only by the client thread but also by pipeline threads. For example, when anINSERT ... SELECTtriggers a materialized view with a scalar subquery,PullingAsyncPipelineExecutor::pullinsideExecutingInnerQueryFromViewTransformcalls it at the same time asCompletedPipelineExecutor::executeon the client thread. As a result,ClientBase::onProgress(not thread-safe) ran concurrently. TSan reported this for the embedded client (ClientEmbedded, used by SSH / the web terminal) in a stress test. The same applies toclickhouse-local.Fix: only the thread that sent the query sends progress. Progress from other threads stays in
state->progress, and the client thread sends it later.LocalQueryState::is_cancelledis also made atomic, because the same callback sets it from arbitrary threads.CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=977ed4e7292b3c64b89d31e5d2b0399e6589eb86&name_0=MasterCI&name_1=Stress%20test%20%28azure%2C%20amd_tsan%29
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a data race in progress reporting of
clickhouse-localand the embedded client (SSH, web terminal) when a query runs nested pipelines in parallel threads, such as materialized views with scalar subqueries duringINSERT.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/128395]