boostorg / leaf

Lightweight Error Augmentation Framework
Boost Software License 1.0
302 stars 48 forks source link

Assertion failed: top_!=0 && *top_==this, when using Boost::coroutine #10

Closed Tradias closed 3 years ago

Tradias commented 3 years ago

The following code triggers assertion: Assertion failed: top_!=0 && *top_==this, file boost/leaf/error.hpp, line 284. Both on development branch from 4th October 2020 and in version 0.2.1.

#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/leaf.hpp>

namespace bl = boost::leaf;
namespace asio = boost::asio;

int main() {
    asio::io_context io_context;
    asio::spawn(io_context, [&](auto &&yield) {
        bl::try_handle_some(
            [&]() -> bl::result<void> {
                asio::steady_timer timer(io_context);
                timer.expires_after(std::chrono::milliseconds(100));
                timer.async_wait(yield);
                return bl::new_error(42);
            },
            [&](const int &) {});
    });
    asio::spawn(io_context, [&](auto &&yield) {
        bl::try_handle_some(
            [&]() -> bl::result<void> {
                asio::steady_timer timer(io_context);
                timer.expires_after(std::chrono::seconds(1));
                timer.async_wait(yield);
                return {};
            },
            [](const int &) {});
    });
    io_context.run();
}

If the assertion doesn't trigger then you may want to increase the wait time of the first timer. The goal is to have the second try_handle_some function being called and stuck before the first one is called and has errored.

To compile the code you will need Boost and link with the needed libraries:

find_package(Boost 1.73 REQUIRED COMPONENTS coroutine)
target_link_libraries(main PRIVATE Boost::boost Boost::coroutine)

I have tested it with Boost 1.73 and 1.74, but I am sure previous versions can be used as well.

zajo commented 3 years ago

Coroutines are not supported at least for now. I'd be interested in a proof of concept implementation if that is possible.