Skip to content

Commit 91c3736

Browse files
xunnanxufacebook-github-bot
authored andcommitted
fix test race condition
Summary: this would segfault if tests throw Reviewed By: bmaurer Differential Revision: D45723214 fbshipit-source-id: a2b433b8e66c4067b94e90a76675270385224a41
1 parent 648081c commit 91c3736

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

gloo/test/base_test.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <exception>
1414
#include <functional>
15+
#include <stdexcept>
1516
#include <thread>
1617
#include <vector>
1718

@@ -89,13 +90,15 @@ class BaseTest : public ::testing::Test {
8990
protected:
9091
void spawnThreads(int size, std::function<void(int)> fn) {
9192
std::vector<std::thread> threads;
92-
std::vector<std::exception_ptr> errors;
93+
std::mutex mutex;
94+
std::vector<std::string> errors;
9395
for (int rank = 0; rank < size; rank++) {
9496
threads.push_back(std::thread([&, rank]() {
9597
try {
9698
fn(rank);
97-
} catch (const std::exception&) {
98-
errors.push_back(std::current_exception());
99+
} catch (const std::exception& e) {
100+
std::lock_guard<std::mutex> lock(mutex);
101+
errors.push_back(e.what());
99102
}
100103
}));
101104
}
@@ -107,7 +110,7 @@ class BaseTest : public ::testing::Test {
107110

108111
// Re-throw first exception if there is one
109112
if (errors.size() > 0) {
110-
std::rethrow_exception(errors[0]);
113+
throw std::runtime_error(errors[0]);
111114
}
112115
}
113116

0 commit comments

Comments
 (0)