aboutsummaryrefslogtreecommitdiff
path: root/src/index
diff options
context:
space:
mode:
authorRyan Ofsky <ryan@ofsky.org>2024-04-17 17:24:05 -0300
committerfurszy <matiasfurszyfer@protonmail.com>2024-04-17 17:24:05 -0300
commit65951e0418c53cbbf30b9ee85e24ccaf729088a1 (patch)
tree92b5ce92bc8c4fe25ce66514c5a8df4e858fcd29 /src/index
parent3f6a6da3b08da15c10cbf2806f672da4a57254f6 (diff)
downloadbitcoin-65951e0418c53cbbf30b9ee85e24ccaf729088a1.tar.xz
index: race fix, lock cs_main while 'm_synced' is subject to change
This ensures that the index does not miss any 'new block' signals occurring in-between reading the 'next block' and setting 'm_synced'. Because, if this were to happen, the ignored blocks would never be indexed, thus stalling the index forever.
Diffstat (limited to 'src/index')
-rw-r--r--src/index/base.cpp16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/index/base.cpp b/src/index/base.cpp
index a203ce4a9f..e66c89f9e4 100644
--- a/src/index/base.cpp
+++ b/src/index/base.cpp
@@ -160,12 +160,24 @@ void BaseIndex::Sync()
}
const CBlockIndex* pindex_next = WITH_LOCK(cs_main, return NextSyncBlock(pindex, m_chainstate->m_chain));
+ // If pindex_next is null, it means pindex is the chain tip, so
+ // commit data indexed so far.
if (!pindex_next) {
SetBestBlockIndex(pindex);
// No need to handle errors in Commit. See rationale above.
Commit();
- m_synced = true;
- break;
+
+ // If pindex is still the chain tip after committing, exit the
+ // sync loop. It is important for cs_main to be locked while
+ // setting m_synced = true, otherwise a new block could be
+ // attached while m_synced is still false, and it would not be
+ // indexed.
+ LOCK(::cs_main);
+ pindex_next = NextSyncBlock(pindex, m_chainstate->m_chain);
+ if (!pindex_next) {
+ m_synced = true;
+ break;
+ }
}
if (pindex_next->pprev != pindex && !Rewind(pindex, pindex_next->pprev)) {
FatalErrorf("%s: Failed to rewind index %s to a previous chain tip", __func__, GetName());