Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/inc/drogon/utils/coroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ struct [[nodiscard]] Task

struct promise_type
{
// Make promise_type a non-aggregate so Clang >= 21 does not
// aggregate-initialize `value` from the first coroutine argument
// (crashes if the conversion throws). See issue #2579.
promise_type() = default;

Task<T> get_return_object()
{
return Task<T>{handle_type::from_promise(*this)};
Expand Down Expand Up @@ -300,6 +305,9 @@ struct [[nodiscard]] Task<void>

struct promise_type
{
// Same as Task<T>::promise_type above. See issue #2579.
promise_type() = default;

Task<> get_return_object()
{
return Task<>{handle_type::from_promise(*this)};
Expand Down
24 changes: 24 additions & 0 deletions lib/tests/unittests/CoroutineTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,27 @@ DROGON_TEST(WhenAll)
CHECK(counter == 1);
}(TEST_CTX);
}

DROGON_TEST(PromiseNotAggregateInitializedFromArgs)
{
// Clang >= 21 used to aggregate-initialize Task<T>::promise_type::value
// from the first coroutine argument. If the argument was implicitly
// convertible to T and the conversion threw (e.g. nlohmann::json), the
// exception escaped the partially constructed coroutine frame and crashed
// the process. promise_type is a non-aggregate now, so the promise is
// default-constructed and the conversion never runs.
struct ThrowOnConvert
{
operator std::string() const
{
throw std::runtime_error("must not be converted");
}
};

auto returnOk = [](const ThrowOnConvert &) -> Task<std::string> {
co_return "ok";
};

ThrowOnConvert arg;
CHECK(sync_wait(returnOk(arg)) == "ok");
}
Loading