Skip to content

Commit 3973efa

Browse files
yfeldblumfacebook-github-bot
authored andcommitted
avoid rethrowing exceptions in AsyncScope
Reviewed By: snarkmaster Differential Revision: D69706244 fbshipit-source-id: 93c60b59b4b1fa1680a85622ada8f9a2e1742d1f
1 parent fcd0110 commit 3973efa

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

folly/coro/AsyncScope.h

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,25 +161,36 @@ class AsyncScope {
161161
std::is_same_v<await_result_t<Awaitable>, folly::Unit>,
162162
"Result of the task would be discarded. Make sure task result is either void or folly::Unit.");
163163

164+
exception_wrapper exn;
164165
try {
165-
co_await std::move(awaitable);
166-
} catch (const OperationCancelled&) {
166+
if constexpr (detail::is_awaitable_try<Awaitable>) {
167+
auto ret = co_await co_awaitTry(std::move(awaitable));
168+
if (ret.hasException()) {
169+
exn = std::move(ret.exception());
170+
}
171+
} else {
172+
co_await std::move(awaitable);
173+
}
167174
} catch (...) {
175+
// not-awaitable-try awaitables and not-noexcept-copy-constructible values
176+
exn = exception_wrapper(std::current_exception());
177+
}
178+
if (exn && !exn.get_exception<OperationCancelled>()) {
168179
if (throwOnJoin) {
169180
LOG(ERROR)
170181
<< (source.has_value() ? sourceLocationToString(source.value())
171182
: "")
172183
<< "Unhandled exception thrown from task added to AsyncScope: "
173-
<< folly::exceptionStr(current_exception());
184+
<< exn;
174185
if (!exceptionRaised.exchange(true)) {
175-
maybeException = folly::exception_wrapper(current_exception());
186+
maybeException = std::move(exn);
176187
}
177188
} else {
178189
LOG(DFATAL)
179190
<< (source.has_value() ? sourceLocationToString(source.value())
180191
: "")
181192
<< "Unhandled exception thrown from task added to AsyncScope: "
182-
<< folly::exceptionStr(current_exception());
193+
<< exn;
183194
}
184195
}
185196
}
@@ -255,8 +266,8 @@ inline Task<void> AsyncScope::joinAsync() noexcept {
255266
joinStarted_.store(true, std::memory_order_relaxed);
256267
co_await barrier_.arriveAndWait();
257268
joined_ = true;
258-
if (maybeException_.has_exception_ptr()) {
259-
maybeException_.throw_exception();
269+
if (maybeException_) {
270+
co_yield co_error{std::move(maybeException_)};
260271
}
261272
}
262273

folly/coro/ViaIfAsync.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,20 @@ using semi_await_result_t = await_result_t<decltype(folly::coro::co_viaIfAsync(
574574

575575
namespace detail {
576576

577+
template <typename Awaiter>
578+
using detect_await_resume_try =
579+
decltype(FOLLY_DECLVAL(Awaiter).await_resume_try());
580+
581+
template <typename Awaiter>
582+
constexpr bool is_awaiter_try = is_detected_v<detect_await_resume_try, Awaiter>;
583+
584+
template <typename Awaitable>
585+
constexpr bool is_awaitable_try = is_awaiter_try<awaiter_type_t<Awaitable>>;
586+
577587
template <typename Awaitable>
578588
class TryAwaiter {
589+
static_assert(is_awaitable_try<Awaitable&&>);
590+
579591
using Awaiter = awaiter_type_t<Awaitable>;
580592

581593
public:

0 commit comments

Comments
 (0)