Skip to content

annotate virtual mutex ownership transfer - #15752

Open
yuxuanchen1997 wants to merge 1 commit into
meta:mainfrom
yuxuanchen1997:export-D133381511
Open

yuxuanchen1997 wants to merge 1 commit into
meta:mainfrom
yuxuanchen1997:export-D133381511

Conversation

@yuxuanchen1997

Copy link
Copy Markdown
Member

Summary:
Clang thread-safety analysis cannot model ownership transferred through the Status-based transaction mutex interface: successful Lock/TryLockFor calls retain the mutex for a later UnLock, and TryLockFor can also report failure without acquiring it.

Exclude only the implementation bodies of those three virtual methods from that analysis. Keep locking, timeouts, Status results, object layout and runtime behavior unchanged; compilers without the Clang attribute are unaffected.

Differential Revision: D133381511

Summary:
Clang thread-safety analysis cannot model ownership transferred through the Status-based transaction mutex interface: successful Lock/TryLockFor calls retain the mutex for a later UnLock, and TryLockFor can also report failure without acquiring it.

Exclude only the implementation bodies of those three virtual methods from that analysis. Keep locking, timeouts, Status results, object layout and runtime behavior unchanged; compilers without the Clang attribute are unaffected.

Differential Revision: D133381511
@meta-cla meta-cla Bot added the CLA Signed label Nov 21, 2026
@meta-codesync

meta-codesync Bot commented Nov 21, 2026

Copy link
Copy Markdown

@yuxuanchen1997 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D133381511.

@github-actions

Copy link
Copy Markdown

✅ clang-tidy: No findings on changed lines

Completed in 7.1s.

@github-actions

Copy link
Copy Markdown

✅ Claude Code Review

Auto-triggered after CI passed — reviewing commit c1b1d4a


Summary

Clean, low-risk change that adds [[clang::no_thread_safety_analysis]] to three virtual methods whose ownership-transfer semantics cannot be modeled by Clang's static analyzer. No runtime behavior change.

High-severity findings (0):

No high-severity findings.

Full review (click to expand)

Findings

🟡 MEDIUM

M1. Consistency: TransactionDBCondVarImpl::Wait and WaitFor have the same modeling problem — utilities/transactions/transaction_db_mutex_impl.cc:92-130
  • Issue: Wait() and WaitFor() use std::adopt_lock / lock.release() to transfer mutex ownership into and out of a unique_lock without actually acquiring or releasing the underlying mutex. This is the same pattern Clang's thread-safety analysis cannot model. If thread-safety analysis is ever enabled on this TU (e.g., via GUARDED_BY annotations on mutex_), these methods will produce the same false positives that motivated annotating Lock/TryLockFor/UnLock.
  • Root cause: The PR annotates only the TransactionDBMutex methods but not the TransactionDBCondVar methods that also perform non-standard mutex ownership manipulation.
  • Suggested fix: Apply the same [[clang::no_thread_safety_analysis]] annotation to Wait and WaitFor for completeness, or document why they are intentionally excluded.

🟢 LOW / NIT

L1. Consider a portability macro — project-wide pattern
  • Issue: The #if defined(__clang__) / [[clang::no_thread_safety_analysis]] pattern is repeated three times. RocksDB typically defines portability macros for compiler-specific attributes (see e.g., CACHE_LINE_SIZE, LIKELY/UNLIKELY). If more suppressions are expected, a macro like ROCKSDB_NO_THREAD_SAFETY_ANALYSIS in a port/ header would reduce repetition and centralize the guard.
  • Suggested fix: Optional. For a single file with three uses this is acceptable as-is. Worth considering if thread-safety annotations expand to other files.
L2. Attribute placement on declaration vs. definition — transaction_db_mutex_impl.cc:23-27
  • Issue: The annotations are placed on the declarations inside the .cc-file-local class definition. The out-of-line definitions of Lock() and TryLockFor() at lines 63 and 68 do not repeat the attribute. Per Clang docs, no_thread_safety_analysis on the declaration is sufficient and this is correct, but it creates a minor readability gap — a reader of the definitions may not realize the suppression is active.
  • Suggested fix: No change needed. This is informational.

Cross-Component Analysis

Context Affected? Notes
WritePreparedTxnDB No Uses TransactionDBMutex interface but the change is annotation-only
ReadOnly DB No Transactions not used
MSVC / GCC builds No #if defined(__clang__) correctly guards; other compilers see nothing
Runtime behavior No Zero behavioral change — annotations are compile-time only

DB Mutex Analysis

This change does not affect DBImpl::mutex_. The TransactionDBMutex is an entirely separate mutex used for key-level locking in the transaction layer. No DB mutex critical sections are modified.

Positive Observations

  • Correct use of #if defined(__clang__) guard ensures zero impact on GCC, MSVC, and MinGW builds.
  • The attribute is applied at the right granularity — only the methods whose ownership semantics cannot be statically modeled.
  • No runtime behavior, locking semantics, or object layout changes.
  • The UnLock() body expansion from inline to braced form is a no-op change that only exists to attach the attribute cleanly.

ℹ️ About this response

Generated by Claude Code.
Review methodology: claude_md/ci_review_prompt.md

Limitations:

  • Claude may miss context from files not in the diff
  • Large PRs may be truncated
  • Always apply human judgment to AI suggestions

Commands:

  • /claude-review [context] — Request a code review
  • /claude-query <question> — Ask about the PR or codebase

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant