2

Can anyone tell me under what conditions boost::asio's io_service::run() method will return? The documentation documentation for io_service::run() seems to suggest that as long as there is work to be done or handlers to be dispatched, run() won't return.

The reason I'm asking this is that we have a legacy https client that contacts a server and executes http POST's. The separation of concerns in the client is a bit different than what we'd like so we're changing a few things about it, but we're running into problems.

Right now, the client basically has a mis-named connect() call that effectively drives the entire protocol conversation with the server. The connect() call starts off by creating a boost::asio::ip::tcp::resolver object and calling ::async_resolve() on it. This starts a chain where new asio calls are made from within asio callbacks.

void connect()
{
    m_resolver.async_resolve( query, bind( &clientclass::resolve_callback, this ) );
    thread = new boost::thread( bind( &boost::asio::io_service::run, m_io_service ) );
}
void resolve_callback( error_code & e, resolver::iterator i )
{
    if (!e)
    { 
        tcp::endpoint = *i;
        m_socket.lowest_layer().async_connect(endpoint, bind(&clientclass::connect_callback,this,_1,++i));
    }
}
void connect_callback( error_code & e, resolve::iterator i )
{
    if (!e)
    { 
        m_socket.lowest_layer().async_handshake(boost::asio::ssl::stream_base::client,
            bind(&clientclass::handshake_callback,this,_1,++i));
    }
}
void handshake_callback( error_code &e )
{
    if (!e)
    {
        mesg = format_hello_message();
        http_send( mesg, bind(&clientlass::hello_resp_handler,this,_1,_2) );
    }
}
void http_send( stringstream & mesg, reply_handler handler )
{
    async_write(m_socket, m_request_buffer, bind(&clientclass::write_complete_callback,this,_1,handler));
}
void write_comlete_callback( error_code &e, reply_handler handler )
{
    if (!e)
    {
        async_read_until(m_socket,m_reply_buffer,"\r\n\r\n", bind(&clientclass::handle_reply,this,handler));
    }
}
...

Anyways, this continues through the protocol until the protocol conversation is done. From the code here you can see that while connect() is running on the main thread, all of the subsequent callbacks and requests are coming back on the worker thread that is created in connect(). This is 'working' code.

When I try to break this chain up and expose it via an external interface, it stops working. In particular, I'm having the call handle_handshake() call outside of the clientclass object. Then http_send() is part of the interface (or is called by the external interface) and it creates a new worker thread to call io_service::run(). What happens is even though async_write() has been called and even though write_complete_callback() hasn't returned, io_service::run() exits. It exits without error and claims that no handlers were dispatched, but there's still 'work' to be done?

So what I'm wondering is what is io_service::run()'s definition of 'work'? Is it a pending request? Why is it that io_service::run() never returns during this chain of requests and responses in the existing code, but when I try to start the thread up again and start a new chain, it returns almost immediately before it's finished its work?

1 Answer 1

5

The definition of work in the context of the run() call is any pending asynchronous operations on that io_service object. This includes the invocations of the handlers in response to an operation. So, if a handler for one operation starts another operation, there is always work available.

In addition, there is an io_service::work class that can be used to create work on an io_service that never completes until the object is destroyed.

When a single chain completes, the io_service has completed all asynchronous operations, and all of the handler's have been invoked without starting a new operation, so it returns. Until you call io_service::reset(), further calls to run() will return without executing any operations.

3
  • +1 The OP has very likely not called reset before the subsequent run
    – Ralf
    Oct 25, 2012 at 6:09
  • That was EXACTLY what I wasn't doing. I added the ::reset() call and all is well now. Thanks! Oct 25, 2012 at 17:46
  • Thank you for mentioning the io_service::work object. I couldn't figure out why my threads calling io_context::run() would exit before I called io_context::stop(). Seems like this defeats the purpose of having a thread pool.
    – ulatekh
    May 3, 2022 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.