From 20a51de7097893dfc34d611f4c3561f0a61ca64d Mon Sep 17 00:00:00 2001 From: Raphael Dumusc Date: Wed, 30 Aug 2017 14:33:28 +0200 Subject: [PATCH] Fixed unit test deadlock due to incorrect execption handling This bug was introduced in #177 and could result in the global QThreadPool threads being locked forever. --- deflect/ImageSegmenter.cpp | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/deflect/ImageSegmenter.cpp b/deflect/ImageSegmenter.cpp index 603183e..1beef69 100644 --- a/deflect/ImageSegmenter.cpp +++ b/deflect/ImageSegmenter.cpp @@ -124,11 +124,25 @@ bool ImageSegmenter::_generateJpeg(const ImageWrapper& image, // Note: Qt insists that sending (by calling handler()) should happen // exclusively from the QThread where the socket lives. Sending from the // worker threads triggers a qWarning. - bool result = true; - for (size_t i = 0; i < segments.size(); ++i) - if (!handler(_sendQueue.dequeue())) - result = false; - return result; + size_t i = 0; + try + { + bool result = true; + for (; i < segments.size(); ++i) + if (!handler(_sendQueue.dequeue())) + result = false; + return result; + } + catch (...) + { + // Wait for remaining threaded operations to finish, without calling the + // handler. Otherwise the remaining threads may wait forever leading to + // a deadlock in QApplication destructor. + ++i; + for (; i < segments.size(); ++i) + _sendQueue.dequeue(); + std::rethrow_exception(std::current_exception()); + } #else static bool first = true; if (first)