diff options
| author | Cory Fields <[email protected]> | 2017-02-06 02:34:57 -0500 |
|---|---|---|
| committer | Matt Corallo <[email protected]> | 2017-02-10 11:32:39 -0500 |
| commit | 321d0fc6b6624c65508f8b9059418cb936f0bbbe (patch) | |
| tree | 57a99a1ede16e5449db2d230ddbc7a3b561547be /src/net.cpp | |
| parent | Merge #9698: net: fix socket close race (diff) | |
| download | discoin-321d0fc6b6624c65508f8b9059418cb936f0bbbe.tar.xz discoin-321d0fc6b6624c65508f8b9059418cb936f0bbbe.zip | |
net: fix a few races. Credit @TheBlueMatt
These are (afaik) all long-standing races or concurrent accesses. Going
forward, we can clean these up so that they're not all individual atomic
accesses.
- Reintroduce cs_vRecv to guard receive-specific vars
- Lock vRecv/vSend for CNodeStats
- Make some vars atomic.
- Only set the connection time in CNode's constructor so that it doesn't change
Diffstat (limited to 'src/net.cpp')
| -rw-r--r-- | src/net.cpp | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/src/net.cpp b/src/net.cpp index 7c45cff1d..c96ca469f 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -389,7 +389,6 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo uint64_t nonce = GetDeterministicRandomizer(RANDOMIZER_ID_LOCALHOSTNONCE).Write(id).Finalize(); CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addrConnect, CalculateKeyedNetGroup(addrConnect), nonce, pszDest ? pszDest : "", false); pnode->nServicesExpected = ServiceFlags(addrConnect.nServices & nRelevantServices); - pnode->nTimeConnected = GetSystemTimeInSeconds(); pnode->AddRef(); return pnode; @@ -612,10 +611,16 @@ void CNode::copyStats(CNodeStats &stats) X(fInbound); X(fAddnode); X(nStartingHeight); - X(nSendBytes); - X(mapSendBytesPerMsgCmd); - X(nRecvBytes); - X(mapRecvBytesPerMsgCmd); + { + LOCK(cs_vSend); + X(mapSendBytesPerMsgCmd); + X(nSendBytes); + } + { + LOCK(cs_vRecv); + X(mapRecvBytesPerMsgCmd); + X(nRecvBytes); + } X(fWhitelisted); // It is common for nodes with good ping times to suddenly become lagged, @@ -643,6 +648,7 @@ bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete { complete = false; int64_t nTimeMicros = GetTimeMicros(); + LOCK(cs_vRecv); nLastRecv = nTimeMicros / 1000000; nRecvBytes += nBytes; while (nBytes > 0) { |