diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 5bf0b937d..f2f2b6a89 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -45,7 +45,7 @@ void HostNodeTableHandler::processEvent(NodeId const& _n, NodeTableEventType con m_host.onNodeTableEvent(_n, _e); } -Host::Host(std::string const& _clientVersion, NetworkPreferences const& _n, bool _start): +Host::Host(std::string const& _clientVersion, NetworkPreferences const& _n): Worker("p2p", 0), m_clientVersion(_clientVersion), m_netPrefs(_n), @@ -60,8 +60,6 @@ Host::Host(std::string const& _clientVersion, NetworkPreferences const& _n, bool clog(NetNote) << "IP Address: " << address << " = " << (isPrivateAddress(address) ? "[LOCAL]" : "[PEER]"); clog(NetNote) << "Id:" << id(); - if (_start) - start(); } Host::~Host() @@ -300,16 +298,34 @@ void Host::runAcceptor() { clog(NetConnect) << "Listening on local port " << m_listenPort << " (public: " << m_tcpPublic << ")"; m_accepting = true; + + // socket is created outside of acceptor-callback + // An allocated socket is necessary as asio can use the socket + // until the callback succeeds or fails. + // + // Until callback succeeds or fails, we can't dealloc it. + // + // Callback is guaranteed to be called via asio or when + // m_tcp4Acceptor->stop() is called by Host. + // + // All exceptions are caught so they don't halt asio and so the + // socket is deleted. + // + // It's possible for an accepted connection to return an error in which + // case the socket may be open and must be closed to prevent asio from + // processing socket events after socket is deallocated. - bi::tcp::socket* s = new bi::tcp::socket(m_ioService); + bi::tcp::socket *s = new bi::tcp::socket(m_ioService); m_tcp4Acceptor.async_accept(*s, [=](boost::system::error_code ec) { + // if no error code, doHandshake takes ownership bool success = false; if (!ec) { try { - // incoming connection so we don't yet know nodeid + // doHandshake takes ownersihp of *s via std::move + // incoming connection; we don't yet know nodeid doHandshake(s, NodeId()); success = true; } diff --git a/libp2p/Host.h b/libp2p/Host.h index d770b81b1..a4493f9bf 100644 --- a/libp2p/Host.h +++ b/libp2p/Host.h @@ -125,7 +125,7 @@ class Host: public Worker public: /// Start server, listening for connections on the given port. - Host(std::string const& _clientVersion, NetworkPreferences const& _n = NetworkPreferences(), bool _start = false); + Host(std::string const& _clientVersion, NetworkPreferences const& _n = NetworkPreferences()); /// Will block on network process events. virtual ~Host();