Skip to content

Commit 4bc3d6a

Browse files
authored
Merge pull request #85083 from KushalP/TaskQueueTest-fix-race
TaskQueue(Test): Add destructor to close FDs, preventing resource leak, fix race in TaskSignalHandling test
2 parents 781489f + 8f14263 commit 4bc3d6a

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/Basic/Unix/TaskQueue.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ public:
114114
"Env must either be empty or null-terminated!");
115115
}
116116

117+
~Task() {
118+
// Ensure pipes are closed when the task is destroyed
119+
if (Pipe >= 0) {
120+
close(Pipe);
121+
Pipe = -1;
122+
}
123+
if (ErrorPipe >= 0) {
124+
close(ErrorPipe);
125+
ErrorPipe = -1;
126+
}
127+
}
128+
117129
const char *getExecPath() const { return ExecPath; }
118130
ArrayRef<const char *> getArgs() const { return Args; }
119131
StringRef getOutput() const { return Output; }

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)