Skip to content

Commit 3dcfdaf

Browse files
committed
TaskQueueTest: Fix race condition in TaskSignalHandling test
Replace `sleep` with `condition_variable` synchronization to ensure the child PID is captured before attempting to kill it. This eliminates timing dependencies.
1 parent 0e4dc2f commit 3dcfdaf

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

unittests/Basic/TaskQueueTest.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <algorithm>
2020
#include <chrono>
21+
#include <condition_variable>
2122
#include <mutex>
2223
#include <thread>
2324

@@ -93,9 +94,15 @@ TEST(TaskQueueTest, TaskSignalHandling) {
9394
bool TaskSignalled = false;
9495
int ReceivedSignal = 0;
9596
ProcessId ChildPid = 0;
97+
std::mutex PidMutex;
98+
std::condition_variable PidCv;
9699

97100
auto TaskBegan = [&](ProcessId Pid, void *Context) {
98-
ChildPid = Pid;
101+
{
102+
std::lock_guard<std::mutex> lock(PidMutex);
103+
ChildPid = Pid;
104+
}
105+
PidCv.notify_one();
99106
};
100107

101108
auto TaskSignalledCallback = [&](ProcessId Pid, llvm::StringRef ErrorMsg,
@@ -117,7 +124,11 @@ TEST(TaskQueueTest, TaskSignalHandling) {
117124
TQ.execute(TaskBegan, nullptr, TaskSignalledCallback);
118125
});
119126

120-
std::this_thread::sleep_for(std::chrono::milliseconds(100));
127+
// Wait for the task to actually start and get its PID
128+
{
129+
std::unique_lock<std::mutex> lock(PidMutex);
130+
PidCv.wait_for(lock, std::chrono::seconds(5), [&] { return ChildPid > 0; });
131+
}
121132

122133
if (ChildPid > 0) {
123134
EXPECT_EQ(0, kill(ChildPid, SIGTERM)) << "Should kill the specific child process we spawned";

0 commit comments

Comments
 (0)