diff --git a/src/bin/dhcp4/tests/http_control_socket_unittest.cc b/src/bin/dhcp4/tests/http_control_socket_unittest.cc index d53df7f130..38880683c2 100644 --- a/src/bin/dhcp4/tests/http_control_socket_unittest.cc +++ b/src/bin/dhcp4/tests/http_control_socket_unittest.cc @@ -149,7 +149,10 @@ public: this, true), TEST_TIMEOUT, IntervalTimer::ONE_SHOT); // Run until the client stops the service or an error occurs. - io_service->run(); + try { + io_service->run(); + } catch (...) { + } test_timer.cancel(); if (io_service->stopped()) { io_service->restart(); diff --git a/src/bin/dhcp6/tests/http_control_socket_unittest.cc b/src/bin/dhcp6/tests/http_control_socket_unittest.cc index 4487d645ce..0c72acc840 100644 --- a/src/bin/dhcp6/tests/http_control_socket_unittest.cc +++ b/src/bin/dhcp6/tests/http_control_socket_unittest.cc @@ -187,7 +187,10 @@ public: this, true), TEST_TIMEOUT, IntervalTimer::ONE_SHOT); // Run until the client stops the service or an error occurs. - io_service->run(); + try { + io_service->run(); + } catch (...) { + } test_timer.cancel(); if (io_service->stopped()) { io_service->restart(); diff --git a/src/lib/http/connection.cc b/src/lib/http/connection.cc index fdf0f37028..4c83d1b0b6 100644 --- a/src/lib/http/connection.cc +++ b/src/lib/http/connection.cc @@ -68,7 +68,7 @@ SocketCallback::operator()(boost::system::error_code ec, size_t length) { HttpConnection::HttpConnection(const asiolink::IOServicePtr& io_service, const HttpAcceptorPtr& acceptor, const TlsContextPtr& tls_context, - HttpConnectionPool& connection_pool, + HttpConnectionPoolPtr connection_pool, const HttpResponseCreatorPtr& response_creator, const HttpAcceptorCallback& callback, const long request_timeout, @@ -234,11 +234,16 @@ HttpConnection::close() { void HttpConnection::shutdownConnection() { + auto connection_pool = connection_pool_.lock(); try { LOG_DEBUG(http_logger, isc::log::DBGLVL_TRACE_BASIC, HTTP_CONNECTION_SHUTDOWN) .arg(getRemoteEndpointAddressAsText()); - connection_pool_.shutdown(shared_from_this()); + if (connection_pool) { + connection_pool->shutdown(shared_from_this()); + } else { + shutdown(); + } } catch (...) { LOG_ERROR(http_logger, HTTP_CONNECTION_SHUTDOWN_FAILED); } @@ -246,11 +251,16 @@ HttpConnection::shutdownConnection() { void HttpConnection::stopThisConnection() { + auto connection_pool = connection_pool_.lock(); try { LOG_DEBUG(http_logger, isc::log::DBGLVL_TRACE_BASIC, HTTP_CONNECTION_STOP) .arg(getRemoteEndpointAddressAsText()); - connection_pool_.stop(shared_from_this()); + if (connection_pool) { + connection_pool->stop(shared_from_this()); + } else { + close(); + } } catch (...) { LOG_ERROR(http_logger, HTTP_CONNECTION_STOP_FAILED); } diff --git a/src/lib/http/connection.h b/src/lib/http/connection.h index eec18d319c..11b90799ba 100644 --- a/src/lib/http/connection.h +++ b/src/lib/http/connection.h @@ -245,7 +245,7 @@ public: HttpConnection(const asiolink::IOServicePtr& io_service, const HttpAcceptorPtr& acceptor, const asiolink::TlsContextPtr& tls_context, - HttpConnectionPool& connection_pool, + std::shared_ptr connection_pool, const HttpResponseCreatorPtr& response_creator, const HttpAcceptorCallback& callback, const long request_timeout, @@ -441,7 +441,7 @@ protected: HttpAcceptorPtr acceptor_; /// @brief Connection pool holding this connection. - HttpConnectionPool& connection_pool_; + std::weak_ptr connection_pool_; /// @brief Pointer to the @ref HttpResponseCreator object used to create /// HTTP responses. diff --git a/src/lib/http/connection_pool.h b/src/lib/http/connection_pool.h index ab6a7cfde6..d5a995bc31 100644 --- a/src/lib/http/connection_pool.h +++ b/src/lib/http/connection_pool.h @@ -71,6 +71,9 @@ protected: std::mutex mutex_; }; +/// @brief Pointer to the @ref HttpConnection. +typedef std::shared_ptr HttpConnectionPoolPtr; + } } diff --git a/src/lib/http/listener_impl.cc b/src/lib/http/listener_impl.cc index a5df194c16..70535ef7e5 100644 --- a/src/lib/http/listener_impl.cc +++ b/src/lib/http/listener_impl.cc @@ -26,7 +26,7 @@ HttpListenerImpl::HttpListenerImpl(const IOServicePtr& io_service, const long request_timeout, const long idle_timeout) : io_service_(io_service), tls_context_(tls_context), acceptor_(), - endpoint_(), connections_(), + endpoint_(), connections_(new HttpConnectionPool()), creator_factory_(creator_factory), request_timeout_(request_timeout), idle_timeout_(idle_timeout), use_external_(false) { @@ -102,7 +102,7 @@ HttpListenerImpl::start() { void HttpListenerImpl::stop() { - connections_.stopAll(); + connections_->stopAll(); if (use_external_) { IfaceMgr::instance().deleteExternalSocket(acceptor_->getNative()); } @@ -124,7 +124,7 @@ HttpListenerImpl::accept() { conn->addExternalSockets(true); } // Add this new connection to the pool. - connections_.start(conn); + connections_->start(conn); } void diff --git a/src/lib/http/listener_impl.h b/src/lib/http/listener_impl.h index f65cd3fa6d..73f70dc507 100644 --- a/src/lib/http/listener_impl.h +++ b/src/lib/http/listener_impl.h @@ -128,7 +128,7 @@ protected: boost::scoped_ptr endpoint_; /// @brief Pool of active connections. - HttpConnectionPool connections_; + HttpConnectionPoolPtr connections_; /// @brief Pointer to the @ref HttpResponseCreatorFactory. HttpResponseCreatorFactoryPtr creator_factory_; @@ -144,7 +144,6 @@ protected: bool use_external_; }; - } // end of namespace isc::http } // end of namespace isc diff --git a/src/lib/http/tests/connection_pool_unittests.cc b/src/lib/http/tests/connection_pool_unittests.cc index c6e7258541..bbbdd7e15f 100644 --- a/src/lib/http/tests/connection_pool_unittests.cc +++ b/src/lib/http/tests/connection_pool_unittests.cc @@ -107,7 +107,7 @@ public: HttpConnectionPoolTest() : io_service_(new IOService()), acceptor_(new HttpAcceptor(io_service_)), - connection_pool_(), + connection_pool_(new HttpConnectionPool()), response_creator_(new TestHttpResponseCreator()) { MultiThreadingMgr::instance().setMode(false); } @@ -219,7 +219,7 @@ public: IOServicePtr io_service_; ///< IO service. HttpAcceptorPtr acceptor_; ///< Test acceptor. - HttpConnectionPool connection_pool_; ///< Test connection pool. + HttpConnectionPoolPtr connection_pool_; ///< Test connection pool. HttpResponseCreatorPtr response_creator_; ///< Test response creator. }; diff --git a/src/lib/http/tests/http_server_test.h b/src/lib/http/tests/http_server_test.h index a87c411a1a..48c0671400 100644 --- a/src/lib/http/tests/http_server_test.h +++ b/src/lib/http/tests/http_server_test.h @@ -125,7 +125,7 @@ public: HttpConnectionLongWriteBuffer(const IOServicePtr& io_service, const HttpAcceptorPtr& acceptor, const TlsContextPtr& tls_context, - HttpConnectionPool& connection_pool, + HttpConnectionPoolPtr connection_pool, const HttpResponseCreatorPtr& response_creator, const HttpAcceptorCallback& callback, const long request_timeout, @@ -172,7 +172,7 @@ public: HttpConnectionTransactionChange(const IOServicePtr& io_service, const HttpAcceptorPtr& acceptor, const TlsContextPtr& tls_context, - HttpConnectionPool& connection_pool, + HttpConnectionPoolPtr connection_pool, const HttpResponseCreatorPtr& response_creator, const HttpAcceptorCallback& callback, const long request_timeout,