From 109bbcc87386143f0e0a785cfe3b62c3ca005574 Mon Sep 17 00:00:00 2001 From: Omar Mohamed Date: Wed, 18 Sep 2024 12:55:32 +0300 Subject: [PATCH] Fix coroutine continuation handle Using coroutines directly by the user e.g. declaring a function with drogon::Task<> return type, will have it's continuation set to nullptr. Returning nullptr causes a segfault and it must be checked before returning e.g. the snippet form https://en.cppreference.com/w/cpp/coroutine/noop_coroutine ```cpp struct final_awaiter { std::coroutine_handle<> await_suspend(std::coroutine_handle h) noexcept { // final_awaiter::await_suspend is called when the execution of the // current coroutine (referred to by 'h') is about to finish. // If the current coroutine was resumed by another coroutine via // co_await get_task(), a handle to that coroutine has been stored // as h.promise().previous. In that case, return the handle to resume // the previous coroutine. // Otherwise, return noop_coroutine(), whose resumption does nothing. if (auto previous = h.promise().previous; previous) return previous; else return std::noop_coroutine(); } }; ``` This commit default initializes the continuation handle to no op coroutine and avoids the check. Signed-off-by: Omar Mohamed --- lib/inc/drogon/utils/coroutine.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/inc/drogon/utils/coroutine.h b/lib/inc/drogon/utils/coroutine.h index 9c6678d2e4..601d0d3de6 100644 --- a/lib/inc/drogon/utils/coroutine.h +++ b/lib/inc/drogon/utils/coroutine.h @@ -245,7 +245,7 @@ struct [[nodiscard]] Task std::optional value; std::exception_ptr exception_; - std::coroutine_handle<> continuation_; + std::coroutine_handle<> continuation_{std::noop_coroutine()}; }; auto operator co_await() const noexcept @@ -332,7 +332,7 @@ struct [[nodiscard]] Task } std::exception_ptr exception_; - std::coroutine_handle<> continuation_; + std::coroutine_handle<> continuation_{std::noop_coroutine()}; }; auto operator co_await() const noexcept