Skip to content

run_async behavior when handlers throw #259

@anarthal

Description

@anarthal

The following is a common pattern in Boost.Asio, meant to propagate exceptions thrown in coroutines (i.e. to terminate main in case of an unhandled exception):

#include <boost/capy/ex/run_async.hpp>
#include <boost/capy/task.hpp>
#include <boost/corosio/io_context.hpp>

#include <exception>
#include <stdexcept>

namespace corosio = boost::corosio;
namespace capy = boost::capy;

capy::task<> main_impl()
{
    throw std::runtime_error("this should propagate");
    co_return;
}

int main()
{
    corosio::io_context ctx;

    capy::run_async(
        ctx.get_executor(),
        []() { },
        [](std::exception_ptr exc) {
            if (exc)
                std::rethrow_exception(exc);
        }
    )(main_impl());

    ctx.run();
}

Unfortunately, this does not work, and the exception is silently swallowed by run_async_trampoline::unhandled_exception(). This is surprising behavior for me. I'd suggest to either document it or change it.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions