Skip to content

Commit cef4494

Browse files
committed
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.
1 parent 381b25d commit cef4494

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/rpcserver.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static ssl::context* rpc_ssl_context = NULL;
3939
static boost::thread_group* rpc_worker_group = NULL;
4040
static boost::asio::io_service::work *rpc_dummy_work = NULL;
4141
static std::vector<CSubNet> rpc_allow_subnets; //!< List of subnets to allow RPC connections from
42+
static std::vector< boost::shared_ptr<ip::tcp::acceptor> > rpc_acceptors;
4243

4344
void RPCTypeCheck(const Array& params,
4445
const list<Value_type>& typesExpected,
@@ -593,12 +594,13 @@ void StartRPCThreads()
593594
asio::ip::address bindAddress = loopback ? asio::ip::address_v6::loopback() : asio::ip::address_v6::any();
594595
ip::tcp::endpoint endpoint(bindAddress, GetArg("-rpcport", Params().RPCPort()));
595596
boost::system::error_code v6_only_error;
596-
boost::shared_ptr<ip::tcp::acceptor> acceptor(new ip::tcp::acceptor(*rpc_io_service));
597597

598598
bool fListening = false;
599599
std::string strerr;
600600
try
601601
{
602+
boost::shared_ptr<ip::tcp::acceptor> acceptor(new ip::tcp::acceptor(*rpc_io_service));
603+
rpc_acceptors.push_back(acceptor);
602604
acceptor->open(endpoint.protocol());
603605
acceptor->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
604606

@@ -616,15 +618,15 @@ void StartRPCThreads()
616618
{
617619
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());
618620
}
619-
620621
try {
621622
// If dual IPv6/IPv4 failed (or we're opening loopback interfaces only), open IPv4 separately
622623
if (!fListening || loopback || v6_only_error)
623624
{
624625
bindAddress = loopback ? asio::ip::address_v4::loopback() : asio::ip::address_v4::any();
625626
endpoint.address(bindAddress);
626627

627-
acceptor.reset(new ip::tcp::acceptor(*rpc_io_service));
628+
boost::shared_ptr<ip::tcp::acceptor> acceptor(new ip::tcp::acceptor(*rpc_io_service));
629+
rpc_acceptors.push_back(acceptor);
628630
acceptor->open(endpoint.protocol());
629631
acceptor->set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
630632
acceptor->bind(endpoint);
@@ -668,7 +670,16 @@ void StopRPCThreads()
668670
{
669671
if (rpc_io_service == NULL) return;
670672

673+
// First, cancel all timers and acceptors
674+
// This is not done automatically by ->stop(), and in some cases the destructor of
675+
// asio::io_service can hang if this is skipped.
676+
BOOST_FOREACH(const boost::shared_ptr<ip::tcp::acceptor> &acceptor, rpc_acceptors)
677+
acceptor->cancel();
678+
rpc_acceptors.clear();
679+
BOOST_FOREACH(const PAIRTYPE(std::string, boost::shared_ptr<deadline_timer>) &timer, deadlineTimers)
680+
timer.second->cancel();
671681
deadlineTimers.clear();
682+
672683
rpc_io_service->stop();
673684
if (rpc_worker_group != NULL)
674685
rpc_worker_group->join_all();

0 commit comments

Comments
 (0)