Skip to content

Backport the NonblockingBoundedQueue TSAN suppression to 26.6 - #112912

Merged
PedroTadim merged 1 commit into
ClickHouse:26.6from
groeneai:backport-112695-tsan-suppression-26.6
Sep 4, 2026
Merged

PedroTadim merged 1 commit into
ClickHouse:26.6from
groeneai:backport-112695-tsan-suppression-26.6

Conversation

@groeneai

@groeneai groeneai commented Aug 1, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #112695
Related: #112722
Related: #112899

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

...

Description

Backport of #112695 to 26.6, requested on #112722. 26.6 still carries the defect.

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 the class has no non-protocol payload access. But abort_on_error=1 aborts the Keeper node, so healthy runs turn red.

A NO_SANITIZE_THREAD attribute cannot suppress it. Neither element type declares an operator=, so the racing access happens in the implicitly generated move assignment, a separate function that stays instrumented. A runtime __tsan_default_suppressions entry is consulted for every frame of a report, so one entry covers the whole nested move chain.

This registers that hook with two anchored patterns for the two Keeper instantiations. Only tryPush is listed: the dequeue_pos CAS gives one consumer sole ownership of a slot before tryPop's move-out, so a pop/pop payload race cannot occur at any consumer count, and a race: entry also hides heap-use-after-free through the frame it names. Anchoring keeps them off the unrelated keeper-bench queues in the same binary.

Verification (26.6, TSAN build, clang-21)

Built -DCMAKE_BUILD_TYPE=None -DSANITIZE=thread (the amd_tsan shape). Both directions, on the real binary:

nm -C T count W count shipped patterns in binary
without the hook 0 1 (compiler-rt weak fallback) 0
with the hook 1 0 2

The weak fallback is why a bare grep for the symbol is not a valid check. clickhouse-keeper is a symlink to the main.cpp-built binary (BUILD_STANDALONE_KEEPER is off in the TSAN builds), so the hook is linked into the Keeper process, and the runtime loads the strings without a parse error.

A standalone TSAN probe, taking the hook from the real header, confirms behavior on three arms: KeeperRequestForSession suppressed, KeeperResponseForSession suppressed, QueueItem (the keeper-bench shape) still reported. Mangling the category to bogusrace: makes TSAN answer "failed to parse suppressions", proving the runtime consumes the string rather than ignoring it.

Version info

  • Merged into: 26.6.5.48

Backport of ClickHouse#112695 to 26.6, requested by alexey-milovidov on ClickHouse#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: ClickHouse#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 ClickHouse#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 ClickHouse#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]>
@groeneai

groeneai commented Aug 1, 2026 •

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (independent reviewer + Gate B, 3 rounds, $30.20)

This change was reviewed by a reviewer independent of the author, then by a second model
(Gate B) run cold against the resulting code. Every finding and its verdict:

Finding Verdict
💡 The body said programs/main.cpp was "absent on 26.5". Backwards: main.cpp is on 26.5; the file missing there is base/base/sanitizer_options.h. AGREE, reworded. git cat-file -e origin/26.5:programs/main.cpp succeeds; the same probe on base/base/sanitizer_options.h fails.
💡 Related links were shorthand on one line, with two full URLs hidden inside a non-rendering HTML comment. AGREE, replaced with three visible Related: <full URL> lines, per .claude/CLAUDE.md:169 and the upstream template.
💡 @ alexey-milovidov renders as plain text; the gate asked to close the space. Partly declined. Closing it would notify him on a pull request that asks nothing of him, and the space-broken form is the deliberate convention for referencing someone without a ping. Resolved instead by dropping the handle: the attribution now reads "requested on #112722", with that thread linked as a full URL.

No blocker or major was raised in either review. The two nits and the reformulated third were
all in the pull request description; the code is unchanged from what the author committed.

What the independent review verified against the source, rather than accepting from the
comments:

  • The instantiation set was enumerated from scratch: exactly three on this branch
    (KeeperRequestForSession, KeeperResponseForSession, and keeper-bench's QueueItem).
    Two suppressed, one deliberately not.
  • The load-bearing claim is the omission of tryPop, so it was re-derived:
    dequeue_pos.compare_exchange_weak establishes sole ownership of a slot before the
    move-out, so no two consumers can write one payload at any consumer count. That is
    stronger than the "single consumer thread" wording that shipped on master, which would go
    stale if a consumer were added.
  • "Purely additive" was checked against the base tree: NO_SANITIZE_THREAD is absent from the
    26.6 queue header, so nothing is de-instrumented here.
  • Coverage loss was checked, not assumed: the hook is linked into unit_tests_dbms as well,
    but there is no unit test for this queue on this branch, so no deliberate-race oracle is
    silenced. Nothing in ci/ or tests/ sets suppressions= in TSAN_OPTIONS, so this hook
    cannot be shadowed by an env-provided file.
  • The affected job really runs here: release_branches.py selects the non-required
    integration jobs filtered to tsan, and BUILD_STANDALONE_KEEPER is off in the TSAN builds,
    so clickhouse-keeper is a symlink into the binary that carries the hook.

Gate B was run with an explicitly supplied 26.6 merge base, because its default base is
master; the scoped change map it reviewed is the intended 2 files / +30 lines. The pull
request description was corrected and then re-gated, so the final clean verdict
(findings = 0) was rendered against the exact text published here.

Session id: cron:clickhouse-review-slot-52:20260801-141502

@groeneai

groeneai commented Aug 1, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes, for the property this change is about. The CI race itself is ~0.19%/build and is a TSAN false positive, so it is not the repro target; the mechanism is. Deterministic on the real 26.6 TSAN binary: nm -C build-tsan/programs/clickhouse | grep -c ' T __tsan_default_suppressions' gives 0 without the change and 1 with it, and strings -a gives 0 vs 2 shipped patterns. A standalone TSAN probe that takes the hook from the real header reproduces a genuine race on demand and shows both Keeper instantiations suppressed while QueueItem still reports.
b Root cause explained? Yes. The element type declares no operator=, so the reported payload access lives in the implicitly generated move assignment, a separate function that keeps sanitize_thread; NO_SANITIZE_THREAD only covers plain accesses in the annotated function's own body, and additionally blocks inlining of that callee. abort_on_error=1 then turns the false-positive report into an aborted Keeper node and Sanitizer assert found for instance zoo<N>.
c Fix matches root cause? Yes. A runtime __tsan_default_suppressions entry is consulted for every frame of a report, which is exactly what a compile-time attribute cannot do for the nested move chain. Not a band-aid: nothing is widened, disabled, or resized, and no test is weakened.
d Test intent preserved / new tests added? No test, matching #112695 which shipped none. The only oracle is a CI-side absence property over many runs, which no functional test can assert; this is stated in the PR body. No existing test is touched or weakened (tests/ is untouched, so no 50/50 run is owed).
e Both directions demonstrated? Yes, on Build-ID-verified binaries. Without the hook: Build ID 8f5fe400…, T=0, W=1 (compiler-rt's weak fallback), 0 patterns. With it: Build ID 093a2928…, T=1, W=0, 2 patterns, runtime loads them with no parse error. Restoring the change returned the Build ID to exactly 093a2928…. Negative control: mangling the category to bogusrace: makes TSAN answer "failed to parse suppressions", proving the runtime consumes the string.
f Fix is general across code paths? Yes. Both concurrent Keeper payload writers are covered: NonblockingBoundedQueue<DB::KeeperRequestForSession>::tryPush and …<DB::KeeperResponseForSession>::tryPush (explicit instantiations at src/Coordination/KeeperRequestDispatcher.cpp:15-16). tryPop is deliberately excluded: the dequeue_pos CAS gives one consumer sole ownership of a slot before the move-out, so no pop/pop payload race exists at any consumer count, and an entry there would be dead and would hide heap-use-after-free through that frame.
g Fix generalizes across inputs? Not an input-shaped change (no types, parameters, or settings are involved). The relevant axis is instantiation, and it is enumerated: 2 Keeper instantiations covered, the 3 keeper-bench NonblockingBoundedQueue<QueueItem> queues deliberately not. That exclusion is verified, not assumed, and it matters because clickhouse-keeper-bench is a symlink to the same binary, so an unanchored pattern would silence them in the same process.
h Backward compatible? Yes, nothing at stake: no setting, no serialization format, no SettingsChangesHistory.cpp entry, no user-visible behavior. The whole hook is inside #ifdef THREAD_SANITIZER, so non-sanitizer builds see no codegen change and there is no performance impact; the second file's change is a comment.
i Invariants and contracts preserved? Yes. No control flow, memory ordering, or lifetime is altered; the queue's release/acquire protocol is untouched. The invariant this change maintains is "every concurrent payload writer of a Keeper queue slot is covered by a pattern, and no other instantiation is", which is enumerated in row f and verified on all three arms. The extern "C" single-definition contract of the __tsan_default_* hooks holds: T=1 and W=0 on the linked binary, so exactly one strong definition exists and it is ours rather than compiler-rt's weak fallback.

Session id: cron:clickhouse-impl-slot-45:20260801-132600

@groeneai

groeneai commented Aug 1, 2026

Copy link
Copy Markdown
Collaborator Author

cc @alesapin, could you review this? It is the 26.6 backport of the already-merged #112695: a runtime __tsan_default_suppressions hook with two anchored patterns for the Keeper queue instantiations, which is the only mechanism that covers the element type's implicitly generated move-assignment frame where the false-positive report actually lands.

@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 [4350474]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_asan_ubsan, db disk, old analyzer, 5/6) ERROR

@groeneai

groeneai commented Aug 1, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 4350474

Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Integration tests (amd_asan_ubsan, db disk, old analyzer, 5/6) runner-level host wedge, not a test failure: the job annotation is The self-hosted runner lost communication with the server (runner i-0181b30c0ee1e912f), the Run step never reported a conclusion, and CIDB has 0 FAIL/ERROR test rows for this commit. The sibling Integration tests (amd_tsan, 5/6) shard passed on the same commit. a fix task is moved to pending (investigating at full effort, fixing-PR link to follow on this PR)

The other 35 completed check-runs on this commit are green (Config Workflow and
Finish Workflow both succeeded); 2 are skipped by configuration.

Session id: cron:our-pr-ci-monitor:20260801-190047

@groeneai

groeneai commented Aug 2, 2026

Copy link
Copy Markdown
Collaborator Author

Fixing PR for the Integration tests (amd_asan_ubsan, db disk, old analyzer, 5/6) runner wedge
on this commit: #112984.

Root cause: the job runs a nested Docker daemon under --cgroupns=host, so the daemon parents
its containers at the host's /docker, a sibling of the job's own cgroup. The job's --memory
therefore bounds only the harness, while each test container defaults to 12g. Measured: with the
outer container capped at 1500 MiB, a resource-limited nested child wrote 1900 MiB past it while
the job's cgroup stayed at oom_kill 0.

@groeneai groeneai added the groeneai-origin-request PR origin: a maintainer pinged or directed groeneai label Aug 19, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-keeper ClickHouse Keeper (Raft-based coordination layer; ZooKeeper replacement) and ZooKeeper client lib... label Sep 4, 2026
@PedroTadim
PedroTadim merged commit 2d278e2 into ClickHouse:26.6 Sep 4, 2026
74 of 77 checks passed
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-keeper ClickHouse Keeper (Raft-based coordination layer; ZooKeeper replacement) and ZooKeeper client lib... groeneai-origin-request PR origin: a maintainer pinged or directed groeneai

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants