Skip to content

Fix data race in ClientBase::onProgress with the embedded client - #128395

Closed
alexey-milovidov wants to merge 6 commits into
masterfrom
fix-local-connection-progress-race
Closed

alexey-milovidov wants to merge 6 commits into
masterfrom
fix-local-connection-progress-race

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

Related: #100230

The interactive cancel callback set by LocalConnection::sendQuery (introduced in #100230) also sends accumulated progress by calling ClientBase::onProgress. The callback is called not only by the client thread but also by pipeline threads. For example, when an INSERT ... SELECT triggers a materialized view with a scalar subquery, PullingAsyncPipelineExecutor::pull inside ExecutingInnerQueryFromViewTransform calls it at the same time as CompletedPipelineExecutor::execute on 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 to clickhouse-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_cancelled is 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):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix a data race in progress reporting of clickhouse-local and the embedded client (SSH, web terminal) when a query runs nested pipelines in parallel threads, such as materialized views with scalar subqueries during INSERT.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/128395]

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]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e224404]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_msan, flaky check) FAIL
05480_local_progress_mv_scalar_subquery_race FAIL cidb
05480_local_progress_mv_scalar_subquery_race FAIL cidb
05480_local_progress_mv_scalar_subquery_race FAIL cidb
05480_local_progress_mv_scalar_subquery_race FAIL cidb
05480_local_progress_mv_scalar_subquery_race FAIL cidb
05480_local_progress_mv_scalar_subquery_race FAIL cidb
05480_local_progress_mv_scalar_subquery_race FAIL cidb
05480_local_progress_mv_scalar_subquery_race FAIL cidb
05480_local_progress_mv_scalar_subquery_race FAIL cidb
05480_local_progress_mv_scalar_subquery_race FAIL cidb
2 more test cases not shown
Stateless tests (amd_tsan, s3 storage, targeted) FAIL
05480_local_progress_mv_scalar_subquery_race FAIL cidb
05480_local_progress_mv_scalar_subquery_race FAIL cidb
Stateless tests (amd_tsan, flaky check) ERROR

AI Review

Summary

The 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

⚠️ Majors

  • tests/queries/0_stateless/05480_local_progress_mv_scalar_subquery_race.sh:21: interactive_delay = 1 means one microsecond. LocalConnection::poll returns progress before reaching pollImpl, which marks the completed insert finished. If each packet round trip takes at least one microsecond, it repeatedly returns even empty progress packets and never reaches EndOfStream. This can cause sanitizer timeouts independently of the reduced workload. Remove the override or use the normal interval; worker scalar-subquery callbacks still execute after pulling chunks. Inline finding.
Tests
  • ⚠️ The current test passes in Fast test, but current-head TSan validation is still queued. After correcting the interval, confirm that the focused test completes under TSan with the flaky-check thread fuzzer and reports no race. No local execution was possible because this checkout contains no ClickHouse binary or build directory.
Final Verdict

Status: ⚠️ Request changes. Use a progress interval that permits query completion and confirm the focused test under TSan.

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-local clickhouse-local / embedded modes (incl. local file processing). labels Oct 31, 2026
@alexey-milovidov
alexey-milovidov marked this pull request as ready for review October 31, 2026 18:05

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little bit weird, but ok.

Comment thread src/Client/LocalConnection.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing e2244042d with master e6c0a8e34 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

22 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.
The matched translation units cost -7.1 s (-5%) in total before that adjustment.

Job report

alexey-milovidov and others added 2 commits November 2, 2026 02:37
…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]>
alexey-milovidov and others added 2 commits November 3, 2026 06:31
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🕵 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).

@PedroTadim

Copy link
Copy Markdown
Member

@alexey-milovidov do we need this after #128019 gets merged?

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @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 master (5f014d8), and nothing later reverted it. Current master sends progress from the interactive cancel callback of LocalConnection::sendQuery only when getThreadId() == client_thread_id, and LocalQueryState::is_cancelled is std::atomic_bool, so the code matches this PR exactly. Its test 05454_local_progress_from_pipeline_threads covers the same scenario (a materialized view with a scalar subquery during a parallel INSERT). This PR's test 05480_local_progress_mv_scalar_subquery_race was also failing the MSan flaky check because of interactive_delay = 1. Closing as superseded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-local clickhouse-local / embedded modes (incl. local file processing). pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants