boostorg / cobalt

Coroutines for C++20 & asio
https://www.boost.org/doc/libs/master/libs/cobalt/doc/html/index.html
228 stars 25 forks source link

cobalt::try_handle_all does not behave as expected #213

Open Kajonn opened 1 week ago

Kajonn commented 1 week ago

Thanks for a great library!

We have discovered that cobalt::try_handle_all may not "catch" errors as expected (as boost::leaf::try_handle_all does).

This example program is expected to print 1 but print 2, indicating the SomeError cannot be handled:

 #include <boost/cobalt.hpp>
#include <boost/cobalt/leaf.hpp>
#include <boost/cobalt/main.hpp>
#include <boost/leaf/handle_errors.hpp>
#include <boost/leaf/result.hpp>
#include <iostream>
struct SomeError
{
    int val;
};

boost::cobalt::main co_main(int, char *[])
{
    auto r = co_await boost::cobalt::try_handle_all(
        []() -> boost::cobalt::promise<boost::leaf::result<int>> {
            co_return boost::leaf::new_error(SomeError {1234});
            co_return 0;
        }(),
        [](SomeError &) -> int {
            return 1;
        },
        []() -> int {
            return 2;
        });
    std::cout << r << std::endl;
    co_return r;
}

The example using boost::leaf::try_handle_all does print 1 as expected:

#include <boost/leaf/handle_errors.hpp>
#include <boost/leaf/result.hpp>
#include <iostream>

struct SomeError
{
    int val;
};

int main() {
    auto r = boost::leaf::try_handle_all(
        []() -> boost::leaf::result<int> {
            return boost::leaf::new_error(SomeError {1234});
        },
        [](SomeError &e) {
            return 1;
        },
        []() {
            return 2;
        });
    std::cout << r << std::endl;    
    return r;
}

Is it a bug or is it something wrong with our implementation in the example?

Best regards Jonas

klemens-morgenstern commented 1 week ago

Probably a bug.