Boost logo

Boost Users :

Subject: [Boost-users] [asio] how to handle sync_accept error to avoid server hang on?
From: tomasz jankowski (tomasz_jacek_at_[hidden])
Date: 2009-02-04 06:29:34


hello,

actualy I'm testing my server. I'm making new connections faster than
server is able to handle it.
because of it server quickly is getting from system errno 24 (Too many
open files).

because of chaining async operation in asio design async_accept handler
like below lead to server hang on.

void AnldServer::handle_accept(const boost::system::error_code& e)
{
  if (!e)
  {
    new_connection_->start();
    new_connection_.reset(new AnldConnection(io_service_, request_handler_));
    acceptor_.async_accept(new_connection_->socket(),
        boost::bind(&AnldServer::handle_accept, this,
          boost::asio::placeholders::error));
  }else{
        NDSLOG_NOTICE_S() << "Accept error: " << e << "\n";
  }
}

Since we are not starting a new accept operation the io_service will run
out of work to do and the server will hang on (if it was daemonised) or
exit(if not).
ofcourse server in production environment couldn't behaves like this.
server must can handle such situation.

my question is: how to fix it? just start async_accept without creating
new_connection in case of error?

like this for example?

void AnldServer::handle_accept(const boost::system::error_code& e)
{
  if (!e)
  {
    new_connection_->start();
    new_connection_.reset(new AnldConnection(io_service_, request_handler_));
    acceptor_.async_accept(new_connection_->socket(),
        boost::bind(&AnldServer::handle_accept, this,
          boost::asio::placeholders::error));
  }else{
        NDSLOG_NOTICE_S() << "Accept error: " << e << "\n";
    acceptor_.async_accept(new_connection_->socket(),
        boost::bind(&AnldServer::handle_accept, this,
          boost::asio::placeholders::error));
  }
}

I have just tested above code - it seems to work fine.
but maybe someone have beter solution>
or maybe it is good idea to add sleep(5) before async_accept in case of
error?

thanks,

tom

----------------------------------------------------
Twoja rodzina na bliscy.pl
Zobacz:
http://klik.wp.pl/?adr=http%3A%2F%2Fcorto.www.wp.pl%2Fas%2Fbliscy.html&sid=628


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net