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:
+7
-2
@@ -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())));
|
||||
|
||||
Reference in New Issue
Block a user