Skip to content

Backport the runtime TSan suppression for NonblockingBoundedQueue to 26.7 - #112916

Merged
clickhouse-gh[bot] merged 1 commit into
ClickHouse:26.7from
groeneai:backport-112695-tsan-suppression-26.7
Aug 1, 2026
Merged

clickhouse-gh[bot] merged 1 commit into
ClickHouse:26.7from
groeneai:backport-112695-tsan-suppression-26.7

Conversation

@groeneai

@groeneai groeneai commented Aug 1, 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):

...

Related: #112695
Related: #112722

Description

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. Example on a 26.5 backport branch: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112722&sha=21ba2dbcf7a5d9e1a4f1e1357677f5429f121040&name_0=BackportPR&name_1=Integration%20tests%20%28amd_tsan%2C%203%2F6%29

master fixed this in #112695 with two anchored tryPush patterns in __tsan_default_suppressions. A function attribute cannot suppress the reports: the access happens in the element type's implicitly generated move assignment, a separate function that stays instrumented, which the attribute also keeps from being inlined. A runtime suppression is consulted for every frame.

I authored this by hand because the cherry-pick does not apply and the label route is unavailable: #112695 carries no *-must-backport label and I cannot apply one. @alexey-milovidov asked for it in #112722; 26.5 is #112899.

The hook goes beside the existing __tsan_default_options in base/base/sanitizer_options.h, master's placement (26.5 lacks that header, so #112899 used programs/main.cpp). Purely additive: #107083, whose attributes #112695 removed as ineffective, was never backported here.

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 through that frame.

Sanitizer builds only, no performance impact: outside THREAD_SANITIZER the hook is preprocessed away, and inside it returns a fixed string read once at sanitizer init. The queue header change is a comment. No new test, matching #112695: the only end-to-end oracle is the TSan shards going quiet over many runs. The comment says TSan rather than master's TSAN, per .claude/CLAUDE.md; happy to unify across the three branches.

Why 26.7 is affected although the signature has not been observed on it

Over the last 45 days the signature appears four times on master, plus once on a robot backport branch targeting 26.6 and once on one targeting 26.5. It never appears with 26.5, 26.6 or 26.7 as the head ref, so the zero on 26.7 is not distinctive. Worth flagging for anyone re-running this: two of the master occurrences escape the obvious query, because the TSan banner overwrites the instance name and the harness line reads Sanitizer assert found for instance ================== rather than naming a zoo container.

That zero is sample size, not immunity. At master's observed rate the probability of seeing zero occurrences is about 84 percent on 26.7's TSan integration build volume, and about 64 percent on each of 26.5's and 26.6's. So no release branch can be shown immune from this data, and I am not claiming the data shows 26.7 susceptible either.

The case for 26.7 is structural. Both carrier files are present, src/Common/NonblockingBoundedQueue.h and src/Coordination/KeeperRequestDispatcher.cpp, with both explicit instantiations. Its copy of the queue header is md5-identical to 26.6's, which did see the failure. 26.4 and older lack the files entirely and are immune for that reason.

One detail on scope: 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 anchored pattern. Verified in the built binary, where each pattern names exactly one live symbol and that one names none.

…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 ClickHouse#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. ClickHouse#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 (ClickHouse#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 ClickHouse#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]>
@groeneai

groeneai commented Aug 1, 2026

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

Two independent passes over this change: my own cold review of the resulting code
(carriers enumerated from scratch, before reading the implementation notes), then a
second-model gate review against the frozen description above. Verdicts and evidence below.

Gate review: 0 findings. Its stated coverage: both touched files read in full, all queue
instantiations traced, plus the sanitizer runtime matching path, executable linkage, the CI
failure stack, the TSan build and runtime startup.

My own review: 3 findings, 1 accepted and fixed, 2 rejected with evidence.

⚠️ The occurrence count and the derived probabilities were wrong, and are corrected above.
My first pass suspected the opposite problem, that the three master rows matching
Sanitizer assert found for instance zoo% were not attributable to this race at all. I
refuted that myself by downloading each shard's logs.tar.gz and grepping the Keeper
container's docker.log: all three carry the demangled tryPop/tryPush pair verbatim. The
same sweep, however, surfaced a fourth master occurrence that the zoo% filter structurally
cannot see, because the TSan banner overwrites the instance name and the harness line reads
Sanitizer assert found for instance ==================. With the correct numerator of four
the figures are about 84 and 64 percent, not 88 and 72. The direction of the argument is
unchanged, a zero on 26.7 is still the expected outcome, but a reviewer re-running the query
would have landed on different numbers than the ones I had written, and that paragraph is the
whole justification for including 26.7. Fixed before publishing, and the pitfall is now
documented in the description.

💡 A successfully suppressed race still makes the runtime print
ThreadSanitizer: Matched N suppressions at finalize, because print_suppressions defaults
to true. Rejected as a finding against this change, on three grounds. It cannot affect the
job this fixes: the integration harness keys on ==================, which is printed inside
PrintReport, and a suppressed report returns from OutputReport before reaching it. It is
byte-identical to what is already merged on master, so diverging here would itself be the
defect in a parity backport. And it has no empirical support: zero CIDB rows in 45 days match
that string. Recording it because the stateless job's own log check is broader than the
integration one, so it is worth someone's awareness even though the alternative, an
unsuppressed report that also aborts, is strictly worse.

💡 programs/keeper/keeper_main.cpp includes no sanitizer options header, so a
BUILD_STANDALONE_KEEPER=1 binary would carry neither this hook nor the pre-existing
__tsan_default_options. Rejected as out of scope. The TSan build lines in
ci/jobs/build_clickhouse.py do not set that option, so clickhouse-keeper is a symlink to
clickhouse and the harness launches clickhouse keeper, the binary that does carry the
hook. The same include is absent on master and the gap predates this change.

For completeness, what I checked and found correct rather than reporting: the carrier set
(three element types on this branch, two covered by design and keeper-bench's private nested
type deliberately not), the per-frame suppression matching that makes one tryPush entry cover
the nested implicit move assignment, the prefix semantics of ^ in TemplateMatch, the fact
that a suppressed report emits no banner and does not Die, and that no executable line of the
queue changed.

Session id: cron:clickhouse-review-slot-48:20260801-140200

@groeneai

groeneai commented Aug 1, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? N/A for the race itself: it is a TSan false positive with no on-demand trigger. Substituted with a deterministic property of the artifact, reproducible on demand: the __tsan_default_suppressions symbol type goes from weak to strong, and poisoning the suppression category makes the runtime abort at init.
b Root cause explained? Yes. The release/acquire handoff on slot.pos orders every slot payload access, so the report is spurious. NO_SANITIZE_THREAD cannot suppress it: the reported access is in the element type's implicitly generated move assignment, a separate function that stays instrumented and that the attribute also keeps from being inlined. A runtime suppression is consulted for every frame of a report.
c Fix matches root cause? Yes. The suppression is attached to the sanitizer runtime, per instantiation, at the frame the report names, which is the only layer that reaches a nested implicit function. Nothing is widened, disabled or reduced. Rejected alternatives: re-adding the attribute #112695 removed as ineffective; a TSAN_OPTIONS=suppressions= file, for which no wiring exists on this branch; a blanket race:^NonblockingBoundedQueue pattern, which would be broader than the false positive.
d Test intent preserved / new tests added? No new test, matching #112695 and the 26.5 sibling #112899. No existing test is modified or weakened, and the queue header change is a comment. The only end-to-end oracle is the TSan integration shards going quiet over many runs, which is a CI-side property.
e Both directions demonstrated? Yes, on Build-ID-verified binaries. Without the change the binary carries only compiler-rt's weak fallback symbol and no suppressions; with it there is exactly one strong definition and the runtime parses the payload at init. Poisoning the payload category yields ThreadSanitizer: failed to parse suppressions. and exit 134, and reverting returns the Build ID bit-identically.
f Fix is general across code paths? Yes. Every user of the queue on this branch was enumerated and classified: both Keeper instantiations are covered; keeper-bench's element type is a private nested struct, so it is deliberately unmatched, which was confirmed by measurement in the binary rather than by reading; Changelog.cpp uses a different class; src/Loggers/ has no uses. tryPop is excluded with a consumer-count-independent argument, so the entry set is the minimum of two.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A: there is no input, type-wrapper or settings surface. The change adds one function returning a fixed string, read once at sanitizer init, plus a comment. The equivalent of an input matrix here is the instantiation set, which is enumerated exhaustively and measured in the built binary.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, no default change, no serialization or format change, so no SettingsChangesHistory.cpp entry applies. Additive on this branch, since #107083 is absent and nothing is removed. Sanitizer builds only: the non-sanitizer build carries zero hook symbols and zero payload strings, measured.
i Invariants and contracts preserved? Yes. No executable line of NonblockingBoundedQueue.h changed, so the Vyukov protocol, the release/acquire handoff and the move-out ownership rule are bit-for-bit intact. The header's own contract, that these hooks are defined exactly once per binary with external C linkage, is preserved: the definition sits in the file's single extern "C" block under the existing #ifdef THREAD_SANITIZER, no second guard was added, and both build arms link. The file's existing -Wreserved-identifier and -Wmissing-prototypes pragma push already covers the new name. The one contract deliberately narrowed is TSan's reporting surface, minimally: each race: entry also hides heap-use-after-free through the frame it names, which is why the set is two anchored entries rather than a wildcard.

Session id: cron:clickhouse-impl-slot-41:20260801-130600

@groeneai

groeneai commented Aug 1, 2026

Copy link
Copy Markdown
Collaborator Author

cc @thevar1able @antonio2368, could you review this? It is the 26.7 backport of the already-merged #112695: a __tsan_default_suppressions hook in base/base/sanitizer_options.h with two ^-anchored patterns for the two Keeper queue instantiations, plus a comment in the queue header. Purely additive here, since #107083's attributes were never backported to this branch, and the anchoring keeps it off the keeper-bench queues in the same binary.

@PedroTadim PedroTadim added the can be tested Allows running workflows for external contributors label Aug 1, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [BackportPR], commit [4e23789]

Summary: ✅

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants