Fixed potential deadlock

Can't lock `m_minerLock` inside SideChain update
pull/189/head
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); 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 p2pool is stopped, m_blockTemplateAsync is most likely already closed
if (m_stopped) { if (m_stopped) {
return; return;
} }
m_isAlternativeBlock = is_alternative_block;
const int err = uv_async_send(&m_blockTemplateAsync); const int err = uv_async_send(&m_blockTemplateAsync);
if (err) { if (err) {
LOGERR(1, "uv_async_send failed, error " << uv_err_name(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); m_blockTemplate->update(data, *m_mempool, &m_params->m_wallet);
stratum_on_block(); stratum_on_block();
api_update_pool_stats(); 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) void p2pool::download_block_headers(uint64_t current_height)
@ -1373,15 +1385,6 @@ void p2pool::stop_mining()
m_miner = nullptr; m_miner = nullptr;
} }
} }
void p2pool::reset_miner()
{
MutexLock lock(m_minerLock);
if (m_miner) {
m_miner->reset_share_counters();
}
}
#endif #endif
static void on_signal(uv_signal_t* handle, int signum) 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_block_async(const std::vector<uint8_t>& blob);
void submit_sidechain_block(uint32_t template_id, uint32_t nonce, uint32_t extra_nonce); 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 update_block_template();
void download_block_headers(uint64_t current_height); void download_block_headers(uint64_t current_height);
@ -91,7 +91,6 @@ public:
#ifdef WITH_RANDOMX #ifdef WITH_RANDOMX
void start_mining(uint32_t threads); void start_mining(uint32_t threads);
void stop_mining(); void stop_mining();
void reset_miner();
#endif #endif
uint64_t zmq_last_active() const { return m_zmqLastActive; } uint64_t zmq_last_active() const { return m_zmqLastActive; }
@ -198,6 +197,8 @@ private:
uv_async_t m_blockTemplateAsync; uv_async_t m_blockTemplateAsync;
uv_async_t m_stopAsync; uv_async_t m_stopAsync;
std::atomic<bool> m_isAlternativeBlock{ false };
std::atomic<uint64_t> m_zmqLastActive; std::atomic<uint64_t> m_zmqLastActive;
uint64_t m_startTime; uint64_t m_startTime;
uv_async_t m_restartZMQAsync; uv_async_t m_restartZMQAsync;

View File

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