I'm tring to write my custom async function for boost::asio as described here.
However I'm getting boost::coroutines::detail::forced_unwind exception on line with result.get
#include <boost/chrono.hpp>
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/steady_timer.hpp>
#include <iostream>
namespace asio = ::boost::asio;
template <typename Timer, typename Token>
auto my_timer (Timer& timer, Token&& token)
{
typename asio::handler_type<Token,
void (::boost::system::error_code const)>::type
handler (std::forward<Token> (token));
asio::async_result<decltype (handler)> result (handler);
timer.async_wait (handler);
return result.get (); // Got forced_unwind exception here.
}
int main ()
{
asio::io_service io;
asio::steady_timer timer (io, ::boost::chrono::seconds (1));
asio::spawn (io, [&] (asio::yield_context yield)
{
try {
std::cout << "my_timer enter\n";
my_timer (timer, yield);
std::cout << "my_timer returns\n";
}
catch (const boost::coroutines::detail::forced_unwind& e)
{
std::cout << "boost::coroutines::detail::forced_unwind\n";
}
}
);
io.run ();
}
Same code on Coliru
UPDATE:
The behavior exists on:
Darwin 14.0.0 (MacOS 10.10)
clang version 3.6.0 (trunk 216817) and gcc version 4.9.1 (MacPorts gcc49 4.9.1_1)
boost 1.57
and
Red Hat 6.5
gcc version 4.7.2 20121015 (Red Hat 4.7.2-5) (GCC)
boost 1.57 and 1.56
(the example code was trivially modified because gcc 4.7 does not support c++14 mode)
In short, you need to create a copy of handler, such as by posting it into the
io_service
, before attempting to get theasync_result
in order to keep the coroutine alive.Boost.Asio prevents a non-resumable coroutine from indefinitely suspending by destroying the coroutine, resulting in the coroutine's stack to unwind. The coroutine object will throw
boost::coroutines::detail::forced_unwind
during its destruction, causing the suspended stack to unwind. Asio accomplishes this by:yield_context
CompletionToken maintains aweak_ptr
to the coroutine.handler_type::type
handler is constructed, it obtains ashared_ptr
for the coroutine via the CompletionToken'sweak_ptr
. When the handler is passed as the completion handler to asynchronous operations, the handler and itsshared_ptr
are copied. When the handler is invoked, it resumes the coroutine.async_result::get()
, the specialization will reset the coroutineshared_ptr
owned by the handler that was passed toasync_result
during construction, and then yield the coroutine.Here is an attempt to illustrate the execution of the code. Paths in
|
indicate the active stack,:
indicates the suspended stack, and arrows are used to indicate transfer of control:To fix this problem,
handler
needs to be copied and invoked throughasio_handler_invoke()
when it is time to resume the coroutine. For example, the following will post a completion handler1 intoio_service
that invokes a copy ofhandler
:As demonstrated here, with this additional code, the output becomes:
1. The completion handler code can likely be cleaned up a bit, but as I was answering how to resume a Boost.Asio stackful coroutine from a different thread, I observed some compilers selecting the wrong
asio_handler_invoke
hook.