[clangd] Fix a race
authorKadir Cetinkaya <kadircet@google.com>
Wed, 24 Feb 2021 09:42:42 +0000 (10:42 +0100)
committerKadir Cetinkaya <kadircet@google.com>
Wed, 24 Feb 2021 11:15:16 +0000 (12:15 +0100)
Differential Revision: https://reviews.llvm.org/D97366

clang-tools-extra/clangd/unittests/ClangdLSPServerTests.cpp

index 4542f74..695ed89 100644 (file)
@@ -314,8 +314,14 @@ TEST_F(LSPTest, ModulesThreadingTest) {
     ~AsyncCounter() {
       // Verify shutdown sequence was performed.
       // Real modules would not do this, to be robust to no ClangdServer.
-      EXPECT_TRUE(ShouldStop) << "ClangdServer should request shutdown";
-      EXPECT_EQ(Queue.size(), 0u) << "ClangdServer should block until idle";
+      {
+        // We still need the lock here, as Queue might be empty when
+        // ClangdServer calls blockUntilIdle, but run() might not have returned
+        // yet.
+        std::lock_guard<std::mutex> Lock(Mu);
+        EXPECT_TRUE(ShouldStop) << "ClangdServer should request shutdown";
+        EXPECT_EQ(Queue.size(), 0u) << "ClangdServer should block until idle";
+      }
       Thread.join();
     }