Skip to content

Commit 3799607

Browse files
facontidavideclaude
andcommitted
Fix TSAN: acquire mutex before spawning writer thread
Establish a happens-before relationship by acquiring the mutex in the main thread before starting the writer thread. This satisfies TSAN's requirement that mutex creation and first cross-thread access are properly synchronized. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent a3f853a commit 3799607

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

src/loggers/bt_file_logger_v2.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ FileLogger2::FileLogger2(const BT::Tree& tree, std::filesystem::path const& file
8787
flatbuffers::WriteScalar(write_buffer.data(), timestamp_usec);
8888
_p->file_stream.write(write_buffer.data(), 8);
8989

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+
9097
_p->writer_thread = std::thread(&FileLogger2::writerLoop, this);
9198

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

0 commit comments

Comments
 (0)