From 42484e6434f5089d6e26815a9df05e8134fbbc4c Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Wed, 10 Feb 2021 22:02:05 +0100 Subject: [PATCH] zmc: Fix a deadlock on shutdown/reconnection First stop the analysis threads, then close the monitors and thus drain the packet queues before trying to join the analysis threads since they might hang while waiting for the next packet to arrive. --- src/zm_analysis_thread.cpp | 2 +- src/zm_analysis_thread.h | 2 ++ src/zmc.cpp | 7 +++++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/zm_analysis_thread.cpp b/src/zm_analysis_thread.cpp index 143f970ac..4f1d1506b 100644 --- a/src/zm_analysis_thread.cpp +++ b/src/zm_analysis_thread.cpp @@ -9,7 +9,7 @@ AnalysisThread::AnalysisThread(std::shared_ptr monitor) : } AnalysisThread::~AnalysisThread() { - terminate_ = true; + Stop(); if (thread_.joinable()) thread_.join(); } diff --git a/src/zm_analysis_thread.h b/src/zm_analysis_thread.h index 9c7a770f7..8cf389e93 100644 --- a/src/zm_analysis_thread.h +++ b/src/zm_analysis_thread.h @@ -13,6 +13,8 @@ class AnalysisThread { AnalysisThread(AnalysisThread &rhs) = delete; AnalysisThread(AnalysisThread &&rhs) = delete; + void Stop() { terminate_ = true; } + private: void Run(); diff --git a/src/zmc.cpp b/src/zmc.cpp index 9d0051e78..c4ebe57c4 100644 --- a/src/zmc.cpp +++ b/src/zmc.cpp @@ -384,8 +384,8 @@ int main(int argc, char *argv[]) { } // end if zm_reload } // end while ! zm_terminate and connected - // Killoff the analysis threads. Don't need them spinning while we try to reconnect - analysis_threads.clear(); + for (std::unique_ptr &analysis_thread: analysis_threads) + analysis_thread->Stop(); for (size_t i = 0; i < monitors.size(); i++) { #if HAVE_RTSP_SERVER @@ -408,6 +408,9 @@ int main(int argc, char *argv[]) { camera->Close(); } + // Killoff the analysis threads. Don't need them spinning while we try to reconnect + analysis_threads.clear(); + #if HAVE_RTSP_SERVER if (rtsp_server_threads) { delete[] rtsp_server_threads;