Repository navigation
Conversation
`DataPartsAnyLock` is the token that 19 `MergeTreeData` members accept as the caller's proof that it holds `data_parts_mutex`. Both of its converting constructors discarded their argument, so the token proved only that a lock object existed in the caller's scope, never that the object still owned the mutex: a lock that had been released (`DataPartsSharedLock::unlock()` is public) or moved from satisfied it silently, and the callee then read `data_parts_by_state_and_info` / `data_parts_by_info` unsynchronised. Name the parameter in both constructors and assert that the lock still owns its mutex, reaching the private `std::unique_lock` / `std::shared_lock` member through a new `owns_lock()` accessor on each lock struct. The constructors are the only way a token can come into existence, so the check covers every token use: the class is final, has no default constructor, and both its copy and move constructors are deleted. This is a diagnostic only. `chassert` is active under `DEBUG_OR_SANITIZER_BUILD` and compiles to `(void)sizeof(!(x))` in official stable builds, so there is no user-visible behaviour change and no release codegen change. An audit of all 157 call sites of the lock-taking members found no public site that reaches the parts index with a token built from a lock it no longer holds, so this is not expected to fire on existing paths. The one place a parts lock is released while still in scope, `calculateColumnAndSecondaryIndexSizesLazily`, builds no token afterwards. Related: ClickHouse#119634 Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings), preceded by an independent
Severity: ❌ blocker / Two things worth a reviewer's time, both measured rather than argued:
Private code is not auditable from here, and I make no claim about it. If the assertion fires there, that Session id: cron:clickhouse-review-slot-48:20260914-214300 |
|
Workflow [PR], commit [12a0e3a] Summary: ❌
AI ReviewSummaryThis PR adds Findings
Final VerdictStatus: LLVM Coverage ReportMeasured on commit 12a0e3a.
Changed lines: Changed C/C++ lines covered: 4/6 (66.67%) · Uncovered code |
| DataPartsAnyLock(const DataPartsLock & lock) noexcept // NOLINT(google-explicit-constructor) | ||
| { | ||
| chassert(lock.owns_lock()); /// guards data_parts_indexes | ||
| } | ||
|
|
||
| DataPartsAnyLock(const DataPartsSharedLock & lock) noexcept // NOLINT(google-explicit-constructor) | ||
| { | ||
| chassert(lock.owns_lock()); /// guards data_parts_indexes |
There was a problem hiding this comment.
DataPartsAnyLock is still a detached proof token here: the new chassert only checks that the source lock owns data_parts_mutex while the token is being constructed. A caller can still write const DataPartsAnyLock & token = readLockParts(); and use token later; the DataPartsSharedLock temporary dies at the end of the first statement, so the later call reaches data_parts_indexes without any held lock even though these constructors already passed. The fix needs to keep the real lock object alive at the callee boundary (const DataPartsLock & / const DataPartsSharedLock & overloads, or another design that ties the token lifetime to the source lock), not just assert during conversion.
There was a problem hiding this comment.
The mechanism is real and I am not disputing it. I measured it first: against the published head, const DataPartsAnyLock & token = data.readLockParts(); compiles, and clang-22 (the CI pin) reports nothing about it under this file's own flag set.
No such site exists in src/, though. All 43 mentions of the type are its own definition plus 38 const DataPartsAnyLock & parameters (19 declarations over 14 members); there is no variable, data member, reference or return type of that type in the tree. To make that a compiler check rather than a grep, I put [[clang::lifetimebound]] on both converting constructor parameters and rebuilt, with clang-22 and each unit's own flags, the 24 translation units that reference any of those 14 members. All 24 stay clean. The only failure is a throwaway file holding your exact spelling, which compiles clean without the attribute:
error: temporary bound to local reference 'token' will be destroyed at the end of the full-expression [-Werror,-Wdangling]
14 | const DataPartsAnyLock & token = data.readLockParts();
So the lifetime axis is coverable in this PR's footprint by two attribute tokens rather than a lifetime redesign, and I will add it if a reviewer asks. I have not, for one reason I cannot measure from here: with -Weverything -Werror the attribute turns that spelling into a build error, and this token exists for the private tree (72020e8fb693a, "Accept DataPartsAnyLock for others getActiveContainingPart() (required for private)"), which I cannot compile. Typed const DataPartsLock & / const DataPartsSharedLock & overloads would reverse that commit, so that is this PR's reviewers' call, not something I push on my own.
This PR stays a debug and sanitizer diagnostic; the race it came from is closed by taking readLockParts() in the private getStatus.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units241 translation units recompiled, 2410 s compile time in total, 241 of them have a recent master baseline. |
CI finish ledger - 12a0e3aEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260915-030045 |
azat
left a comment
There was a problem hiding this comment.
This is useless, everything already checked at compile time, and this may create false-positive assumptions that it has some sense under it.
|
Understood, not reopening. One correction on the premise: the acquisition is guaranteed by construction, but the token's lifetime is not checked at compile time. If a compile-time check is ever wanted, |
|
clang::lifetimebound make sense, please open PR and assign me (and AFAICS we do not need private part for this) |
|
Acknowledged. I am opening a separate PR on master that puts |
|
Opened #121376 with Both directions measured on master, under clang-21 and the CI-pinned clang-22: |
DataPartsAnyLock is the proof token that 14 MergeTreeData members accept as the
caller's evidence that it holds data_parts_mutex. Both converting constructors
take the lock by const & and store nothing, which gives clang no reason to
connect the constructed token's lifetime to the lock's, so -Wdangling analysis
stops at the constructor boundary and this compiles with no diagnostic:
const DB::DataPartsAnyLock & token = data.readLockParts();
Guaranteed elision materialises the token directly (the deleted copy and move
constructors do not prevent it) and lifetime-extends it to the end of the
enclosing block, while the DataPartsSharedLock temporary it was built from is
destroyed at the semicolon. The token then certifies a lock that no longer
exists.
[[clang::lifetimebound]] on each constructor parameter declares the missing
connection, which turns that spelling into:
error: temporary bound to local reference 'token' will be destroyed at the
end of the full-expression [-Werror,-Wdangling]
No existing site has the dangling shape, so nothing else needed changing: every
use of the type outside its own definition is a const DataPartsAnyLock &
parameter. I measured both directions under clang-21 and the CI-pinned clang-22,
and the 24 src/ translation units that can construct a token stay clean under
both.
The two annotated constructors are the complete construction surface: the class
is final, deletes copy and move, and has no default constructor, factory or
friend. The attribute is diagnostic-only, with no effect on overload resolution,
mangling, codegen, ABI or runtime behaviour.
Leading attribute position is required because the parameters are unnamed. After
the reference the attribute appertains to the type rather than to the parameter
declaration, which clang rejects outright, and naming the parameters would need
[[maybe_unused]] under -Weverything -Werror.
Requested by @ azat in
ClickHouse#120093 (comment)
Co-Authored-By: Claude Opus 5 <[email protected]>
Related: #119634
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Debug and sanitizer builds now assert that a
DataPartsAnyLocktoken is constructed from a parts lock that still owns its mutex. No user-visible change.Description
DataPartsAnyLockis the token that 19MergeTreeDatamembers accept as the caller's proof that it holdsdata_parts_mutex. Both of its converting constructors discarded their argument:So the token proved only that a lock object existed in the caller's scope, never that it still owned the mutex. A released (
DataPartsSharedLock::unlock()is public) or moved-from lock satisfied it silently, and the callee then readdata_parts_by_state_and_info/data_parts_by_infounsynchronised.This names the parameter in both constructors and asserts the lock still owns its mutex, reaching the private
std::unique_lock/std::shared_lockthrough a newowns_lock()accessor. Those constructors are the only way a token can exist, since the class isfinal, has no default constructor and deletes copy and move, so the check covers every token use.chassertis active only underDEBUG_OR_SANITIZER_BUILD, so official stable builds are unaffected.@ alexey-milovidov approved this shape and asked for it separately, in #119634 (comment):
This is a diagnostic only, and it is deliberately not a fix for the race discussed there: that missing
readLockParts()acquisition is on the privategetStatusside.I audited all 157 call sites of the lock-taking members and found no public site reaching the parts index with a token built from a lock it no longer holds, so I do not expect it to fire on existing paths. The one place a parts lock is released while still in scope,
calculateColumnAndSecondaryIndexSizesLazily, builds no token afterwards; a breakpoint confirmed aSELECT count()over aJSON-column table enters that post-release branch with the assertion armed without aborting. I have not audited private code and make no claim about it.Demonstration that the assertion is not vacuous
Driven by a throwaway gtest (not committed). Both violations were asserted twice, on the message and on
KilledBySignal(SIGABRT).With the assertion in place, both violations abort and the positive controls do not:
As a mutation control, neutralising only the two
chassertlines and rebuilding stops the aborts, which is what shows the assertion and nothing else is responsible:The two violations are a released shared lock and a moved-from exclusive lock, which is the only way to make an exclusive lock non-owning since it has no public
unlock().Workflow [PR]
Sync PR [sync-upstream/pr/120093]