Skip to content

common,global: fix data races reported by ThreadSanitizer - #73712

Open
Doracly wants to merge 4 commits into
ceph:mainfrom
Doracly:TSan-common
Open

Doracly wants to merge 4 commits into
ceph:mainfrom
Doracly:TSan-common

Conversation

@Doracly

@Doracly Doracly commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

These commits fix data races and one signal-safety bug that ThreadSanitizer reports in the unit tests.

Commit Reports Tests
mempool: initialize pool_index once in get_pool() 4,225 22
common/admin_socket: make m_shutdown atomic 7 1
common: start the service thread after its perf counters 180 9
global/signal_handler: use _exit() in reraise_fatal() 50,770 2

Each count drops to 0. The _exit() commit is independent of the others. Split from #72460.

Measured with GCC 15.2, Debug, -DWITH_TSAN=ON: ctest -j4 -R unittest_ -E unittest_erasure_code_shec_all with TSAN_OPTIONS=second_deadlock_stack=1:history_size=7:report_thread_leaks=0, against main as of 2026-10-05.

Contribution Guidelines

  • To sign and title your commits, please refer to Submitting Patches to Ceph.

  • If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.

  • When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

You must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.

get_pool() wrote table[ix].pool_index on every call. It is called for
every buffer::raw construction and destruction, so concurrent calls
race:

    WARNING: ThreadSanitizer: data race
      Write of size 4 by thread T130:
        #0 mempool::get_pool(mempool::pool_index_t) src/common/mempool.cc:86
        ceph#1 ceph::buffer::v15_2_0::raw::raw(char*, unsigned int, int)
      Previous write of size 4 by main thread:
        #0 mempool::get_pool(mempool::pool_index_t) src/common/mempool.cc:86
        ceph#1 ceph::buffer::v15_2_0::raw::~raw()

Set all pool_index values once, when the function-local static table is
constructed. The table was already initialized on first use, so this
adds no cost and still works from static constructors in other
translation units.

Under TSan (GCC 15.2, Debug), this removes 4,225 reports from 22 unit
tests.

Assisted-by: claude-opus-5-5
Signed-off-by: Dominik Raddatz <[email protected]>
shutdown() sets m_shutdown while the admin socket thread polls it in
entry():

    WARNING: ThreadSanitizer: data race
      Write of size 1 by main thread:
        #0 AdminSocket::shutdown() src/common/admin_socket.cc:1145
      Previous read of size 1 by thread T4:
        #0 AdminSocket::entry() src/common/admin_socket.cc:302
      Thread T4 'admin_socket' created by main thread

Relaxed ordering is enough: m_shutdown guards no other data, and
shutdown() joins the thread after waking it through the wakeup pipe.

Assisted-by: claude-opus-5-5
Signed-off-by: Dominik Raddatz <[email protected]>

@tchaikov tchaikov 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.

as an alternative, probably we can have something like

{
    std::lock_guard lg(_service_thread_lock);
    if (_service_thread) {
      return;
    }

    // create the counters before the thread, whose loop reads them
    if (!(get_init_flags() & CINIT_FLAG_NO_CCT_PERF_COUNTERS)) {
      _enable_perf_counter();
    }

    _service_thread = new CephContextServiceThread(this);
    _service_thread->create("service");
  }

?

Comment thread src/common/ceph_context.cc Resolved
Comment thread src/common/ceph_context.cc Resolved
start_service_thread() started the service thread before
_enable_perf_counter() had built the counters its loop reads:

    WARNING: ThreadSanitizer: data race
      Read of size 8 by thread T2:
        #0 ceph::common::PerfCounters::set(int, unsigned long) src/common/perf_counters.cc:239
        ceph#1 ceph::common::CephContext::_refresh_perf_values() src/common/ceph_context.cc:1081
      Previous write of size 8 by main thread:
        ceph#1 ceph::common::PerfCountersBuilder::PerfCountersBuilder(...)
        ceph#2 ceph::common::CephContext::_enable_perf_counter() src/common/ceph_context.cc:1012
      Thread T2 'service' created by main thread

Build the counters first, under _service_thread_lock and before
_service_thread is set, so no caller sees an unstarted thread.

Under TSan, this removes 180 reports from 9 unit tests.

Assisted-by: claude-opus-5-5
Signed-off-by: Dominik Raddatz <[email protected]>
If the re-raised signal does not terminate the process, as under TSan,
reraise_fatal() calls exit(), which runs atexit handlers and static
destructors inside the signal handler while other threads keep running:

    WARNING: ThreadSanitizer: signal-unsafe call inside of a signal
        ceph#7 Option::~Option() src/common/options.h:15
        ceph#14 __static_initialization_and_destruction_0 src/common/options.cc:341
        ceph#15 handle_oneshot_fatal_signal src/global/signal_handler.cc:372
        ceph#17 ceph::__ceph_assert_fail(...) src/common/assert.cc:102

Under TSan, this removes 50,770 reports from 2 unit tests.

Assisted-by: claude-opus-5-5
Signed-off-by: Dominik Raddatz <[email protected]>
@Doracly

Doracly commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor Author

as an alternative, probably we can have something like

{
    std::lock_guard lg(_service_thread_lock);
    if (_service_thread) {
      return;
    }

    // create the counters before the thread, whose loop reads them
    if (!(get_init_flags() & CINIT_FLAG_NO_CCT_PERF_COUNTERS)) {
      _enable_perf_counter();
    }

    _service_thread = new CephContextServiceThread(this);
    _service_thread->create("service");
  }

?

Thanks, adopted.
I was hesitant to move _enable_perf_counter() into the spinlock, but I don't expect any lock contention here.

@Doracly

Doracly commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test windows

@Doracly
Doracly requested a review from tchaikov November 16, 2026 10:31
@Doracly

Doracly commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

@tchaikov I pushed your start_service_thread suggestion on Nov 11; could you take another look?

@tchaikov

Copy link
Copy Markdown
Contributor

@Doracly will take a look shortly.

@tchaikov tchaikov 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.

lgtm

@Doracly

Doracly commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

@SrinivasaBharath could you add this to a RADOS batch? tchaikov approved it with needs-qa on 11-26.

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.

2 participants