Skip to content

Annotate BRAVO locks for thread-safety analysis#13330

Merged
masaori335 merged 2 commits into
apache:masterfrom
masaori335:asf-master-clang-mutex
Jun 25, 2026
Merged

Annotate BRAVO locks for thread-safety analysis#13330
masaori335 merged 2 commits into
apache:masterfrom
masaori335:asf-master-clang-mutex

Conversation

@masaori335

@masaori335 masaori335 commented Jun 25, 2026

Copy link
Copy Markdown
Contributor
  1. Add annotations to suppress warnings with -Wthread-safety in BRAVO implementation.
  2. Cleanups

Enabling -Wthread-safety tree-wide surfaced findings in Bravo.h on
macOS, where libc++ marks the underlying std::shared_mutex as a
capability: the wrapper acquires and releases it across method
boundaries and on fast/slow-path branches the analyzer cannot follow.
Fedora CI uses libstdc++, which does not annotate std types, so it
never saw these.

Make ts::bravo::shared_mutex_impl a real capability so the contract is
checked on every Clang, not papered over on one std lib; its
lock-driving bodies are the trusted implementation and stay exempted,
matching the ts::shared_mutex pattern. Make the reader guard a rigid
scoped capability -- acquire in constructor, release in destructor,
no copy/move/defer/release -- which the analysis tracks with no
exemption; the movable std::shared_lock-style forms it replaced could
not be tracked, and no caller used them. Restructure the unit test's
try-lock asserts so the acquire gates a branch.
@masaori335 masaori335 added this to the 11.0.0 milestone Jun 25, 2026
@masaori335 masaori335 self-assigned this Jun 25, 2026
Copilot AI review requested due to automatic review settings June 25, 2026 04:46
@masaori335 masaori335 added Build work related to build configuration or environment Cleanup labels Jun 25, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the BRAVO reader/writer lock wrapper (ts::bravo::shared_mutex_impl) to participate cleanly in Clang’s -Wthread-safety analysis across different standard libraries (notably libc++ on macOS), by making the wrapper itself the tracked capability and using a rigid scoped reader guard that the analyzer can follow.

Changes:

  • Annotate ts::bravo::shared_mutex_impl as a thread-safety capability and mark its lock/unlock methods with acquire/release contracts (exempting their bodies from analysis where needed).
  • Replace the flexible/movable ts::bravo::shared_lock API with a strict acquire-in-ctor / release-in-dtor scoped capability to improve analyzability.
  • Restructure BRAVO unit test try-lock assertions so unlocks only occur on the success branch.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
include/tsutil/Bravo.h Adds Clang thread-safety capability annotations and refactors BRAVO’s shared guard into a rigid scoped capability.
src/tsutil/unit_tests/test_Bravo.cc Updates try-lock test structure to align with conditional-acquire modeling and avoid unconditional unlocks.

Comment thread include/tsutil/Bravo.h
Comment thread include/tsutil/Bravo.h
lock_shared/try_lock_shared assigned the Token only on the fast path,
so a reused non-zero Token surviving into a slow-path acquisition would
make unlock_shared release a reader slot instead of the underlying
mutex. Clear it on entry to enforce the documented 0-init contract.

@moonchen moonchen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding these annotations. Confirmed the token = 0 reset is correct and doesn't affect existing callers.

@masaori335 masaori335 merged commit 4fabae0 into apache:master Jun 25, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Build work related to build configuration or environment Cleanup

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants