Browse Source

Merge pull request #463 from arkpar/develop

fixed crashes on shutdown
cl-refactor
Gav Wood 10 years ago
parent
commit
0a6b4bf993
  1. 2
      alethzero/MainWin.h
  2. 40
      libp2p/Host.cpp
  3. 4
      libp2p/Host.h

2
alethzero/MainWin.h

@ -255,7 +255,7 @@ private:
QString m_logHistory; QString m_logHistory;
bool m_logChanged = true; bool m_logChanged = true;
std::unique_ptr<WebThreeStubServer> m_server;
QWebThreeConnector m_qwebConnector; QWebThreeConnector m_qwebConnector;
std::unique_ptr<WebThreeStubServer> m_server;
QWebThree* m_qweb = nullptr; QWebThree* m_qweb = nullptr;
}; };

40
libp2p/Host.cpp

@ -62,8 +62,8 @@ Host::Host(std::string const& _clientVersion, NetworkPreferences const& _n, bool
m_clientVersion(_clientVersion), m_clientVersion(_clientVersion),
m_netPrefs(_n), m_netPrefs(_n),
m_ioService(new ba::io_service), m_ioService(new ba::io_service),
m_acceptor(*m_ioService), m_acceptor(new bi::tcp::acceptor(*m_ioService)),
m_socket(*m_ioService), m_socket(new bi::tcp::socket(*m_ioService)),
m_key(KeyPair::create()) m_key(KeyPair::create())
{ {
populateAddresses(); populateAddresses();
@ -91,11 +91,11 @@ void Host::start()
bi::tcp::endpoint endpoint(bi::tcp::v4(), i ? 0 : m_netPrefs.listenPort); bi::tcp::endpoint endpoint(bi::tcp::v4(), i ? 0 : m_netPrefs.listenPort);
try try
{ {
m_acceptor.open(endpoint.protocol()); m_acceptor->open(endpoint.protocol());
m_acceptor.set_option(ba::socket_base::reuse_address(true)); m_acceptor->set_option(ba::socket_base::reuse_address(true));
m_acceptor.bind(endpoint); m_acceptor->bind(endpoint);
m_acceptor.listen(); m_acceptor->listen();
m_listenPort = i ? m_acceptor.local_endpoint().port() : m_netPrefs.listenPort; m_listenPort = i ? m_acceptor->local_endpoint().port() : m_netPrefs.listenPort;
break; break;
} }
catch (...) catch (...)
@ -105,7 +105,7 @@ void Host::start()
cwarn << "Couldn't start accepting connections on host. Something very wrong with network?\n" << boost::current_exception_diagnostic_information(); cwarn << "Couldn't start accepting connections on host. Something very wrong with network?\n" << boost::current_exception_diagnostic_information();
return; return;
} }
m_acceptor.close(); m_acceptor->close();
continue; continue;
} }
} }
@ -118,20 +118,24 @@ void Host::start()
void Host::stop() void Host::stop()
{ {
// if there's no ioService, it means we've had quit() called - bomb out - we're not allowed in here.
if (!m_ioService)
return;
for (auto const& h: m_capabilities) for (auto const& h: m_capabilities)
h.second->onStopping(); h.second->onStopping();
stopWorking(); stopWorking();
if (m_acceptor.is_open()) if (m_acceptor->is_open())
{ {
if (m_accepting) if (m_accepting)
m_acceptor.cancel(); m_acceptor->cancel();
m_acceptor.close(); m_acceptor->close();
m_accepting = false; m_accepting = false;
} }
if (m_socket.is_open()) if (m_socket->is_open())
m_socket.close(); m_socket->close();
disconnectPeers(); disconnectPeers();
if (!!m_ioService) if (!!m_ioService)
@ -147,6 +151,8 @@ void Host::quit()
// such tasks may involve socket reads from Capabilities that maintain references // such tasks may involve socket reads from Capabilities that maintain references
// to resources we're about to free. // to resources we're about to free.
stop(); stop();
m_acceptor.reset();
m_socket.reset();
m_ioService.reset(); m_ioService.reset();
// m_acceptor & m_socket are DANGEROUS now. // m_acceptor & m_socket are DANGEROUS now.
} }
@ -463,18 +469,18 @@ void Host::ensureAccepting()
{ {
clog(NetConnect) << "Listening on local port " << m_listenPort << " (public: " << m_public << ")"; clog(NetConnect) << "Listening on local port " << m_listenPort << " (public: " << m_public << ")";
m_accepting = true; m_accepting = true;
m_acceptor.async_accept(m_socket, [=](boost::system::error_code ec) m_acceptor->async_accept(*m_socket, [=](boost::system::error_code ec)
{ {
if (!ec) if (!ec)
{ {
try try
{ {
try { try {
clog(NetConnect) << "Accepted connection from " << m_socket.remote_endpoint(); clog(NetConnect) << "Accepted connection from " << m_socket->remote_endpoint();
} catch (...){} } catch (...){}
bi::address remoteAddress = m_socket.remote_endpoint().address(); bi::address remoteAddress = m_socket->remote_endpoint().address();
// Port defaults to 0 - we let the hello tell us which port the peer listens to // Port defaults to 0 - we let the hello tell us which port the peer listens to
auto p = std::make_shared<Session>(this, std::move(m_socket), bi::tcp::endpoint(remoteAddress, 0)); auto p = std::make_shared<Session>(this, std::move(*m_socket), bi::tcp::endpoint(remoteAddress, 0));
p->start(); p->start();
} }
catch (Exception const& _e) catch (Exception const& _e)

4
libp2p/Host.h

@ -214,8 +214,8 @@ private:
int m_listenPort = NetworkStopped; ///< What port are we listening on? int m_listenPort = NetworkStopped; ///< What port are we listening on?
std::unique_ptr<ba::io_service> m_ioService; ///< IOService for network stuff. std::unique_ptr<ba::io_service> m_ioService; ///< IOService for network stuff.
bi::tcp::acceptor m_acceptor; ///< Listening acceptor. std::unique_ptr<bi::tcp::acceptor> m_acceptor; ///< Listening acceptor.
bi::tcp::socket m_socket; ///< Listening socket. std::unique_ptr<bi::tcp::socket> m_socket; ///< Listening socket.
UPnP* m_upnp = nullptr; ///< UPnP helper. UPnP* m_upnp = nullptr; ///< UPnP helper.
bi::tcp::endpoint m_public; ///< Our public listening endpoint. bi::tcp::endpoint m_public; ///< Our public listening endpoint.

Loading…
Cancel
Save