Fixed potential deadlock

Can't lock `m_minerLock` inside SideChain update
This commit is contained in:
SChernykh 2022-07-14 13:14:20 +02:00
parent 0e7c1aa481
commit 3c697c2d7e
3 changed files with 17 additions and 16 deletions

View file

@ -596,13 +596,15 @@ void p2pool::submit_sidechain_block(uint32_t template_id, uint32_t nonce, uint32
m_blockTemplate->submit_sidechain_block(template_id, nonce, extra_nonce);
}
void p2pool::update_block_template_async()
void p2pool::update_block_template_async(bool is_alternative_block)
{
// If p2pool is stopped, m_blockTemplateAsync is most likely already closed
if (m_stopped) {
return;
}
m_isAlternativeBlock = is_alternative_block;
const int err = uv_async_send(&m_blockTemplateAsync);
if (err) {
LOGERR(1, "uv_async_send failed, error " << uv_err_name(err));
@ -620,6 +622,16 @@ void p2pool::update_block_template()
m_blockTemplate->update(data, *m_mempool, &m_params->m_wallet);
stratum_on_block();
api_update_pool_stats();
#ifdef WITH_RANDOMX
if (m_isAlternativeBlock.exchange(false)) {
MutexLock lock(m_minerLock);
if (m_miner) {
m_miner->reset_share_counters();
}
}
#endif
}
void p2pool::download_block_headers(uint64_t current_height)
@ -1373,15 +1385,6 @@ void p2pool::stop_mining()
m_miner = nullptr;
}
}
void p2pool::reset_miner()
{
MutexLock lock(m_minerLock);
if (m_miner) {
m_miner->reset_share_counters();
}
}
#endif
static void on_signal(uv_signal_t* handle, int signum)

View file

@ -77,7 +77,7 @@ public:
void submit_block_async(const std::vector<uint8_t>& blob);
void submit_sidechain_block(uint32_t template_id, uint32_t nonce, uint32_t extra_nonce);
void update_block_template_async();
void update_block_template_async(bool is_alternative_block = false);
void update_block_template();
void download_block_headers(uint64_t current_height);
@ -91,7 +91,6 @@ public:
#ifdef WITH_RANDOMX
void start_mining(uint32_t threads);
void stop_mining();
void reset_miner();
#endif
uint64_t zmq_last_active() const { return m_zmqLastActive; }
@ -198,6 +197,8 @@ private:
uv_async_t m_blockTemplateAsync;
uv_async_t m_stopAsync;
std::atomic<bool> m_isAlternativeBlock{ false };
std::atomic<uint64_t> m_zmqLastActive;
uint64_t m_startTime;
uv_async_t m_restartZMQAsync;

View file

@ -1539,7 +1539,7 @@ void SideChain::update_chain_tip(const PoolBlock* block)
block->m_wantBroadcast = true;
if (m_pool) {
m_pool->update_block_template_async();
m_pool->update_block_template_async(is_alternative);
// Reset stratum share counters when switching to an alternative chain to avoid confusion
if (is_alternative) {
@ -1547,9 +1547,6 @@ void SideChain::update_chain_tip(const PoolBlock* block)
if (s) {
s->reset_share_counters();
}
#ifdef WITH_RANDOMX
m_pool->reset_miner();
#endif
LOGINFO(0, log::LightCyan() << "SYNCHRONIZED");
}
}