Repository navigation
Fix a data race on the active connection count of remote query connections - #131436
PedroTadim merged 1 commit into
Conversation
…tions With `async_socket_for_remote = 0`, `RemoteQueryExecutor::read` releases `was_cancelled_mutex` before `connections->receivePacket()` (since 58718f2 in ClickHouse#107888, which fixed a livelock on detach). When `LIMIT` closes the port, `RemoteSource::onUpdatePorts` calls `finish()` on another executor thread, and its drain loop reads `hasActiveConnections()` without a lock while the reader's `receivePacket()` decrements `active_connection_count` on `EndOfStream` under `cancel_mutex` only. TSan reports it as STID 4761-4859 (Stress test (amd_tsan)). The comment that justified the lock-free accessor ("sendCancel() does not change the state of the replicas") did not cover this writer. `HedgedConnections` has the same accessor and the same writer (`finishProcessReplica`). Make the top-level counter `std::atomic<size_t>` in both classes. This only makes the existing lock-free read defined: no value changes, because the counter just guards the drain loop and the next `receivePacket()` re-checks it under `cancel_mutex` and returns `EndOfStream` once the query is cancelled. `HedgedConnections::OffsetState::active_connection_count` stays a plain `size_t`: it is never read by `hasActiveConnections()`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding) plus my own cold review (1 finding, the same one).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261113-205100 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261113-190300 |
|
Workflow [PR], commit [c5ca9c7] Summary: ✅
AI ReviewSummaryMakes Validation includes the author's documented forced-interleaving TSan comparison: 10/10 race reports before the fix and 0/10 afterward across both implementations. No builds or tests were run during this review. Final VerdictStatus: ✅ Approve |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Slowest function optimization changes (ThinLTO)Median per-function time ratio to the master baseline:
Compile time of recompiled translation units9 translation units recompiled, 23 s compile time in total, 9 of them have a recent master baseline. |
|
@mstetsyuk this seems to have been introduced by #107888, I think this fix is right |
7074838
Related: #131213
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Description
Stress test (amd_tsan)reportedThreadSanitizer: data race (STID: 4761-4859): https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=131213&sha=84f0cdd8256b3471d86053f28bac4788b2bbcfde&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29With
async_socket_for_remote = 0,RemoteQueryExecutor::readreleaseswas_cancelled_mutexbeforeconnections->receivePacket()(since #107888). WhenLIMITcloses the port,RemoteSource::onUpdatePortscallsfinish()on another thread, and its drain loop readshasActiveConnections()with no lock while the reader'sreceivePacket()decrementsactive_connection_countonEndOfStreamundercancel_mutex. The comment onMultiplexedConnections::hasActiveConnections("sendCancel()does not change the state of the replicas") no longer covers that.HedgedConnectionshas the same unlocked accessor.The fix makes the top-level counter
std::atomic<size_t>in both classes. No value changes: the counter only guards the drain loop, andreceivePacket()re-checks it undercancel_mutex.No released version shows a user-visible effect (the query result is the same either way), hence not a bug fix.
Validation: with temporary local instrumentation that forces the interleaving (reader waits before the
EndOfStreamreceive untilfinishhas sent the cancel), a TSan build reports the CI pair 10/10 without the fix (5MultiplexedConnections, 5HedgedConnections) and 0/10 with it. The 36 stateless tests that useasync_socket_for_remote = 0oruse_hedged_requestsgive identical results with and without the fix over 5 randomized runs (3 that need CI-only setup fail on both). No test is added: a deterministic one would have to hold a pipeline thread insideRemoteSource::work, and the failpoint tests for this path were removed in #125005.Workflow [PR]
Sync PR [sync-upstream/pr/131436]
Version info
26.11.1.2420-master(included in26.11and later)