Skip to content

Move two lightweight-update test rendezvous out of IProcessor::work() - #119069

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/move-lwu-test-rendezvous-out-of-work
Sep 12, 2026
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/move-lwu-test-rendezvous-out-of-work

Conversation

@groeneai

@groeneai groeneai commented Sep 9, 2026 •

Copy link
Copy Markdown
Collaborator

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):

Test-only change: two lightweight-update tests no longer park a pipeline worker thread inside IProcessor::work().

Description

Follows a review request on #110334 (#110334 (comment)): no test should park a PipelineExecutor worker inside IProcessor::work() (IProcessor.h: synchronous work must only use CPU), and to audit the tests I committed over the last four months. Part 2 of 2; part 1 is #119030.

Two were left. 04905_lwu_drop_race_table_lock parked in the ReplicatedMergeTree sink's commit path, reached from ExceptionKeepingTransform::work(). 03100_lwu_57_lock_acquire_timeout parked inside sleep() at infinite_sleep, before its bound check and before its sliced, cancellation-polling wait loop, so it held a worker unboundedly and un-cancellably.

Both want a rendezvous while a lightweight update's locks are held. Those locks are taken once while the pipeline is built (the table share lock into the pipeline's resources, the lightweight update lock into the patch sink) and released only by the teardown, so both are still held in BlockIO::onFinish, on the query thread, with no work() frame. One PAUSEABLE_ONCE failpoint goes there, after releaseQuerySlot().

Every query whose pipeline is completed reaches onFinish, including plain SELECTs (PullingAsyncPipelineExecutor completes the pipeline it runs), and failpoints are keyed by name only. So the pause is gated on a query-id prefix outside the fiu evaluation, as QueryMetricLog::finishQuery already is.

Two costs. 04905 no longer lands the DROP between the patch-part rename and the commit, only after it; since the locks are released only by that teardown, held there implies held throughout, so the assertion does not weaken, but the interleaving is gone. And this is a test hook in BlockIO, which is query plumbing; the alternatives are TCPHandler (native protocol only) and the end of CompletedPipelineExecutor::execute() (a step earlier, still holding the query slot). Either is a one-line move leaving both tests unchanged, so say the word if you prefer one.

Neither reference file changes. rmt_pause_before_commit_local_part had no other user and goes; infinite_sleep stays for 04095.


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

groeneai and others added 3 commits September 9, 2026 14:55
Tests that need a lightweight update to hold its locks open while a second
statement runs currently park a PipelineExecutor worker inside
IProcessor::work(), which the contract in IProcessor.h forbids: synchronous
work must only use CPU.

BlockIO::onFinish, between releaseQuerySlot() and the pipeline teardown, is the
last point at which those locks are still held: the table locks the interpreter
moved into the pipeline's resources, and a patch sink's lightweight update lock,
are both released by that teardown. A pause there parks the query thread once
the pipeline has finished, instead of a worker while it runs. It sits after
releaseQuerySlot() so it holds no concurrency slot, and before
releaseMemoryReservation() so it does not touch the reservation pipeline threads
still read.

Every query whose pipeline is completed reaches onFinish, including plain
SELECTs, since PullingAsyncPipelineExecutor completes the pipeline it runs, and
failpoints are keyed by name only. The pause is therefore gated on a dedicated
query-id prefix outside the fiu evaluation, the way QueryMetricLog::finishQuery
already is. Ungated, the one-shot arm could be consumed by an unrelated
pipeline, and a test could not tell "something parked" from "my query parked".

A pipeline that throws goes to onException rather than onFinish, so a failed
query never parks holding nothing.

Co-Authored-By: Claude Opus 5 <[email protected]>
The test used rmt_pause_before_commit_local_part, which sits in the
ReplicatedMergeTree sink's commit path and is therefore reached from
ExceptionKeepingTransform::work(), so the park occupied a PipelineExecutor
worker.

The share lock the test asserts on is taken by the interpreter, moved into the
pipeline's resources, and released only when the pipeline is torn down, so it is
still held once the pipeline has finished. Parking at
completed_pipeline_pause_before_teardown keeps the drop blocked in
lockExclusively on the query thread instead, and leaves all three arms, the
RWLockWritersWaitMilliseconds oracle and the recorded output unchanged.

The updater's query id carries the failpoint's prefix; the drop's deliberately
does not, so the drop can never be the query that parks.

Parking after the commit rather than inside it makes the assertion a superset of
the old one: held after the pipeline finished implies held throughout it. It also
drops the dependency on reaching the commit hook, which had already cost one CI
run where all three arms reported that the sink never reached it.

The pause was rmt_pause_before_commit_local_part's only user, so the failpoint
goes. The lambda's sleep_before_commit_local_part_in_replicated_table_ms
behaviour, which ATTACH PARTITION and restore also reach, stays.

Co-Authored-By: Claude Opus 5 <[email protected]>
The holder took the lightweight update lock and then parked inside sleep() at
the infinite_sleep failpoint, which FunctionSleep::execute reaches from
ExpressionTransform::work(). That site is before sleep()'s max_microseconds
bound check and before its sliced, cancellation-polling wait loop, so it parked
a worker unboundedly and un-cancellably.

The lock is taken while the pipeline is built and released only when the patch
sink is destroyed, so parking at completed_pipeline_pause_before_teardown holds
it just as well, on the query thread. The holder no longer needs sleep(0.001) in
its WHERE, and its 'auto' mode conflict set is unchanged: it still writes s and
reads id.

The holder's own update now commits while it is parked, which changes no
assertion: every reference line is a status or a counter, and 'auto' mode
conflict detection compares the in_progress nodes' affected-column sets rather
than committed data.

The property that made this test non-flaky is kept: the hold ends when the test
disables the failpoint, never after the runtime of real work. Added an armed
1 to 0 bracket around the park, which the query-id prefix upgrades from
"something parked" to "this holder parked". All 22 reference lines unchanged.

infinite_sleep stays in the tree: 04095_wait_fp_pause_disable.sh depends on it
having a real call site.

Co-Authored-By: Claude Opus 5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Sep 9, 2026
@groeneai

groeneai commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings) plus my own cold
pass over the resulting tree (3 findings, no blockers).

# Sev Finding Verdict Evidence / action
1 💡 04905's no-shared-merge-tree reason no longer names a mechanism: the old one cited the ReplicatedMergeTree sink failpoint that this PR deletes, and the share lock the oracle measures is taken in the interpreter with no engine dispatch AGREE, not blocking Tag is unchanged from before this PR and I kept it: SharedMergeTree is not in this tree, so nothing here can measure whether the substitution breaks the test, and dropping a tag on an unmeasurable arm would be an unmeasured change. Happy to sharpen the wording or drop the tag if you prefer either

Severity: ❌ blocker / ⚠️ major / 💡 nit. One nit is recorded internally only (shell hygiene, no
functional effect). The two costs of the relocation are stated in the PR description rather than here.

Session id: cron:clickhouse-review-slot-10:20260909-160400

@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [52b256e]

Summary: ✅


AI Review

Summary

This PR moves two lightweight-update test rendezvous out of IProcessor::work() by adding a query-thread pause in BlockIO::onFinish, gated by a dedicated query-id prefix, and rewires 03100_lwu_57_lock_acquire_timeout.sh and 04905_lwu_drop_race_table_lock.sh to use it. I found no correctness or coverage regressions in the current tree: the table share lock still lives in the pipeline resources until teardown, the patch sink still owns the lightweight update lock until destruction, and the current PR CI is fully green.

Final Verdict

✅ No findings.

LLVM Coverage Report

Measured on commit 52b256e.

Metric Baseline Current Δ
Lines 88.90% 88.90% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.30% 81.30% +0.00%

Changed lines: Changed C/C++ lines covered: 23/23 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Sep 9, 2026
@groeneai

groeneai commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator Author

cc @vdimir @scanhex12, could you review this? Two lightweight-update tests parked a PipelineExecutor worker inside IProcessor::work() to hold an update's locks open, which IProcessor.h forbids; both now use one PAUSEABLE_ONCE failpoint in BlockIO::onFinish, placed after releaseQuerySlot() and before the teardown that releases those locks, and gated on a query-id prefix so no unrelated completed pipeline can consume the one-shot arm. The description offers two alternative sites for the hook if either reads better to you.

@clickhouse-gh clickhouse-gh Bot added the comp-testing Test content only (no production code). label Sep 9, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 52b256e28 with master 4b3f269d1 (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

9 translation units recompiled, 22 s compile time in total, 9 of them have a recent master baseline.

Job report

@alexey-milovidov alexey-milovidov self-assigned this Sep 12, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 12, 2026
Merged via the queue into ClickHouse:master with commit b41474b Sep 12, 2026
174 of 175 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 12, 2026
alexey-milovidov added a commit to groeneai/ClickHouse that referenced this pull request Sep 16, 2026
…on-cancel

Conflicts in `FailPoint.cpp` and `RemoteQueryExecutor.cpp`: master (ClickHouse#119069, after the
review request on ClickHouse#110334 that no test may park a pipeline worker inside `IProcessor::work`)
removed the `remote_query_executor_receive_packet_pause` reader park and the
`in_receive_packet_window` flag that guarded this PR's `finish` and `cancel` holds, replacing
04512's fixture with the inline `remote_query_executor_cancel_and_drain_in_receive_window`.

Resolved by taking master's failpoint and dropping the reader park here too. The three holds
stay, guarded by a new executor-local pin instead: `remote_query_executor_finish_entry_hold`
fires once, and the executor that fires it sets `owns_test_holds`, so the drain hold and the
cancel-gate hold can only be consumed by that executor and not by the sibling shard. Every
`finish` entry of the pinned executor waits at the entry hold while it is armed, because
`work` and `onUpdatePorts` can enter `finish` on different threads and a second one running
ahead into the drain would defeat the interleaving.

`05076` no longer arms the reader park; scenario A pins through the entry hold and releases
it before waiting for the drain hold, scenario B is unchanged apart from the dropped park.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Sep 16, 2026
`RemoteQueryExecutor::finish` runs from `RemoteSource::work` (`LIMIT 1` closes the output port,
`prepare` returns `Finished`, `need_drain` is set) and also from `onUpdatePorts`. Both are
pipeline executor worker threads, so `finish_entry_hold` and `finish_drain_hold` broke the
contract in `src/Processors/IProcessor.h` on every run of this test rather than in a corner
case: `notifyPauseAndWaitForResume` waits on a condition variable with no timeout that only
another client statement can release, and scenario A's assertion depended on `KILL QUERY` not
releasing it. ClickHouse#119069 had already dropped the reader park here for the same reason.

The defect under test is a state, not an interleaving: `cancel` only has to run while a thread
is inside `finish` holding `was_cancelled_mutex`. So the three `PAUSEABLE_ONCE` holds and the
`owns_test_holds` pin are replaced by one `ONCE` failpoint that runs `cancel()` inline, on the
draining thread itself, in the shape 04512 already uses at the reader. Nothing is parked, so
the executor-local pin has nothing left to guard and the `std::atomic_bool` leaves the header.

That also removes the flake the pin introduced. The entry hold fired before
`++finish_in_progress` and pinned whichever executor entered `finish` first, with nothing
keeping that executor's drain outstanding, so any of the three conditions in
`if (!isQueryPending() || hasThrownException() || was_cancelled)` could become true under the
park, after which the drain hold was unreachable (1 failure in 126 runs at 8d973b2). The
predicate ClickHouse#119069 removed, `in_receive_packet_window`, could only be true while a reader was
blocked mid-receive, that is, only for a shard that provably still had packets to drain.

The fixture now carries that implication positively, through `distributed_push_down_limit=0`:
by default each shard applies the `LIMIT` itself and sends `EndOfStream` after a single granule
(measured 16384 rows read, against 200000 with the push-down off), so a `finish` that already
observed `finished` returned before reaching the drain and the failpoint never fired. That was
reproducible at 1 in 25 randomized runs before the pin.

Both directions measured on this content, debug build:
  fixed:   50/50 randomized runs pass (two batches, the second in `--order desc`), plus 25/25
           with `use_hedged_requests=1` forced. 1.44 s per run, against 11.3 s before.
  unfixed: with `cancel` reverted to its pre-PR body, FAIL at 61.5 s reporting `armed 1` and
           `consumed 0`, so the injection fired and the interleaving was established.
           `system.stack_trace` then shows the deadlock the gate prevents, including the
           production victim: `PullingAsyncPipelineExecutor::cancel` -> `ExecutingGraph::cancel`
           -> `RemoteSource::onCancel` -> `RemoteQueryExecutor::cancel` blocked on
           `was_cancelled_mutex` on the TCPHandler thread, behind the drain thread holding it.

The fix itself, `finish_gate_mutex` and `finish_in_progress`, is untouched.

Co-Authored-By: Claude Opus 5 <[email protected]>
pull Bot pushed a commit to erickirt/ClickHouse that referenced this pull request Sep 27, 2026
@clickhouse-gh clickhouse-gh Bot added comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). and removed comp-testing Test content only (no production code). labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). groeneai-origin-request PR origin: a maintainer pinged or directed groeneai 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