From 79f9f3a1ecef1f182faac3b4f700ef756c8316bf Mon Sep 17 00:00:00 2001 From: Cory Perry Date: Mon, 1 Jan 2018 10:36:02 -0800 Subject: [PATCH] Fix segfault and deadlock in CUDAMiner. * Add stopWorking() to destructor before pause() to prevent workLoop from continuing on deleted members. * Scope minerWork lock to just the m_miners member, preventing deadlock with collectHashRate. * Fix race with hashrateTimer by canceling and removing after serviceThread is joined to prevent retriggering the timer in serviceThread. * Add boost::system to target_link_libraries for devcore for those that don't compile with stratum. --- libdevcore/CMakeLists.txt | 2 +- libethash-cuda/CUDAMiner.cpp | 3 ++- libethcore/Farm.h | 16 +++++++++------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/libdevcore/CMakeLists.txt b/libdevcore/CMakeLists.txt index bcea5399da..a85ce7b562 100644 --- a/libdevcore/CMakeLists.txt +++ b/libdevcore/CMakeLists.txt @@ -4,5 +4,5 @@ file(GLOB SOURCES "*.cpp") find_package(Threads) add_library(devcore ${SOURCES} ${HEADERS}) -target_link_libraries(devcore PUBLIC Boost::boost) +target_link_libraries(devcore PUBLIC Boost::boost Boost::system) target_link_libraries(devcore PRIVATE Threads::Threads) diff --git a/libethash-cuda/CUDAMiner.cpp b/libethash-cuda/CUDAMiner.cpp index e716cea624..d563144f46 100644 --- a/libethash-cuda/CUDAMiner.cpp +++ b/libethash-cuda/CUDAMiner.cpp @@ -96,6 +96,7 @@ CUDAMiner::CUDAMiner(FarmFace& _farm, unsigned _index) : CUDAMiner::~CUDAMiner() { + stopWorking(); pause(); delete m_miner; delete m_hook; @@ -189,7 +190,7 @@ void CUDAMiner::workLoop() if (current.exSizeBits >= 0) startN = current.startNonce | ((uint64_t)index << (64 - 4 - current.exSizeBits)); // this can support up to 16 devices m_miner->search(current.header.data(), upper64OfBoundary, *m_hook, (current.exSizeBits >= 0), startN); - + // Check if we should stop. if (shouldStop()) { diff --git a/libethcore/Farm.h b/libethcore/Farm.h index aa7ce50acf..5b95b2dc21 100644 --- a/libethcore/Farm.h +++ b/libethcore/Farm.h @@ -135,17 +135,19 @@ class Farm: public FarmFace */ void stop() { - Guard l(x_minerWork); - m_miners.clear(); - m_isMining = false; - - if (p_hashrateTimer) { - p_hashrateTimer->cancel(); + { + Guard l(x_minerWork); + m_miners.clear(); + m_isMining = false; } m_io_service.stop(); m_serviceThread.join(); - p_hashrateTimer = nullptr; + + if (p_hashrateTimer) { + p_hashrateTimer->cancel(); + p_hashrateTimer = nullptr; + } } void collectHashRate()