Skip to content

Commit 55a1db4

Browse files
committed
Solve chainActive-related locking issues
- In wallet and GUI code LOCK cs_main as well as cs_wallet when necessary - In main.cpp SendMessages move the TRY_LOCK(cs_main) up, to encompass the call to IsInitialBlockDownload. - Make ActivateBestChain, AddToBlockIndex, IsInitialBlockDownload, InitBlockIndex acquire the cs_main lock Fixes #3997
1 parent e07c943 commit 55a1db4

File tree

7 files changed

+252
-255
lines changed

7 files changed

+252
-255
lines changed

src/main.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,7 @@ int GetNumBlocksOfPeers()
13101310

13111311
bool IsInitialBlockDownload()
13121312
{
1313-
AssertLockHeld(cs_main);
1313+
LOCK(cs_main);
13141314
if (fImporting || fReindex || chainActive.Height() < Checkpoints::GetTotalBlocksEstimate())
13151315
return true;
13161316
static int64_t nLastUpdate;
@@ -2087,7 +2087,7 @@ void static FindMostWorkChain() {
20872087

20882088
// Try to activate to the most-work chain (thereby connecting it).
20892089
bool ActivateBestChain(CValidationState &state) {
2090-
AssertLockHeld(cs_main);
2090+
LOCK(cs_main);
20912091
CBlockIndex *pindexOldTip = chainActive.Tip();
20922092
bool fComplete = false;
20932093
while (!fComplete) {
@@ -2136,7 +2136,6 @@ bool ActivateBestChain(CValidationState &state) {
21362136

21372137
bool AddToBlockIndex(CBlock& block, CValidationState& state, const CDiskBlockPos& pos)
21382138
{
2139-
AssertLockHeld(cs_main);
21402139
// Check for duplicate
21412140
uint256 hash = block.GetHash();
21422141
if (mapBlockIndex.count(hash))
@@ -2173,6 +2172,7 @@ bool AddToBlockIndex(CBlock& block, CValidationState& state, const CDiskBlockPos
21732172
if (!ActivateBestChain(state))
21742173
return false;
21752174

2175+
LOCK(cs_main);
21762176
if (pindexNew == chainActive.Tip())
21772177
{
21782178
// Clear fork warning if its no longer applicable
@@ -2962,6 +2962,7 @@ bool LoadBlockIndex()
29622962

29632963

29642964
bool InitBlockIndex() {
2965+
LOCK(cs_main);
29652966
// Check whether we're already initialized
29662967
if (chainActive.Genesis() != NULL)
29672968
return true;
@@ -4201,6 +4202,10 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
42014202
}
42024203
}
42034204

4205+
TRY_LOCK(cs_main, lockMain); // Acquire cs_main for IsInitialBlockDownload() and CNodeState()
4206+
if (!lockMain)
4207+
return true;
4208+
42044209
// Address refresh broadcast
42054210
static int64_t nLastRebroadcast;
42064211
if (!IsInitialBlockDownload() && (GetTime() - nLastRebroadcast > 24 * 60 * 60))
@@ -4251,10 +4256,6 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
42514256
pto->PushMessage("addr", vAddr);
42524257
}
42534258

4254-
TRY_LOCK(cs_main, lockMain);
4255-
if (!lockMain)
4256-
return true;
4257-
42584259
CNodeState &state = *State(pto->GetId());
42594260
if (state.fShouldBan) {
42604261
if (pto->addr.IsLocal())

src/qt/clientmodel.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ int ClientModel::getNumConnections(unsigned int flags) const
5555

5656
int ClientModel::getNumBlocks() const
5757
{
58+
LOCK(cs_main);
5859
return chainActive.Height();
5960
}
6061

@@ -76,6 +77,7 @@ quint64 ClientModel::getTotalBytesSent() const
7677

7778
QDateTime ClientModel::getLastBlockDate() const
7879
{
80+
LOCK(cs_main);
7981
if (chainActive.Tip())
8082
return QDateTime::fromTime_t(chainActive.Tip()->GetBlockTime());
8183
else
@@ -84,6 +86,7 @@ QDateTime ClientModel::getLastBlockDate() const
8486

8587
double ClientModel::getVerificationProgress() const
8688
{
89+
LOCK(cs_main);
8790
return Checkpoints::GuessVerificationProgress(chainActive.Tip());
8891
}
8992

0 commit comments

Comments
 (0)