[clang-tools-extra] [NFC] Avoid data race condition. (PR #118664)

Tom Honermann via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 5 14:04:58 PST 2024


================
@@ -501,6 +501,7 @@ class PreambleThread {
       }
 
       {
+        std::unique_lock<std::mutex> Lock(Mutex);
         WithContext Guard(std::move(CurrentReq->Ctx));
----------------
tahonermann wrote:

Unfortunately, I don't think this solves the issue by itself. Just prior to exiting the above block, we are assured that `CurrentReq` has been assigned and thus holds an object. At this point, the lock on `Mutex` has been released which means it is possible that some other thread executing this same function might acquire the lock on `Mutex` and reset `CurrentReq` at line 517/518 below before this code is reached. The possibility still exists then that `CurrentReq` doesn't hold an object at this point (or holds a partially constructed/destructed object; note that `std::optional` doesn't promise atomic operations).

I'm not sure what the right fix is for this issue at this point. I think it *might* be to move `*NextReq` above into a local `Request` variable and change the `CurrentReq` data member to just be a `bool` that indicates whether a request is being processed. That would avoid any possibility of concurrent modification of the `Request` object while still preserving the ability to check if a request is being processed in `blockUntilIdle()`.

https://github.com/llvm/llvm-project/pull/118664


More information about the cfe-commits mailing list