Skip to content

Commit 8c607f9

Browse files
facontidavideclaude
andcommitted
Fix TSAN: move mutex init to base class constructor
The mutex initialization must happen in FileLogger2PImplBase constructor, BEFORE StatusChangeLogger's constructor runs. StatusChangeLogger subscribes to callbacks during construction, which may fire and access the mutex. Having the mutex "used" by the main thread before any other thread accesses it establishes the happens-before relationship that TSAN requires. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 3799607 commit 8c607f9

1 file changed

Lines changed: 6 additions & 8 deletions

File tree

‎src/loggers/bt_file_logger_v2.cpp‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ struct detail::FileLogger2Private
3636
// Base class constructor - initializes _p before StatusChangeLogger
3737
detail::FileLogger2PImplBase::FileLogger2PImplBase()
3838
: _p(std::make_unique<FileLogger2Private>())
39-
{}
39+
{
40+
// Acquire mutex once to establish happens-before relationship for TSAN.
41+
// This must happen before StatusChangeLogger's constructor subscribes to
42+
// callbacks that may access the mutex from other threads.
43+
std::scoped_lock lock(_p->queue_mutex);
44+
}
4045

4146
// Destructor must be defined where FileLogger2Private is complete
4247
detail::FileLogger2PImplBase::~FileLogger2PImplBase() = default;
@@ -87,13 +92,6 @@ FileLogger2::FileLogger2(const BT::Tree& tree, std::filesystem::path const& file
8792
flatbuffers::WriteScalar(write_buffer.data(), timestamp_usec);
8893
_p->file_stream.write(write_buffer.data(), 8);
8994

90-
// Acquire mutex once before starting writer thread to establish
91-
// happens-before relationship for TSAN (mutex created in this thread,
92-
// first accessed in writer thread)
93-
{
94-
std::scoped_lock lock(_p->queue_mutex);
95-
}
96-
9795
_p->writer_thread = std::thread(&FileLogger2::writerLoop, this);
9896

9997
// Wait for writer thread to signal it's ready

0 commit comments

Comments
 (0)