Boost :: asio io_service doesn't return after stop ()

I am working on an application that uses boost :: asio to listen on different ports for TCP and UDP packets. I did it using similar server classes as in the asio tutorial examples and I point them all to one io_service.

So my main function (win32 console app) looks like this:

int main(int argc, char* argv[])
{
    //some initializations here

    try
    {
        asio::io_service io_service;
        TcpServer ServerTCP_1(io_service, /*port number here*/);
        TcpServer ServerTCP_2(io_service, /*port number here*/);
        UdpServer ServerUDP(io_service, /*port number here*/);

        io_service.run();
    }
    catch(std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }

    //cleanup code here

    return 0;
}

      

I also read an example HTTP server where it is signal_set

used to perform an asynchronous operation that is triggered when a quit signal is received (which I also implemented in the server class).

Here is my class TcpServer

:

class TcpServer : private noncopyable
{
public:
    TcpServer(asio::io_service& io_service, int port) : 
        acceptor_(io_service, tcp::endpoint(tcp::v4(), port)), 
        signals_(io_service), context_(asio::ssl::context::sslv3)
    {
        SSL_CTX_set_cipher_list(context_.native_handle(), "ALL");
        SSL_CTX_set_options(context_.native_handle(), SSL_OP_ALL);
        SSL_CTX_use_certificate_ASN1(context_.native_handle(), sizeof(SSL_CERT_X509), SSL_CERT_X509);
        SSL_CTX_use_PrivateKey_ASN1(EVP_PKEY_RSA, context_.native_handle(), SSL_CERT_RSA, sizeof(SSL_CERT_RSA));
        SSL_CTX_set_verify_depth(context_.native_handle(), 1);

        signals_.add(SIGINT);
        signals_.add(SIGTERM);
        signals_.add(SIGBREAK);
#if defined(SIGQUIT)
        signals_.add(SIGQUIT);
#endif // defined(SIGQUIT)
        signals_.async_wait(boost::bind(&TcpServer::handle_stop, this));

        start_accept();
    }

private:
    tcp::acceptor acceptor_;
    asio::ssl::context context_;
    asio::signal_set signals_;
    TcpConnection::pointer new_ssl_connection;

    void start_accept(){
        new_ssl_connection.reset(new TcpConnection(acceptor_.get_io_service(), context_));
        acceptor_.async_accept( new_ssl_connection->socket(), 
                                    boost::bind(&TcpServer::handle_acceptSSL, this, asio::placeholders::error));
    }

    void handle_acceptSSL(const system::error_code& error){
        if(!error)
            new_ssl_connection->start();
        start_accept();
    }
    void handle_stop(){
        acceptor_.close();
        printf("acceptor closed");
    }
};

      

Now that I have 3 different server objects, it should close all acceptors from them, leaving them io_service

out of work, which again makes the io_service

call return run()

, which should force the program to get the cleanup code in the main function, right? Also, that the call printf

has to be done 3 times or?

Well, first of all io_service

doesn't return, the cleanup code is never reached. Second, the console most displays only one printf call.

And third, after doing some debugging, I found out that when I exit the program, handle_stop()

it gets called once, but for some reason the program closes somewhere in the handle (which means that sometimes it gets a printf and exits after that. and sometimes it just comes to acceptor_.close()

)

Another thing worth mentioning is that the program does not return 0 after closing, but instead (the number of threads changes):

The thread 'Win32 Thread' (0x1758) has exited with code 2 (0x2).
The thread 'Win32 Thread' (0x17e8) has exited with code -1073741510 (0xc000013a).
The thread 'Win32 Thread' (0x1034) has exited with code -1073741510 (0xc000013a).
The program '[5924] Server.exe: Native' has exited with code -1073741510 (0xc000013a).

      

So, I would like to know why this is happening, how can I fix it and how can I get to the cleanup code correctly?

+3


source to share


1 answer


io_service

never returns because io_service

there is always work to do. When called acceptor::close()

, asynchronous receive operations acceptor

will be canceled immediately. An error will be passed to the handlers for these canceled operations boost::asio::error::operation_aborted

.

In the current code, the problem occurs because a new asynchronous receive operation is initialized even though it has acceptor

been closed. Thus, work is always added to io_service

.

void start_accept(){
  // If the acceptor is closed, handle_accept will be ready to run with an
  // error.
  acceptor_.async_accept(...,  handle_accept);
}

void handle_accept(const system::error_code& error){
  if(!error)
  {
    connection->start();
  }

  // Always starts a new async accept operation, even if the acceptor
  // has closed.
  start_accept();
}
void handle_stop(){
  acceptor_.close();
}

      



The Boost.Asio examples prevent this from checking if acceptor_

closed in a call handle_accept

. If acceptor_

it doesn't open anymore, the handler returns earlier without adding any more work to io_service

.

Here's a relevant snippet from the HTTP Server 1 example :

void server::handle_accept(const boost::system::error_code& e)
{
  // Check whether the server was stopped by a signal before this completion
  // handler had a chance to run.
  if (!acceptor_.is_open())
  {
    return;
  }

  if (!e)
  {
    connection_manager_.start(new_connection_);
  }

  start_accept();
}

      

+2


source







All Articles