Problem
When a node is shut down while network activity is disabled (e.g. via -networkactive=0 or setnetworkactive false), anchors.dat is overwritten with an empty list. This happens because StopNodes() collects anchors via GetCurrentBlockRelayOnlyConns(), which returns nothing when there are no active connections. The node then loses its anchors and cannot reconnect to them on the next startup.
Fix
- Guard
m_anchorswith a mutex, since it is now accessed outside the connection-opening thread. - In
SetNetworkActive(false), save the current block-relay-only connections intom_anchorsbefore the connections are closed, so the anchor list is preserved after the network is deactivated. Stored anchors are kept if there are no such connections. - In
ThreadOpenConnections(), only consume anchors while the network is active; otherwise they would be popped and dropped by the connection attempt. - In
StopNodes(), if the network is inactive andm_anchorsis non-empty, use those stored anchors as the source foranchors.datinstead of the (empty) live connection list.
Also, log that "X anchors will be tried for connections" only if the network is active.