From 05b0973a23360ac83eefaa5c922bc6efdfe435c0 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Fri, 3 Jun 2022 18:28:10 +0200 Subject: [PATCH] More reliable p2pool shutdown logic --- src/p2pool.cpp | 29 ++++++++++++++++++++++++++++- src/p2pool.h | 2 +- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/p2pool.cpp b/src/p2pool.cpp index c3cd102..33cdc33 100644 --- a/src/p2pool.cpp +++ b/src/p2pool.cpp @@ -413,6 +413,13 @@ void p2pool::submit_block_async(uint32_t template_id, uint32_t nonce, uint32_t e m_submitBlockData.blob.clear(); } + // If p2pool is stopped, m_submitBlockAsync is most likely already closed + if (m_stopped) { + LOGWARN(0, "p2pool is shutting down, but a block was found. Trying to submit it anyway!"); + submit_block(); + return; + } + const int err = uv_async_send(&m_submitBlockAsync); if (err) { LOGERR(1, "uv_async_send failed, error " << uv_err_name(err)); @@ -430,6 +437,13 @@ void p2pool::submit_block_async(const std::vector& blob) m_submitBlockData.blob = blob; } + // If p2pool is stopped, m_submitBlockAsync is most likely already closed + if (m_stopped) { + LOGWARN(0, "p2pool is shutting down, but a block was found. Trying to submit it anyway!"); + submit_block(); + return; + } + const int err = uv_async_send(&m_submitBlockAsync); if (err) { LOGERR(1, "uv_async_send failed, error " << uv_err_name(err)); @@ -574,6 +588,11 @@ void p2pool::submit_sidechain_block(uint32_t template_id, uint32_t nonce, uint32 void p2pool::update_block_template_async() { + // If p2pool is stopped, m_blockTemplateAsync is most likely already closed + if (m_stopped) { + return; + } + const int err = uv_async_send(&m_blockTemplateAsync); if (err) { LOGERR(1, "uv_async_send failed, error " << uv_err_name(err)); @@ -1403,11 +1422,19 @@ static bool init_signals(p2pool* pool) void p2pool::stop() { - uv_async_send(&m_stopAsync); + // Can be called only once + if (m_stopped.exchange(true) == false) { + uv_async_send(&m_stopAsync); + } } void p2pool::restart_zmq() { + // If p2pool is stopped, m_restartZMQAsync is most likely already closed + if (m_stopped) { + return; + } + if (!is_main_thread()) { uv_async_send(&m_restartZMQAsync); return; diff --git a/src/p2pool.h b/src/p2pool.h index f3dc11e..d27d04c 100644 --- a/src/p2pool.h +++ b/src/p2pool.h @@ -108,7 +108,7 @@ private: void submit_block() const; - bool m_stopped; + std::atomic m_stopped; Params* m_params;