aboutsummaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
authormoneromooo-monero <moneromooo-monero@users.noreply.github.com>2020-08-12 22:13:29 +0000
committermoneromooo-monero <moneromooo-monero@users.noreply.github.com>2020-09-01 14:33:33 +0000
commit6a37da837e1c936200f8d71a142973cc60b17b32 (patch)
tree922c9eff10660a49a16249781b05e5b8e1b5fc97 /src/common
parentMerge pull request #6586 (diff)
downloadmonero-6a37da837e1c936200f8d71a142973cc60b17b32.tar.xz
threadpool: guard against exceptions in jobs, and armour plating
Those would, if uncaught, exit run and leave the waiter to wait indefinitely for the number of active jobs to reach 0
Diffstat (limited to '')
-rw-r--r--src/common/dns_utils.cpp4
-rw-r--r--src/common/threadpool.cpp11
-rw-r--r--src/common/threadpool.h8
3 files changed, 14 insertions, 9 deletions
diff --git a/src/common/dns_utils.cpp b/src/common/dns_utils.cpp
index c871177b1..4f4efcd81 100644
--- a/src/common/dns_utils.cpp
+++ b/src/common/dns_utils.cpp
@@ -521,14 +521,14 @@ bool load_txt_records_from_dns(std::vector<std::string> &good_records, const std
// send all requests in parallel
std::deque<bool> avail(dns_urls.size(), false), valid(dns_urls.size(), false);
tools::threadpool& tpool = tools::threadpool::getInstance();
- tools::threadpool::waiter waiter;
+ tools::threadpool::waiter waiter(tpool);
for (size_t n = 0; n < dns_urls.size(); ++n)
{
tpool.submit(&waiter,[n, dns_urls, &records, &avail, &valid](){
records[n] = tools::DNSResolver::instance().get_txt_record(dns_urls[n], avail[n], valid[n]);
});
}
- waiter.wait(&tpool);
+ waiter.wait();
size_t cur_index = first_index;
do
diff --git a/src/common/threadpool.cpp b/src/common/threadpool.cpp
index a1737778c..edc87fc48 100644
--- a/src/common/threadpool.cpp
+++ b/src/common/threadpool.cpp
@@ -120,7 +120,7 @@ threadpool::waiter::~waiter()
catch (...) { /* ignore */ }
try
{
- wait(NULL);
+ wait();
}
catch (const std::exception &e)
{
@@ -128,12 +128,12 @@ threadpool::waiter::~waiter()
}
}
-void threadpool::waiter::wait(threadpool *tpool) {
- if (tpool)
- tpool->run(true);
+bool threadpool::waiter::wait() {
+ pool.run(true);
boost::unique_lock<boost::mutex> lock(mt);
while(num)
cv.wait(lock);
+ return !error();
}
void threadpool::waiter::inc() {
@@ -166,7 +166,8 @@ void threadpool::run(bool flush) {
lock.unlock();
++depth;
is_leaf = e.leaf;
- e.f();
+ try { e.f(); }
+ catch (const std::exception &ex) { e.wo->set_error(); try { MERROR("Exception in threadpool job: " << ex.what()); } catch (...) {} }
--depth;
is_leaf = false;
diff --git a/src/common/threadpool.h b/src/common/threadpool.h
index 91f9fdf47..66b08fece 100644
--- a/src/common/threadpool.h
+++ b/src/common/threadpool.h
@@ -55,12 +55,16 @@ public:
class waiter {
boost::mutex mt;
boost::condition_variable cv;
+ threadpool &pool;
int num;
+ bool error_flag;
public:
void inc();
void dec();
- void wait(threadpool *tpool); //! Wait for a set of tasks to finish.
- waiter() : num(0){}
+ bool wait(); //! Wait for a set of tasks to finish, returns false iff any error
+ void set_error() noexcept { error_flag = true; }
+ bool error() const noexcept { return error_flag; }
+ waiter(threadpool &pool) : pool(pool), num(0), error_flag(false) {}
~waiter();
};