Skip to content

Commit 0d26216

Browse files
authored
[libc++] Make sure the test for compare-and-wait bug doesn't hang forever (#97907)
This patch adds an explicit timeout mechanism in the compare-and-wait test for std::atomic, ensuring that it doesn't run forever when the bug is present. This is not an issue when we run inside the CI because we specify a timeout manually, but it can be a problem when running locally, for example.
1 parent 7d1b6b2 commit 0d26216

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

libcxx/test/libcxx/atomics/atomics.syn/wait.pass.cpp renamed to libcxx/test/libcxx/atomics/atomics.syn/wait.issue_85107.pass.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,40 @@
1515

1616
// XFAIL: availability-synchronization_library-missing
1717

18+
// This is a regression test for https://github.com/llvm/llvm-project/issues/85107, which describes
19+
// how we were using UL_COMPARE_AND_WAIT instead of UL_COMPARE_AND_WAIT64 in the implementation of
20+
// atomic::wait, leading to potential infinite hangs.
21+
1822
#include <atomic>
1923
#include <cassert>
24+
#include <chrono>
25+
26+
#include "make_test_thread.h"
2027

21-
void test_85107() {
28+
int main(int, char**) {
2229
if constexpr (sizeof(std::__cxx_contention_t) == 8 && sizeof(long) > 4) {
30+
std::atomic<bool> done = false;
31+
auto const timeout = std::chrono::system_clock::now() + std::chrono::seconds(600); // fail after 10 minutes
32+
33+
auto timeout_thread = support::make_test_thread([&] {
34+
while (!done) {
35+
assert(std::chrono::system_clock::now() < timeout); // make sure we don't hang forever
36+
}
37+
});
38+
2339
// https://github.com/llvm/llvm-project/issues/85107
2440
// [libc++] atomic_wait uses UL_COMPARE_AND_WAIT when it should use UL_COMPARE_AND_WAIT64 on Darwin
2541
constexpr std::__cxx_contention_t old_val = 0;
2642
constexpr std::__cxx_contention_t new_val = old_val + (1ll << 32);
2743
std::__cxx_atomic_contention_t ct(new_val);
28-
std::__libcpp_atomic_wait(&ct, old_val); // this will hang forever if the bug is present
29-
}
30-
}
3144

32-
int main(int, char**) {
33-
test_85107();
45+
// This would hang forever if the bug is present, but the test will fail in a bounded amount of
46+
// time due to the timeout above.
47+
std::__libcpp_atomic_wait(&ct, old_val);
48+
49+
done = true;
50+
timeout_thread.join();
51+
}
3452

3553
return 0;
3654
}

0 commit comments

Comments
 (0)