aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2014-05-09 10:01:50 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2014-05-12 17:59:28 +0200
commit9f535d4104c1d446b9f4a4d572d052a9641d12f6 (patch)
tree482b09895d92bc8a4755659fd75754596fbc7446
parent8b1a93f2e53c3f861cacb9ee514273c8ce2e68d0 (diff)
downloadbitcoin-9f535d4104c1d446b9f4a4d572d052a9641d12f6.tar.xz
rpc: keep track of acceptors, and cancel them in StopRPCThreads
Fixes #4156. The problem is that the boost::asio::io_service destructor waits for the acceptors to finish (on windows, and boost 1.55). Fix this by keeping track of the acceptors and cancelling them before stopping the event loops. Rebased-By: Wladimir J. van der Laan <laanwj@gmail.com> Rebased-From: cef4494
-rw-r--r--src/rpcserver.cpp17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp
index 41a1d6d96a..4293deb157 100644
--- a/src/rpcserver.cpp
+++ b/src/rpcserver.cpp
@@ -38,6 +38,7 @@ static map<string, boost::shared_ptr<deadline_timer> > deadlineTimers;
static ssl::context* rpc_ssl_context = NULL;
static boost::thread_group* rpc_worker_group = NULL;
static boost::asio::io_service::work *rpc_dummy_work = NULL;
+static std::vector< boost::shared_ptr<ip::tcp::acceptor> > rpc_acceptors;
void RPCTypeCheck(const Array& params,
const list<Value_type>& typesExpected,
@@ -559,12 +560,13 @@ void StartRPCThreads()
asio::ip::address bindAddress = loopback ? asio::ip::address_v6::loopback() : asio::ip::address_v6::any();
ip::tcp::endpoint endpoint(bindAddress, GetArg("-rpcport", Params().RPCPort()));
boost::system::error_code v6_only_error;
- boost::shared_ptr<ip::tcp::acceptor> acceptor(new ip::tcp::acceptor(*rpc_io_service));
bool fListening = false;
std::string strerr;
try
{
+ boost::shared_ptr<ip::tcp::acceptor> acceptor(new ip::tcp::acceptor(*rpc_io_service));
+ rpc_acceptors.push_back(acceptor);
acceptor->open(endpoint.protocol());
acceptor->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
@@ -582,7 +584,6 @@ void StartRPCThreads()
{
strerr = strprintf(_("An error occurred while setting up the RPC port %u for listening on IPv6, falling back to IPv4: %s"), endpoint.port(), e.what());
}
-
try {
// If dual IPv6/IPv4 failed (or we're opening loopback interfaces only), open IPv4 separately
if (!fListening || loopback || v6_only_error)
@@ -590,7 +591,8 @@ void StartRPCThreads()
bindAddress = loopback ? asio::ip::address_v4::loopback() : asio::ip::address_v4::any();
endpoint.address(bindAddress);
- acceptor.reset(new ip::tcp::acceptor(*rpc_io_service));
+ boost::shared_ptr<ip::tcp::acceptor> acceptor(new ip::tcp::acceptor(*rpc_io_service));
+ rpc_acceptors.push_back(acceptor);
acceptor->open(endpoint.protocol());
acceptor->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
acceptor->bind(endpoint);
@@ -634,7 +636,16 @@ void StopRPCThreads()
{
if (rpc_io_service == NULL) return;
+ // First, cancel all timers and acceptors
+ // This is not done automatically by ->stop(), and in some cases the destructor of
+ // asio::io_service can hang if this is skipped.
+ BOOST_FOREACH(const boost::shared_ptr<ip::tcp::acceptor> &acceptor, rpc_acceptors)
+ acceptor->cancel();
+ rpc_acceptors.clear();
+ BOOST_FOREACH(const PAIRTYPE(std::string, boost::shared_ptr<deadline_timer>) &timer, deadlineTimers)
+ timer.second->cancel();
deadlineTimers.clear();
+
rpc_io_service->stop();
if (rpc_worker_group != NULL)
rpc_worker_group->join_all();