Skip to content

Commit 694eb59

Browse files
committed
Merge #2923: [Bug] Fix wallet data race
f9a6ebd fix: solve data race by making nTimeBestReceived atomic (ale) 4331c30 refactor: move nTimeBestReceived to CWallet (ale) Pull request description: First commit is a small refactor: the global variable `nTimeBestReceived` is now a private member of the `CWallet` class, as it was used only there. Second commit: fix the following data race by making `nTimeBestReceived` atomic ``` WARNING: ThreadSanitizer: data race (pid=18270) Write of size 8 at 0x7b6800000ed8 by thread T16: #0 CWallet::UpdatedBlockTip(CBlockIndex const*, CBlockIndex const*, bool) wallet/wallet.cpp:2094 (pivxd+0x5058c9) #1 void std::__invoke_impl<void, void (CValidationInterface::*&)(CBlockIndex const*, CBlockIndex const*, bool), CValidationInterface*&, CBlockIndex const*, CBlockIndex const*, bool>(std::__invoke_memfun_deref, void (CValidationInterface::*&)(CBlockIndex const*, CBlockIndex const*, bool), CValidationInterface*&, CBlockIndex const*&&, CBlockIndex const*&&, bool&&) /usr/include/c++/12/bits/invoke.h:74 (pivxd+0x2ee1c0) #2 std::__invoke_result<void (CValidationInterface::*&)(CBlockIndex const*, CBlockIndex const*, bool), CValidationInterface*&, CBlockIndex const*, CBlockIndex const*, bool>::type std::__invoke<void (CValidationInterface::*&)(CBlockIndex const*, CBlockIndex const*, bool), CValidationInterface*&, CBlockIndex const*, CBlockIndex const*, bool>(void (CValidationInterface::*&)(CBlockIndex const*, CBlockIndex const*, bool), CValidationInterface*&, CBlockIndex const*&&, CBlockIndex const*&&, bool&&) /usr/include/c++/12/bits/invoke.h:96 (pivxd+0x2ee25a) #3 void std::_Bind<void (CValidationInterface::*(CValidationInterface*, std::_Placeholder<1>, std::_Placeholder<2>, std::_Placeholder<3>))(CBlockIndex const*, CBlockIndex const*, bool)>::__call<void, CBlockIndex const*&&, CBlockIndex const*&&, bool&&, 0ul, 1ul, 2ul, 3ul>(std::tuple<CBlockIndex const*&&, CBlockIndex const*&&, bool&&>&&, std::_Index_tuple<0ul, 1ul, 2ul, 3ul>) /usr/include/c++/12/functional:484 (pivxd+0x2ee25a) ... Previous read of size 8 at 0x7b6800000ed8 by thread T35 (mutexes: write M134298, write M132880): #0 CWallet::ResendWalletTransactions(CConnman*) wallet/wallet.cpp:2065 (pivxd+0x515b25) #1 void std::__invoke_impl<void, void (CValidationInterface::*&)(CConnman*), CValidationInterface*&, CConnman*>(std::__invoke_memfun_deref, void (CValidationInterface::*&)(CConnman*), CValidationInterface*&, CConnman*&&) /usr/include/c++/12/bits/invoke.h:74 (pivxd+0x2eef45) #2 std::__invoke_result<void (CValidationInterface::*&)(CConnman*), CValidationInterface*&, CConnman*>::type std::__invoke<void (CValidationInterface::*&)(CConnman*), CValidationInterface*&, CConnman*>(void (CValidationInterface::*&)(CConnman*), CValidationInterface*&, CConnman*&&) /usr/include/c++/12/bits/invoke.h:96 (pivxd+0x2eefb9) #3 void std::_Bind<void (CValidationInterface::*(CValidationInterface*, std::_Placeholder<1>))(CConnman*)>::__call<void, CConnman*&&, 0ul, 1ul>(std::tuple<CConnman*&&>&&, std::_Index_tuple<0ul, 1ul>) /usr/include/c++/12/functional:484 (pivxd+0x2eefb9) ... ``` ACKs for top commit: f9a6ebd Duddino: utACK f9a6ebd Liquid369: tACK f9a6ebd Fuzzbawls: ACK f9a6ebd Tree-SHA512: c1de883956bf0958204b05c338831c27f694e6453e74b77e3347ae12a3cbd69df2fe1120bae3fca72dab14587c0c499c4f92d5c53943305855e66fc1275ab4fa
2 parents db78a7e + f9a6ebd commit 694eb59

File tree

4 files changed

+7
-4
lines changed

4 files changed

+7
-4
lines changed

src/net_processing.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
using namespace std::chrono_literals;
3636

37-
int64_t nTimeBestReceived = 0; // Used only to inform the wallet of when we last received a block
3837

3938
static const uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL; // SHA256("main address relay")[0:8]
4039

@@ -757,8 +756,6 @@ void PeerLogicValidation::UpdatedBlockTip(const CBlockIndex* pindexNew, const CB
757756
}
758757
});
759758
}
760-
761-
nTimeBestReceived = GetTime();
762759
}
763760

764761
void PeerLogicValidation::BlockChecked(const CBlock& block, const CValidationState& state)

src/validation.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ extern BlockMap mapBlockIndex;
129129
extern PrevBlockMap mapPrevBlockIndex;
130130
extern uint64_t nLastBlockTx;
131131
extern uint64_t nLastBlockSize;
132-
extern int64_t nTimeBestReceived;
133132

134133
// Best block section
135134
extern Mutex g_best_block_mutex;

src/wallet/wallet.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,11 @@ void CWallet::ResendWalletTransactions(CConnman* connman)
20882088
}
20892089
}
20902090

2091+
void CWallet::UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload)
2092+
{
2093+
nTimeBestReceived = GetTime();
2094+
}
2095+
20912096
/** @} */ // end of mapWallet
20922097

20932098

src/wallet/wallet.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
628628

629629
int64_t nNextResend;
630630
int64_t nLastResend;
631+
std::atomic<int64_t> nTimeBestReceived{0}; // Used only to inform the wallet of when we last received a block
631632

632633
/**
633634
* Used to keep track of spent outpoints, and
@@ -1042,6 +1043,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
10421043
void TransactionRemovedFromMempool(const CTransactionRef &ptx, MemPoolRemovalReason reason) override;
10431044
void ReacceptWalletTransactions(bool fFirstLoad = false);
10441045
void ResendWalletTransactions(CConnman* connman) override;
1046+
void UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload) override;
10451047

10461048
struct Balance {
10471049
CAmount m_mine_trusted{0}; //!< Trusted, at depth=GetBalance.min_depth or more

0 commit comments

Comments
 (0)