From a3e64dd657b148fd0eefd5c16cd718e4ec6b06fa Mon Sep 17 00:00:00 2001 From: Matthew Glazar Date: Sun, 24 Mar 2024 22:16:02 -0400 Subject: [PATCH] fix(io): fix data race on Windows Background_Thread_Pipe_Writer's destructor has a data race. It writes to the stop_ member variable without acquiring the lock, but the background thread might be reading the variable concurrently (with the lock held). Fix the data race by guarding access to stop_ with a lock. --- src/quick-lint-js/io/pipe-writer.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/quick-lint-js/io/pipe-writer.cpp b/src/quick-lint-js/io/pipe-writer.cpp index 4a0c93b726..ddfdf0898f 100644 --- a/src/quick-lint-js/io/pipe-writer.cpp +++ b/src/quick-lint-js/io/pipe-writer.cpp @@ -35,8 +35,11 @@ Background_Thread_Pipe_Writer::Background_Thread_Pipe_Writer( } Background_Thread_Pipe_Writer::~Background_Thread_Pipe_Writer() { - this->stop_ = true; - this->data_is_pending_.notify_one(); + { + std::unique_lock lock(this->mutex_); + this->stop_ = true; + this->data_is_pending_.notify_one(); + } this->flushing_thread_.join(); }