diff --git a/src/asyncrpcoperation.cpp b/src/asyncrpcoperation.cpp index 0579ca1f6..d09ea34e1 100644 --- a/src/asyncrpcoperation.cpp +++ b/src/asyncrpcoperation.cpp @@ -32,7 +32,9 @@ using namespace std; static boost::uuids::random_generator uuidgen; -static std::map OperationStatusMap = { +// const and read with at(): getStatus() runs on several RPC threads at once, and operator[] of a +// std::map is not one of the calls the standard guarantees safe for concurrent use. +static const std::map OperationStatusMap = { {OperationStatus::READY, "queued"}, {OperationStatus::EXECUTING, "executing"}, {OperationStatus::CANCELLED, "cancelled"}, @@ -194,7 +196,7 @@ UniValue AsyncRPCOperation::getStatus() const { OperationStatus status = this->getState(); UniValue obj(UniValue::VOBJ); obj.push_back(Pair("id", this->id_)); - obj.push_back(Pair("status", OperationStatusMap[status])); + obj.push_back(Pair("status", OperationStatusMap.at(status))); obj.push_back(Pair("creation_time", this->creation_time_)); // TODO: Issue #1354: There may be other useful metadata to return to the user. UniValue err = this->getError(); @@ -218,5 +220,5 @@ UniValue AsyncRPCOperation::getStatus() const { */ std::string AsyncRPCOperation::getStateAsString() const { OperationStatus status = this->getState(); - return OperationStatusMap[status]; + return OperationStatusMap.at(status); } diff --git a/src/net.cpp b/src/net.cpp index 363930fc9..a35d3bea6 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2974,6 +2974,17 @@ size_t GetNodeCount(NumConnections flags) return nNum; } +void GetNodeCounts(int& nAll, int& nTls) +{ + LOCK(cs_vNodes); + nAll = (int)vNodes.size(); + nTls = 0; + for (const CNode* pnode : vNodes) { + if (pnode->ssl != NULL) + nTls++; + } +} + void SetNetworkActive(bool active) { LogPrint("net", "SetNetworkActive: %s\n", active); diff --git a/src/net.h b/src/net.h index b70230990..fd6b71181 100644 --- a/src/net.h +++ b/src/net.h @@ -173,6 +173,8 @@ enum NumConnections { }; size_t GetNodeCount(NumConnections num); +// All peers and TLS peers, counted under one hold of cs_vNodes so that nTls <= nAll. +void GetNodeCounts(int& nAll, int& nTls); bool GetNetworkActive(); void SetNetworkActive(bool active); diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index e17dbb177..0eef8f3ca 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -132,6 +132,11 @@ UniValue getinfo(const UniValue& params, bool fHelp, const CPubKey& mypk) + HelpExampleRpc("getinfo", "") ); + // vNodes is guarded by cs_vNodes, not cs_main: the socket thread adds and erases peers while we + // read it. Take the counts before cs_main so the two locks are never held together. + int nConnections = 0, nTlsConnections = 0; + GetNodeCounts(nConnections, nTlsConnections); + #ifdef ENABLE_WALLET LOCK2(cs_main, pwalletMain ? &pwalletMain->cs_wallet : NULL); #else @@ -175,8 +180,8 @@ UniValue getinfo(const UniValue& params, bool fHelp, const CPubKey& mypk) } //obj.push_back(Pair("build_date", BUILD_DATE)); obj.push_back(Pair("timeoffset", 0)); - obj.push_back(Pair("connections", (int)vNodes.size())); - obj.push_back(Pair("tls_connections", (int)std::count_if(vNodes.begin(), vNodes.end(), [](CNode* n) {return n->ssl != NULL;}))); + obj.push_back(Pair("connections", nConnections)); + obj.push_back(Pair("tls_connections", nTlsConnections)); obj.push_back(Pair("proxy", (proxy.IsValid() ? proxy.proxy.ToStringIPPort() : string()))); obj.push_back(Pair("testnet", Params().TestnetToBeDeprecatedFieldRPC())); obj.push_back(Pair("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK()))); diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 62a04f45b..cdc4a0c68 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -533,6 +533,10 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp, const CPubKey& mypk) + HelpExampleRpc("getnetworkinfo", "") ); + // vNodes is guarded by cs_vNodes, not cs_main (see getinfo). + int nConnections = 0, nTlsConnections = 0; + GetNodeCounts(nConnections, nTlsConnections); + LOCK(cs_main); UniValue obj(UniValue::VOBJ); @@ -541,8 +545,8 @@ UniValue getnetworkinfo(const UniValue& params, bool fHelp, const CPubKey& mypk) obj.push_back(Pair("protocolversion",PROTOCOL_VERSION)); obj.push_back(Pair("localservices", strprintf("%016x", nLocalServices))); obj.push_back(Pair("timeoffset", 0)); - obj.push_back(Pair("connections", (int)vNodes.size())); - obj.push_back(Pair("tls_connections", (int)std::count_if(vNodes.begin(), vNodes.end(), [](CNode* n) {return n->ssl != NULL;}))); + obj.push_back(Pair("connections", nConnections)); + obj.push_back(Pair("tls_connections", nTlsConnections)); obj.push_back(Pair("networks", GetNetworksInfo())); obj.push_back(Pair("relayfee", ValueFromAmount(::minRelayTxFee.GetFeePerK()))); UniValue localAddresses(UniValue::VARR); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 958bcb4ca..d09f0fad4 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5288,7 +5288,11 @@ UniValue z_getoperationresult(const UniValue& params, bool fHelp, const CPubKey& + HelpExampleRpc("z_getoperationresult", "'[\"operationid\", ... ]'") ); - // This call will remove finished operations + // This call will remove finished operations. It keeps cs_main + cs_wallet, which + // z_getoperationstatus no longer takes: the wallet schedulers (RunSaplingSweep and friends, + // z_consolidate) cancel and pop their previous operation under cs_wallet, and this keeps the + // removal serialised with them and with other z_getoperationresult callers, as before. + LOCK2(cs_main, pwalletMain->cs_wallet); return z_getoperationstatus_IMPL(params, true); } @@ -5317,7 +5321,11 @@ UniValue z_getoperationstatus(const UniValue& params, bool fHelp, const CPubKey& UniValue z_getoperationstatus_IMPL(const UniValue& params, bool fRemoveFinishedOperations=false) { - LOCK2(cs_main, pwalletMain->cs_wallet); + // No cs_main/cs_wallet here (the removing caller, z_getoperationresult, takes them itself). + // Nothing below reads chain or wallet state: AsyncRPCQueue guards its map with its own mutex, + // and getStatus() (base and every override) reads only the atomic state_, members fixed at + // construction, and result/error under the operation's own lock_. Holding the wallet locks + // only made a GUI's status poll wait behind block connection and wallet scans. std::set filter; if (params.size()==1) { @@ -6454,7 +6462,8 @@ UniValue z_listoperationids(const UniValue& params, bool fHelp, const CPubKey& m + HelpExampleRpc("z_listoperationids", "") ); - LOCK2(cs_main, pwalletMain->cs_wallet); + // No cs_main/cs_wallet: like z_getoperationstatus, this only reads the self-locking async + // queue and each operation's atomic state. std::string filter; bool useFilter = false;