rpc: read the peer list under cs_vNodes in getinfo/getnetworkinfo; z_getoperationstatus without cs_main

From origin/perf/wallet-scan-rpc-costs, both still on dev:

- getinfo and getnetworkinfo read vNodes.size() and iterated vNodes without cs_vNodes, racing the
  network threads that add and remove peers (a reallocation mid-iteration is undefined behaviour).
  Both now count through one helper that takes the lock.
- z_getoperationstatus and z_listoperationids took cs_main and cs_wallet, though they only read the
  async queue (which has its own mutex) and each operation's status. GUIs poll them, so every poll
  contended with block validation. Regtest: z_getoperationstatus reports a running sweep normally.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-11-20 20:10:01 -06:00
co-authored by Claude Opus 5.5
parent 3537531901
commit 93f8b3768b
6 changed files with 43 additions and 10 deletions
+5 -3
View File
@@ -32,7 +32,9 @@ using namespace std;
static boost::uuids::random_generator uuidgen;
static std::map<OperationStatus, std::string> 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<OperationStatus, std::string> 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);
}
+11
View File
@@ -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);
+2
View File
@@ -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);
+7 -2
View File
@@ -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())));
+6 -2
View File
@@ -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);
+12 -3
View File
@@ -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<AsyncRPCOperationId> 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;