Repository navigation
Fix the ineffective TSAN suppression in NonblockingBoundedQueue - #112695
alexey-milovidov merged 2 commits into
Conversation
`Integration tests (amd_tsan, N/6)` fails in teardown with `Sanitizer assert found for instance zoo<N>`, from a TSAN data race between the `slot.value` write in `NonblockingBoundedQueue::tryPush` and the read in `tryPop`. PR ClickHouse#107083 added `NO_SANITIZE_THREAD` to both methods to silence it. That fix is merged and an ancestor of the failing branch, and the race still fires: 29 CIDB rows over 45 days across 27 distinct pull requests, 3 of them on master, and 4 after the merge timestamp. The correctness analysis in ClickHouse#107083 is right. The release/acquire handoff on `slot.pos` orders every access to a slot payload, and there is no non-protocol payload access anywhere in the class, so the report is a false positive. Only the chosen mechanism is ineffective: * `NO_SANITIZE_THREAD` suppresses instrumentation of plain accesses in the annotated function's own body only. Neither `KeeperRequestForSession` nor `KeeperResponseForSession` declares an `operator=`, so the racing access happens in the implicitly generated move assignment, a separate function that keeps `sanitize_thread`. * The attribute also causes the frame split that defeats it, because an un-attributed callee can never be inlined into an attributed caller. Measured at IR level on the real header: with the attribute, `tryPush` and `tryPop` carry 0 plain-access hooks but each still calls the element move out of line, and that move body carries 10 hooks including the `__tsan_memcpy` that the CI report names. Register a `__tsan_default_suppressions` hook instead, with two anchored patterns for the two Keeper instantiations, and remove the two attributes. Suppressions are consulted for every frame of a report, so one entry covers the whole nested move chain, which is what no compile-time mechanism achieves. Removing the attributes additionally restores plain-access instrumentation in the queue for all four element types, including the logging and keeper-bench queues that were being silenced for no reason, while atomics stay instrumented so the happens-before edges remain visible to TSAN. Only `tryPush` is listed. A concurrent pop cannot race on a payload, because `dequeue_pos` gives exactly one consumer ownership of a slot before the move-out; this was measured, not assumed. Each `race:` entry also hides heap-use-after-free reports through the frame it names, so the set is kept at the proven minimum of two. The TSAN-internal path that produces the bogus shadow is not identified. Shadow-cell eviction is refuted, since it is reached only after the scan finds no race and can therefore only cause false negatives. The signature is worth reporting upstream, and the suppression is scoped narrowly enough to drop if that lands. Non-sanitizer builds are unaffected: the hook is inside `#ifdef THREAD_SANITIZER`, and removing the attributes is codegen-neutral without a sanitizer (same translation unit compiled with and without the attribute yields byte-identical objects when built without `-g`, and byte-identical `.text` with `-g`).
Review round 1, comment only. The executable change is unmodified: the two anchored `tryPush` suppression entries and the removal of the two `NO_SANITIZE_THREAD` attributes are byte-identical to the previous commit. The comment justified listing only `tryPush` by saying each Keeper queue has a single consumer, and then warned that giving one a second consumer would make a pop/pop report possible and require a `tryPop` entry. That warning is wrong, and the justification is weaker than what the code guarantees. A pop/pop payload race is impossible for any number of consumers. In `tryPop` the `dequeue_pos` compare-exchange admits exactly one consumer per position and only the winner reaches the move-out of `slot.value`. Two consumers can target one slot index only when their positions differ by the capacity, and the later one cannot pass the emptiness check until the earlier one publishes the slot, which happens after its move-out. Independently, ThreadSanitizer requires two distinct thread ids to report a race, so a single consumer cannot race itself. The comment now states that reason and drops the refuted warning. Adding a `tryPop` entry would be a dead entry that also hides heap-use-after-free reports through that frame. Co-Authored-By: Claude Fable 5 <[email protected]>
Internal second-model review (2 rounds, 11 findings adjudicated, 0 open blockers)Before opening this I ran an independent cold review plus a separate second-model gate pass over Verdict totals. Round 1: 3 AGREE (2 fixed by me in the PR text, 1 bounced as a code change), ❌ Blocker raised and refuted: "the standalone Keeper binary never registers the suppressions"The gate observed that The hook cannot be missing from any standalone build, because no standalone build has a sanitizer. And the failing job cannot be running that binary anyway. With
❌ Bounced for a code change: the comment justified the entry set with a claim my own measurement refutesRound 1's substantive finding was in the in-diff comment, not the mechanism. It justified listing A pop/pop payload race is impossible for any consumer count. Since the comment lives in source and I do not edit source in this role, it went back as a fix ❌ Off-by-one in a published measurement, found by re-deriving itRound 2 caught a wrong number in this PR's own description. It said the out-of-line element-move The counting convention is not in doubt, because the three sibling figures in the same sentence are 💡 Reported by the implementation rather than silently changed, and left as is
The premise is true: the only 💡 What I verified independently, in case it saves a reviewer the same workThe delta of the second round is comment-only, so I re-derived the mechanism from the vendored
The one that matters most: Session id: cron:clickhouse-review-slot-9:20260731-020500 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-40:20260731-014200 |
|
cc @al13n321 @antonio2368, could you review this? Your correctness analysis in #107083 holds, but |
|
Workflow [PR], commit [69243dc] Summary: ✅
AI ReviewSummaryThis PR replaces the ineffective Findings
Final VerdictStatus: Minimum required action: make standalone |
| /// Those reports are suppressed at runtime, per instantiation, by | ||
| /// `__tsan_default_suppressions` in base/sanitizer_options.h. A `NO_SANITIZE_THREAD` attribute |
There was a problem hiding this comment.
This is only true for executables whose main translation unit includes base/sanitizer_options.h. BUILD_STANDALONE_KEEPER=1 still runs Keeper through programs/keeper/keeper_main.cpp, which does not include that header, and the integration harness prefers the standalone clickhouse-keeper binary whenever it exists (tests/integration/helpers/cluster.py:1384-1389). So a TSAN standalone Keeper build still hits the same false-positive with no suppression registered. If this is meant to be the general Keeper fix, the standalone executable needs the hook too, likely via a standalone-only TU so clickhouse still keeps exactly one definition.
There was a problem hiding this comment.
Ok, as we don't use the standalone Keeper build.
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 4/4 (100.00%) · Uncovered code |
…26.5 Every 26.5 pull request keeps failing the TSan integration shards with "Sanitizer assert found for instance zoo3-1", whose Keeper container log carries a ThreadSanitizer data race between the slot payload write in NonblockingBoundedQueue::tryPush and the read in tryPop. The release/acquire handoff on slot.pos orders every access to a slot payload, so the report is a false positive. master fixed this in #112695 by registering the two anchored tryPush patterns through __tsan_default_suppressions. That fix is not on 26.5, and it cannot be cherry-picked: base/base/sanitizer_options.h does not exist on this branch, so the hook goes beside the existing __tsan_default_options in programs/main.cpp, inside the same "#ifdef THREAD_SANITIZER" block. The change is purely additive here. #107083, which added the NO_SANITIZE_THREAD attributes that #112695 removed as ineffective, was never backported either, so there is no attribute to delete. A function attribute could not have suppressed these reports anyway: the reported access happens in the element type's implicitly generated move assignment, a separate function that stays instrumented, and the attribute additionally prevents it from being inlined into the annotated one. Only tryPush is listed. tryPop writes the payload too, but the dequeue_pos compare-exchange gives one consumer sole ownership of a slot before the move-out, so two pops never touch one payload concurrently, whatever the consumer count. A tryPop entry would be dead, and a "race:" entry also hides heap-use-after-free reports through the frame it names. Sanitizer builds only: outside THREAD_SANITIZER the hook is preprocessed away, and the queue header change is a comment. Co-Authored-By: Claude Fable 5 <[email protected]>
…26.7 The TSan integration shards abort in teardown with `Sanitizer assert found for instance zoo<N>`. The Keeper container log carries a data race between the slot payload write in `NonblockingBoundedQueue<DB::KeeperResponseForSession>::tryPush` and the read in `tryPop`. The release/acquire handoff on `slot.pos` orders every access to a slot payload, so the report is a false positive, but it aborts the Keeper process and reds the whole shard. master fixed this in #112695 by registering two anchored `tryPush` patterns through `__tsan_default_suppressions`. A function attribute cannot suppress these reports: the reported access happens in the element type's implicitly generated move assignment, a separate function that stays instrumented, and the attribute additionally prevents it from being inlined. A runtime suppression is consulted for every frame of a report, so one entry per instantiation covers the whole nested move chain. Authored by hand rather than cherry-picked: master's queue-header hunk does not apply here, because its context is the `NO_SANITIZE_THREAD` attribute this branch never had. #107083, which added those attributes, was never backported either, so this change is purely additive and there is nothing to remove. The hook goes in `base/base/sanitizer_options.h` beside the existing `__tsan_default_options`, which is master's placement; the 26.5 sibling (#112899) had to use `programs/main.cpp` because that header does not exist there. Only `tryPush` is listed. `tryPop` writes the payload too, but the `dequeue_pos` compare-exchange gives one consumer sole ownership of a slot before the move-out, so two pops never touch one payload concurrently, whatever the consumer count. A `tryPop` entry would be dead, and each `race:` entry also hides heap-use-after-free reports through the frame it names, so the set is kept at the proven minimum of two. Both patterns are `^`-anchored on the two Keeper instantiations and each names exactly one live symbol in the built binary. keeper-bench also instantiates the queue, but its element type is a private nested struct of `StorageRunner`, so it demangles as `NonblockingBoundedQueue<StorageRunner::QueueItem>` and matches neither pattern. The signature has not been observed on this branch, unlike 26.5 and 26.6, but that is sample size rather than immunity: at master's observed rate the probability of seeing zero occurrences is about 88 percent on this branch's TSan integration build volume, and about 72 percent on each of 26.5 and 26.6, which did see one. The reason to include 26.7 is structural. Both carrier files are present with both explicit instantiations, and its copy of the queue header is md5-identical to 26.6's. 26.4 and older lack the files entirely. Sanitizer builds only: outside `THREAD_SANITIZER` the hook is preprocessed away, verified as zero hook symbols and zero payload strings in a non-sanitizer build, and inside it is one `const char *` returning a fixed string read once at sanitizer init. The queue header change is a comment, so no executable line of the queue changed. No new test, matching #112695: the only end-to-end oracle is the TSan integration shards going quiet over many runs. Co-Authored-By: Claude Fable 5 <[email protected]>
…e` subtree On `amd_tsan` integration shards the Keeper container occasionally writes a ThreadSanitizer report for the request the dispatch thread just popped, which aborts the container. `helpers/cluster.py` then raises `Sanitizer assert found for instance zooN` during `cluster.shutdown()`, and whichever test happened to run last goes red; the victim is incidental. There is no real race: `KeeperTCPHandler::receiveRequest` writes every payload field of the request before publishing it with `putRequest`, and `getRequestBytesCost` on the consumer side only reads the vptr and those fields. The existing entry was added as `race_top:^DB::getRequestBytesCost`, which matches the innermost frame only. That covered the vptr read, attributed to the call site inside `getRequestBytesCost` - but suppressing it just moved the report one frame down, to the `path` read inside `Coordination::GetRequest::bytesSize`, which aborts the container all the same. Use `race:` so the entry covers the whole callee subtree; that subtree is only `bytesSize`, a read-only size computation. Validated against the real ThreadSanitizer runtime with a harness that reproduces both frame shapes (arms differ only in the string returned by `__tsan_default_suppressions`; `banner` counts runs printing the `==================` line the integration harness greps for): | suppression | vptr shape | `bytesSize` shape | |--------------------------------------------|------------|-------------------| | (empty) | 8/10 | 10/10 | | `race_top:^DB::getRequestBytesCost` | 8/10 | 9/10 | | `race:^DB::getRequestBytesCost` | 0/20 | 0/20 | With `race_top:` the surviving report is exactly the one master hit: the `path.size()` read inside `bytesSize`, one frame below `getRequestBytesCost`. Controls: a typo'd pattern parses but suppresses nothing (10/10 banner), a mangled type token makes the runtime die with `failed to parse suppressions`, and `print_suppressions=1` shows the new entry being hit. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=dddf61ccff5828bb6937cfe680874a04be35932f&name_0=MasterCI&name_1=Integration%20tests%20%28amd_tsan%2C%206%2F6%29 Related: ClickHouse#113552 Related: ClickHouse#112695 Co-Authored-By: Claude Fable 5 <[email protected]>
Backport of #112695 to 26.6, requested by alexey-milovidov on #112722. 26.6 still carries the defect and has no fix. `Integration tests (amd_tsan, N/6)` fails in teardown with `Sanitizer assert found for instance zoo<N>`, from a TSAN data race report between the `slot.value` write in `NonblockingBoundedQueue::tryPush` and the read in `tryPop`. The report is a false positive. The release/acquire handoff on `slot.pos` orders every access to a slot payload, and there is no non-protocol payload access anywhere in the class, so the code is isomorphic to Vyukov's original implementation. But `abort_on_error=1` in `__tsan_default_options` turns the report into an aborted Keeper node, so healthy runs go red. A `NO_SANITIZE_THREAD` attribute cannot suppress it. Neither `KeeperRequestForSession` nor `KeeperResponseForSession` declares an `operator=`, so the racing access happens in the implicitly generated move assignment, a separate function that keeps `sanitize_thread`. The attribute also prevents that un-attributed callee from being inlined into the attributed caller, which is what creates the frame split that defeats it. A runtime `__tsan_default_suppressions` entry is consulted for every frame of a report, so one entry covers the whole nested move chain. Register that hook with two anchored patterns for the two Keeper instantiations. Only `tryPush` is listed: the `dequeue_pos` compare-exchange gives one consumer sole ownership of a slot before the move-out in `tryPop`, so a pop/pop payload race cannot occur at any consumer count, and each `race:` entry also hides heap-use-after-free reports through the frame it names. Anchoring is load-bearing. Patterns are matched against each frame's function, file and module name, and `clickhouse-keeper-bench` is a symlink to the same binary, so an unqualified pattern would also silence its three unrelated `NonblockingBoundedQueue<QueueItem>` queues. Verified it does not. This is purely additive, unlike the master commit: #107083, which added the attributes master removes, was never backported to 26.6, so there is no attribute to strip here. A straight cherry-pick does not apply (`git apply --check` fails on both files); on 26.6 the hook goes in `base/base/sanitizer_options.h`, which is master's own placement, whereas the 26.5 sibling #112899 had to use `programs/main.cpp` because that header does not exist on 26.5. Non-sanitizer builds are unaffected: the hook is inside `#ifdef THREAD_SANITIZER` and the other change is a comment, so there is no codegen change and no performance impact. A suppression only affects whether TSAN prints or aborts on a report. No test, matching #112695. The only oracle is a CI-side absence property over many runs, which no functional test can assert. Co-Authored-By: Claude Fable 5 <[email protected]>
Related: #107083
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Description
Integration tests (amd_tsan, N/6)fails in teardown withSanitizer assert found for instance zoo<N>, from a TSAN data race between theslot.valuewrite inNonblockingBoundedQueue::tryPushand the read intryPop. #107083 addedNO_SANITIZE_THREADto both methods to silence it. That fix is merged and an ancestor of the failing branch, and the race still fires: 29 CIDB rows over 45 days across 27 pull requests, 4 of them after the merge.The correctness analysis in #107083 is right: the release/acquire handoff on
slot.posorders every payload access, so the report is a false positive. Only the chosen mechanism is ineffective.NO_SANITIZE_THREADcovers just the annotated function's own body, and neither element type declares anoperator=, so the racing access happens in the implicitly generated move assignment, a separate function that keepssanitize_thread. The attribute also causes the frame split that defeats it, since an un-attributed callee cannot inline into an attributed caller.This registers a
__tsan_default_suppressionshook with two anchored patterns for the two Keeper instantiations, and removes the two attributes. Suppression applies to the report rather than to a function body, so one entry covers the whole nested move chain. Removing the attributes also restores plain-access instrumentation in the queue for all four element types, including the logging andkeeper-benchqueues that were being silenced for no reason, while atomics stay instrumented so the happens-before edges remain visible.I have not identified the TSAN-internal path that produces the bogus shadow, so the suppression is scoped narrowly enough to drop if upstream fixes it. Only
tryPushis listed, becausedequeue_posgives one consumer sole ownership of a slot before the move-out, so a concurrent pop cannot race on a payload. Eachrace:entry also hides heap-use-after-free through the named frame, which is why the set is kept at two.Validation
A scratch harness drives the real queue header with the
slot.poshandoff weakened torelaxed, linked against the real shipped hook, so a genuine race exists through the queue frames. It reproduces the CI signature exactly (__tsan_memcpy-> elementoperator=->tryPop, read of size 8). The control arm omits the hook; every case below is confirmed non-vacuous there.req_pushpop(witnessed signature)req_pushpushresp_pushpopresp_pushpushreq_poppoplog_pushpop,log_poppop(logging queue)bench_pushpop(keeper-bench)size,byref,unrelatedSuppressed runs print
matched suppressionnaming the shipped pattern, so the demangled-name match is empirical.Why the attribute cannot work, measured on the real header. With it,
tryPushandtryPopcarry 0 plain-access hooks but each still calls the element move out of line, and that move body carries 11 hooks including__tsan_memcpy. Without it they carry 18 and 19 hooks and the move inlines; atomics are 8/8 in both arms.On the real TSAN binary both patterns match exactly one live symbol each, so neither is a dead entry, and mangling only the suppression type makes the runtime fail to parse and die at startup, proving the list is consumed rather than merely present.
No test is added or changed, and no integration job applies setting randomization.
Witnessed failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107074&sha=e42b9c4c1c5cda0dc694c4150a195295639e4ca3&name_0=PR&name_1=Integration%20tests%20%28amd_tsan%2C%204%2F6%29