Repository navigation
Reduce mutex contention in PipelineExecutor::allocateCPU to mitigate TSan crash in 02124_buffer_with_type_map_long - #101060
Closed
tuanpach wants to merge 4 commits into
Conversation
…CPU resource names per query `PipelineExecutor::allocateCPU` called `getMasterThreadResourceName()` and `getWorkerThreadResourceName()` as two separate operations, each acquiring and releasing the global `WorkloadEntityStorageBase::mutex`. Under TSan with 10 concurrent connections (as in `02124_buffer_with_type_map_long`), the high lock/unlock rate of this global mutex triggered a TSan-internal bug in `TraceSkipGap`: when TSan's slot-recycling sets `trace_pos = 0` transiently during a thread epoch change, a concurrent `TraceAcquire` call dereferences `thr->tctx->trace` with `tctx == nullptr`, causing a near-NULL read segfault. The fix has two parts: 1. Add `getCPUThreadResourceNames()` to `IWorkloadEntityStorage` and `WorkloadEntityStorageBase` to fetch both resource names under a single `mutex` acquisition instead of two. 2. Cache the result in `Context` as `cpu_thread_resource_names` (same pattern as `ClassifierPtr classifier` / `getWorkloadClassifier()`). CPU resource names are semantically fixed for the lifetime of a query — the workload cannot change after query start — so caching is correct. `PipelineExecutor` now calls `query_context->getCPUThreadResourceNames()` once per pipeline execution; subsequent pipelines within the same query hit the cache path and never touch `WorkloadEntityStorageBase::mutex` again. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Contributor
|
Workflow [PR], commit [c7d4369] Summary: ❌
AI ReviewSummaryThis PR reduces lock contention in CPU resource lookup by introducing ClickHouse Rules
Final VerdictStatus: ✅ Approve |
…rge` / `makeQueryContextForMutate` When a context is copied and repurposed for a merge or mutate operation, `classifier` was reset so a new one would be acquired for the correct workload. However, `cpu_thread_resource_names` was not reset, so if it had been populated before the copy it would remain stale — pairing the new classifier with CPU resource names from the wrong workload. Fix: reset `cpu_thread_resource_names` alongside `classifier` in both `makeQueryContextForMerge` and `makeQueryContextForMutate`, so both caches share identical lifetime semantics and are repopulated together on next use. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Contributor
LLVM Coverage Report
Changed lines: 100.00% (39/39) · Uncovered code |
Member
Author
|
02124_buffer_with_type_map_long by caching CPU resource names per query
Member
Author
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PipelineExecutor::allocateCPUcalledgetMasterThreadResourceName()andgetWorkerThreadResourceName()as two separate operations, each acquiring and releasingWorkloadEntityStorageBase::mutex. Under TSan with 10 concurrent connections (as in02124_buffer_with_type_map_long), this contributed to a high mutex lock/unlock rate that reproducibly triggered a crash inside TSan'sTraceEvent<EventUnlock>(tsan_rtl.h:745).Two distinct crash stack traces were observed, both at the same TSan location:
WorkloadEntityStorageBase::getWorkerThreadResourceName→PipelineExecutor::allocateCPU→pthread_mutex_unlockConcurrentBoundedQueue::popImpl→condition_variable::wait_for→pthread_cond_timedwait(internal mutex release) →TCPHandler::processOrdinaryQueryThe second trace has no relation to
WorkloadEntityStorageBase::mutex, which confirms that the root cause is a TSan internal bug:thr->tctxbecomes null for a live, running thread under certain conditions, andTraceAcquire(inlined intoTraceEvent) dereferences it without a null guard. The crash can be triggered by any mutex unlock when TSan's internal state is in this condition.This PR is a partial mitigation. The fix has two parts:
Add
getCPUThreadResourceNames()toIWorkloadEntityStorageandWorkloadEntityStorageBaseto fetch both resource names under a singlemutexacquisition instead of two.Cache the result in
Contextascpu_thread_resource_names(same pattern asClassifierPtr classifier/getWorkloadClassifier()). CPU resource names are semantically fixed for the lifetime of a query — the workload cannot change after query start — so caching is correct.PipelineExecutornow callsquery_context->getCPUThreadResourceNames()once per pipeline execution; subsequent pipelines within the same query hit the cache and never touchWorkloadEntityStorageBase::mutexagain.This reduces the rate of mutex events from the hot executor path, making the flaky test reproducibly pass, but does not eliminate the underlying TSan bug.
Crash stack traces:
https://pastila.clickhouse.com/?00a7c7eb/06f31ac959a0d293bf9ce4e3c190b405#Jf+fDAZ0EpLylxyoLMLGzw==GCM
Changelog category (leave one):
Documentation entry for user-facing changes