Skip to content

Free, open-source 3D sculpting for Linux, macOS and Windows: dense meshes, remeshing, painting, built-in AI (image/text to 3D), Send to Unreal, game-ready LODs, Gaussian splats and OpenUSD.

ThreadPool: a job that throws no longer leaves the pool waiting for ever

islandmed343b61e1fe2c
diff --git a/src/core/ThreadPool.cpp b/src/core/ThreadPool.cpp
index 7af0a6e0..f87ee3ed 100644
--- a/src/core/ThreadPool.cpp
+++ b/src/core/ThreadPool.cpp
@@ -45,12 +45,19 @@ ThreadPool& ThreadPool::shared() {
return pool;
}
-// Runs jobs of `run` until none are left; returns how many this thread ran.
+// Runs jobs of `run` until none are left; returns how many this thread ran. A job's exception stays in run.error
+// and no further job starts: thrown through here, it left run() waiting for ever (its Run gone, workers still in it).
size_t ThreadPool::drain(Run& run) {
const bool wasInside = insidePoolJob;
insidePoolJob = true;
size_t ran = 0;
- for (size_t i; (i = run.next.fetch_add(1)) < run.count; ++ran) (*run.job)(i);
+ try {
+ for (size_t i; (i = run.next.fetch_add(1)) < run.count; ++ran) (*run.job)(i);
+ } catch (...) {
+ run.next.store(run.count);
+ std::lock_guard lock(mutex_);
+ if (!run.error) run.error = std::current_exception();
+ }
insidePoolJob = wasInside;
return ran;
}
@@ -74,7 +81,7 @@ void ThreadPool::workerLoop() {
const size_t ran = drain(*run);
lock.lock();
run->finished += ran;
- if (--run->users == 0 && run->finished == run->count) done_.notify_all();
+ if (--run->users == 0 && (run->finished == run->count || run->error)) done_.notify_all();
}
}
@@ -97,8 +104,9 @@ void ThreadPool::run(size_t count, const std::function<void(size_t)>& job) {
const size_t ran = drain(run);
std::unique_lock lock(mutex_);
run.finished += ran;
- done_.wait(lock, [&] { return run.users == 0 && run.finished == run.count; });
+ done_.wait(lock, [&] { return run.users == 0 && (run.finished == run.count || run.error); });
current_ = nullptr;
+ if (run.error) std::rethrow_exception(run.error);
}
} // namespace sf
diff --git a/src/core/ThreadPool.h b/src/core/ThreadPool.h
index b178b0f1..d2d4a2b8 100644
--- a/src/core/ThreadPool.h
+++ b/src/core/ThreadPool.h
@@ -13,6 +13,7 @@
#include <atomic>
#include <condition_variable>
#include <cstddef>
+#include <exception>
#include <functional>
#include <mutex>
#include <thread>
@@ -39,7 +40,8 @@ public:
bool serial() const { return serial_; }
// Calls job(i) for every i in [0, count), spread over the pool and the calling thread; returns when all
// are done. Runs inline when called from inside a job, while another thread is using the pool, or when
- // count <= 1.
+ // count <= 1. A job that throws stops the jobs not yet started; run() rethrows the first exception once every
+ // worker has left (the pool stays usable).
void run(size_t count, const std::function<void(size_t)>& job);
private:
@@ -51,6 +53,7 @@ private:
std::atomic<size_t> next{0};
size_t finished = 0; // guarded by mutex_
int users = 0; // workers inside, guarded by mutex_
+ std::exception_ptr error; // the first job's exception, guarded by mutex_
};
void workerLoop();
size_t drain(Run& run);
diff --git a/tests/test_performance.cpp b/tests/test_performance.cpp
index 1ac2bbd9..669dde14 100644
--- a/tests/test_performance.cpp
+++ b/tests/test_performance.cpp
@@ -20,6 +20,7 @@
#include <atomic>
#include <bit>
#include <cmath>
+#include <stdexcept>
#include <thread>
using namespace sf;
@@ -116,6 +117,33 @@ TEST(thread_pool_runs_every_job_once) {
CHECK(where == std::this_thread::get_id());
}
+// A job that throws (on the calling thread or a worker): the exception comes out of poolFor once the workers have
+// left, and the pool still runs every job of the next call. (It used to leave the pool pointing at the finished
+// call: the next one waited for ever, every thread asleep.)
+TEST(thread_pool_survives_a_throwing_job) {
+ const std::thread::id caller = std::this_thread::get_id();
+ for (int round = 0; round < 200; ++round) {
+ const bool onCaller = round % 2 == 0;
+ bool threw = false;
+ try {
+ poolFor(64, [&](size_t b, size_t) {
+ if (b == 7 * 8 && (onCaller ? std::this_thread::get_id() == caller : true)) throw std::runtime_error("x");
+ std::this_thread::yield();
+ }, 8);
+ } catch (const std::runtime_error&) {
+ threw = true;
+ }
+ if (!onCaller) CHECK(threw); // (the caller may not take block 7: then nothing threw)
+ std::vector<std::atomic<int>> hits(4096);
+ poolFor(hits.size(), [&](size_t b, size_t e) {
+ for (size_t i = b; i < e; ++i) hits[i].fetch_add(1);
+ }, 64);
+ bool once = true;
+ for (auto& h : hits) once = once && h.load() == 1;
+ CHECK(once);
+ }
+}
+
TEST(parallel_brushes_match_serial) {
// icosphere(7): 164k vertices, so each dab covers several blocks (> 4096 vertices) and runs in parallel.
Document par = sphereDoc(7), ser = sphereDoc(7);