From 7d753d779ec95db39e5fedc3cc16c66f0c8f7d8a Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 4 Mar 2015 02:27:33 +0900 Subject: [PATCH] asio: Fix resource leak (socket not closed) in server code --- src/asio_server.cc | 62 ++++++++++++++++++++++------------------------ src/asio_server.h | 2 +- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/asio_server.cc b/src/asio_server.cc index 811dcb5e..f80d78f7 100644 --- a/src/asio_server.cc +++ b/src/asio_server.cc @@ -82,7 +82,9 @@ server::server(const std::string &address, uint16_t port, acceptors_.push_back(std::move(acceptor)); } - start_accept(); + for (auto &acceptor : acceptors_) { + start_accept(acceptor); + } start_timer(); } @@ -109,48 +111,44 @@ void server::start_timer() { typedef boost::asio::ssl::stream ssl_socket; -void server::start_accept() { +void server::start_accept(boost::asio::ip::tcp::acceptor &acceptor) { if (ssl_ctx_) { auto new_connection = std::make_shared>( request_cb_, io_service_pool_.get_io_service(), *ssl_ctx_); - for (auto &acceptor : acceptors_) { - acceptor.async_accept( - new_connection->socket().lowest_layer(), - [this, new_connection](const boost::system::error_code &e) { - if (!e) { - new_connection->socket().lowest_layer().set_option( - boost::asio::ip::tcp::no_delay(true)); - new_connection->socket().async_handshake( - boost::asio::ssl::stream_base::server, - [new_connection](const boost::system::error_code &e) { - if (!e) { - new_connection->start(); - } - }); - } + acceptor.async_accept( + new_connection->socket().lowest_layer(), + [this, &acceptor, new_connection](const boost::system::error_code &e) { + if (!e) { + new_connection->socket().lowest_layer().set_option( + boost::asio::ip::tcp::no_delay(true)); + new_connection->socket().async_handshake( + boost::asio::ssl::stream_base::server, + [new_connection](const boost::system::error_code &e) { + if (!e) { + new_connection->start(); + } + }); + } - start_accept(); - }); - } + start_accept(acceptor); + }); } else { auto new_connection = std::make_shared>( request_cb_, io_service_pool_.get_io_service()); - for (auto &acceptor : acceptors_) { - acceptor.async_accept( - new_connection->socket(), - [this, new_connection](const boost::system::error_code &e) { - if (!e) { - new_connection->socket().set_option( - boost::asio::ip::tcp::no_delay(true)); - new_connection->start(); - } + acceptor.async_accept( + new_connection->socket(), + [this, &acceptor, new_connection](const boost::system::error_code &e) { + if (!e) { + new_connection->socket().set_option( + boost::asio::ip::tcp::no_delay(true)); + new_connection->start(); + } - start_accept(); - }); - } + start_accept(acceptor); + }); } } diff --git a/src/asio_server.h b/src/asio_server.h index 69090948..ba109256 100644 --- a/src/asio_server.h +++ b/src/asio_server.h @@ -72,7 +72,7 @@ public: private: /// Initiate an asynchronous accept operation. - void start_accept(); + void start_accept(boost::asio::ip::tcp::acceptor &acceptor); void start_timer();