Skip to content

Fix data race in ClientBase::onProgress called by LocalConnection from pipeline threads - #128019

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-local-connection-progress-thread
Nov 3, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-local-connection-progress-thread

Conversation

@groeneai

@groeneai groeneai commented Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #100371
Related: #100230

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

clickhouse-local and the embedded client no longer call the client's progress handler from pipeline threads.

Description

Stress test (arm_tsan) on #100371 reports a data race in ClientBase::onProgress: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=100371&sha=9373f31b7d5da16c1af8cca79c27d546ab9c5d77&name_0=PR&name_1=Stress%20test%20%28arm_tsan%29

Since #100230, the interactive cancel callback that LocalConnection::sendQuery installs also forwards accumulated progress to the client. Nested executors poll that callback (scalar subqueries, sets), and they run on pipeline threads when a query is analysed inside a running pipeline, for example the query of a materialized view with a scalar subquery over the view source, analysed for every inserted block. The client thread meanwhile calls the same progress handler from the CompletedPipelineExecutor poll, so ClientBase::onProgress, which is not thread-safe, runs on two threads at once.

The callback now forwards progress only on the thread that called sendQuery. On other threads the progress stays in the atomic state->progress and reaches the client on its next poll or with the final Progress packet, so totals are unchanged and cancellation still works from any thread. LocalQueryState::is_cancelled becomes atomic because that callback sets it on a pipeline thread.

This is a ThreadSanitizer report in CI and no wrong result has been observed in a release, so the category is CI Fix.

Validation: under TSan the new test 05454_local_progress_from_pipeline_threads aborts with this race in 20 of 20 runs without the fix and passes 20 of 20 with it; --processed-rows checks that the deferred progress is still delivered (a variant that drops it changes the count in 20 of 20 runs); 50 randomized runs pass on a debug build.

#125990 and #125610 carry the identical change; they merge cleanly in any order.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.11.1.1186-master (included in 26.11 and later)

… from pipeline threads

The interactive cancel callback that `LocalConnection::sendQuery` installs
(since ClickHouse#100230) also forwards accumulated progress to the client. Nested
executors poll it (scalar subqueries, sets), and they run on pipeline
threads when a query is analysed inside a running pipeline, for example the
query of a materialized view with a scalar subquery over the view source,
analysed for every inserted block. The client thread meanwhile calls the
same progress handler from the `CompletedPipelineExecutor` poll, so
`ClientBase::onProgress`, which is not thread-safe, ran on two threads at
once.

Forward progress only on the thread that called `sendQuery`; on other
threads it stays in the atomic `state->progress` and reaches the client on
its next poll or with the final `Progress` packet. Make
`LocalQueryState::is_cancelled` atomic, as that callback sets it on a
pipeline thread.

The new test aborts under ThreadSanitizer in 20 of 20 runs without the fix
and passes 20 of 20 with it; its `--processed-rows` count checks that the
deferred progress still reaches the client (a variant that drops it changes
the count in 20 of 20 runs). Under the flaky check's thread fuzzer one run
takes at most 24 s.

CI report (Stress test (arm_tsan), STID 1003-46d8):
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=100371&sha=9373f31b7d5da16c1af8cca79c27d546ab9c5d77&name_0=PR&name_1=Stress%20test%20%28arm_tsan%29
Related: ClickHouse#100371

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (3 rounds): 2 fixed, 1 dismissed
Finding Verdict
⚠️ The test did not check that progress left by pipeline threads is still delivered to the client AGREE, fixed: the test runs with --processed-rows, so a variant that drops that progress changes the count
⚠️ Cancellation from a pipeline thread is not exercised DISAGREE: the cancel branch is unchanged master code; only the type of is_cancelled changed, and the thread check does not wrap it. 04052 and 04003 cover cancellation
⚠️ The test took about 90 s per run under the flaky check's thread fuzzer, against the 180 s limit with many concurrent copies AGREE, fixed: 10 inserted blocks instead of 100; still 20 of 20 TSan aborts without the fix, at most 24 s per run under the fuzzer

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Under TSan 05454_local_progress_from_pipeline_threads.sh aborts in 20 of 20 runs without the fix, with the CI stack: ClientBase::onProgress (ClientBase.cpp:2179) from the interactive cancel callback (LocalConnection.cpp:337, evaluateScalarSubqueryIfNeeded) against the CompletedPipelineExecutor poll (LocalConnection.cpp:639).
b Root cause explained? Since #100230 the interactive cancel callback of LocalConnection::sendQuery forwards progress to the client. Nested executors poll it on pipeline threads when a query is analysed inside a running pipeline (an MV query with a scalar subquery over the view source), while the client thread runs the same handler from its own poll. ClientBase::onProgress is not thread-safe.
c Fix matches root cause? Yes. The callback forwards progress only on the thread that called sendQuery; elsewhere progress stays in the atomic state->progress and is delivered by the client thread. is_cancelled becomes atomic. No tags, no widened bounds.
d Test intent preserved / new tests added? New stateless test without tags; no existing test changed.
e Both directions demonstrated? Yes. Without the fix 20/20 TSan aborts; with it 0/20 reports and 20/20 correct output. 50/50 randomized runs OK on a debug build, and 50/50 under the flaky check's thread fuzzer (16 concurrent copies, at most 24 s per run).
f Fix is general across code paths? Yes. All pollers of getInteractiveCancelCallback call this one lambda. Remote connections deliver progress as packets on the client thread, and TCPHandler serializes its callback with callback_mutex, so LocalConnection is the only place that calls the client from a server thread.
g Fix generalizes across inputs (params/datatypes/wrappers)? No data types involved. Measured with interactive_delay 1000, 10000 and 100000, block sizes 100 and 1000, 1e5 and 1e6 rows: 0 reports with the fix. send_progress off keeps the old path.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, protocol or format change.
i Invariants and contracts preserved? The client is called only on its own thread; sendQuery, its CompletedPipelineExecutor poll and every receivePacket run there, so forwarding on that thread is unchanged. Totals are unchanged (final Progress packet), and 04050_local_json_progress_stats and 04052_local_cancel_scalar_subquery pass, also under TSan.

Session id: cron:clickhouse-impl-slot-5:20261027-232700

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @azat @yakov-olkhovskiy, could you review this and add the can be tested label so CI runs? Nested executors poll the interactive cancel callback of LocalConnection::sendQuery on pipeline threads, so it now forwards progress to the client (not thread-safe) only on the thread that called sendQuery, and LocalQueryState::is_cancelled becomes atomic.

@clickhouse-gh

clickhouse-gh Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0c46cee]

Summary: ❌

job_name test_name status info comment
Build profile diff ERROR

AI Review

Summary

PR #128019, reviewed at 0c46cee56fc24b00c884d585bc2ce74dd76ed0c1, confines LocalConnection progress forwarding to the thread running sendQuery and makes LocalQueryState::is_cancelled atomic. No actionable findings.

Missing context / blind spots
  • ⚠️ TSan CI jobs are still pending. The new regression test passed Fast test; the author reports 20/20 TSan failures before the fix and 20/20 passes afterward. That sanitizer result was not independently rerun during this review; the completed TSan flaky check would close this verification gap.
Final Verdict

✅ Approve. No code changes requested; pending CI checks still need to complete.

@clickhouse-gh clickhouse-gh Bot added pr-ci comp-local clickhouse-local / embedded modes (incl. local file processing). labels Oct 28, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 28, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 0c46cee

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Build profile diff infra, not from this PR: the CI logs cluster never passed the readiness probe, so the job ended with LogClusterNotReady: the endpoint never became ready before comparing anything #126200 (mine, open)
PR praktika rollup of the row above #126200 (the owner of the row above)

The new 05454_local_progress_from_pipeline_threads passed in every stateless flaky check build (50 runs each, including TSan), and CH Inc sync is green. The earlier Config Workflow failures were the attempts before the can be tested label.

Session id: cron:our-pr-ci-monitor:20261028-180006

Merged via the queue into ClickHouse:master with commit d586d92 Nov 3, 2026
527 of 531 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocker This issue / pr blocks a new release can be tested Allows running workflows for external contributors comp-local clickhouse-local / embedded modes (incl. local file processing). pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants