[libcxx-commits] [libcxx] [libc++] Fix std::future not waiting until the thread is finished to clean up (PR #130145)
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Mar 30 02:33:20 PDT 2025
https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/130145
>From 8e7f5c5d673a6c1aa8c00cd71ca8f531283a7214 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Thu, 6 Mar 2025 18:14:59 +0100
Subject: [PATCH] [libc++] Fix std::future not waiting until the thread is
finished to clean up
---
libcxx/include/future | 11 +++++-
.../futures.async/wait_on_destruct.pass.cpp | 36 +++++++++++++++++++
2 files changed, 46 insertions(+), 1 deletion(-)
create mode 100644 libcxx/test/std/thread/futures/futures.async/wait_on_destruct.pass.cpp
diff --git a/libcxx/include/future b/libcxx/include/future
index a08687485bd99..197830809abad 100644
--- a/libcxx/include/future
+++ b/libcxx/include/future
@@ -1829,7 +1829,16 @@ template <class _Rp, class _Fp>
_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f) {
unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
new __async_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
- std::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
+#if _LIBCPP_HAS_EXCEPTIONS
+ try {
+#endif
+ std::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
+#if _LIBCPP_HAS_EXCEPTIONS
+ } catch (...) {
+ __h->__make_ready();
+ throw;
+ }
+#endif
return future<_Rp>(__h.get());
}
diff --git a/libcxx/test/std/thread/futures/futures.async/wait_on_destruct.pass.cpp b/libcxx/test/std/thread/futures/futures.async/wait_on_destruct.pass.cpp
new file mode 100644
index 0000000000000..b33d38736100c
--- /dev/null
+++ b/libcxx/test/std/thread/futures/futures.async/wait_on_destruct.pass.cpp
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// UNSUPPORTED: no-threads
+// UNSUPPORTED: c++03
+
+// This test uses std::atomic interfaces that are only available in C++20
+// UNSUPPORTED: c++11, c++14, c++17
+
+// Make sure that the `future` destructor keeps the data alive until the thread finished. This test fails by triggering
+// TSan. It may not be observable by normal means.
+
+#include <atomic>
+#include <future>
+#include <mutex>
+
+std::mutex mux;
+
+int main() {
+ using namespace std::chrono_literals;
+ std::scoped_lock lock(mux);
+ std::atomic<bool> in_async = false;
+ auto v = std::async(std::launch::async, [&in_async, value = 1]() mutable {
+ in_async = true;
+ in_async.notify_all();
+ std::scoped_lock thread_lock(mux);
+ value = 4;
+ (void)value;
+ });
+ in_async.wait(true);
+}
More information about the libcxx-commits
mailing list