Using AddRef() in ConnectNode() for existing connections doesn't feel right imo considering how refs are released in ThreadSocketHandler() https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp#L1113. I guess this could be the reason that sometimes refs stay >0 no matter what and nodes stuck in vNodesDisconnected forever which means that node never get deleted and FinalizeNode signal is never fired which in its turn means that for example mapBlocksInFlight can't be cleaned properly and #7596 happens.
This commit should solve the issue by:
- removing
AddRef()for existing connections - adding
AddRef()inCNode's constructor using the same conditions as inThreadSocketHandler()