diff --git a/src/libstore/unix/build/local-derivation-goal.cc b/src/libstore/unix/build/local-derivation-goal.cc index 732988df368..83952302b8e 100644 --- a/src/libstore/unix/build/local-derivation-goal.cc +++ b/src/libstore/unix/build/local-derivation-goal.cc @@ -19,6 +19,7 @@ #include "unix-domain-socket.hh" #include "posix-fs-canonicalise.hh" #include "posix-source-accessor.hh" +#include "signals.hh" #include #include @@ -1504,7 +1505,7 @@ void LocalDerivationGoal::startDaemon() daemonThread = std::thread([this, store]() { - while (true) { + loopUntilInterrupted([this, store]() { /* Accept a connection. */ struct sockaddr_un remoteAddr; @@ -1513,8 +1514,8 @@ void LocalDerivationGoal::startDaemon() AutoCloseFD remote = accept(daemonSocket.get(), (struct sockaddr *) &remoteAddr, &remoteAddrLen); if (!remote) { - if (errno == EINTR || errno == EAGAIN) continue; - if (errno == EINVAL || errno == ECONNABORTED) break; + if (errno == EINTR || errno == EAGAIN) return Continue; + if (errno == EINVAL || errno == ECONNABORTED) return Stop; throw SysError("accepting connection"); } @@ -1536,8 +1537,8 @@ void LocalDerivationGoal::startDaemon() }); daemonWorkerThreads.push_back(std::move(workerThread)); - } - + }); + // We've been interrupted, so we shut down this thread asap. debug("daemon shutting down"); }); } diff --git a/src/libutil/thread-pool.cc b/src/libutil/thread-pool.cc index 0355e1f07fa..460e931465c 100644 --- a/src/libutil/thread-pool.cc +++ b/src/libutil/thread-pool.cc @@ -89,7 +89,7 @@ void ThreadPool::doWork(bool mainThread) bool didWork = false; std::exception_ptr exc; - while (true) { + loopUntilInterrupted([&]() { work_t w; { auto state(state_.lock()); @@ -122,7 +122,7 @@ void ThreadPool::doWork(bool mainThread) /* Wait until a work item is available or we're asked to quit. */ while (true) { - if (quit) return; + if (quit) return Stop; if (!state->pending.empty()) break; @@ -132,7 +132,7 @@ void ThreadPool::doWork(bool mainThread) if (!state->active && state->draining) { quit = true; work.notify_all(); - return; + return Stop; } state.wait(work); @@ -150,7 +150,10 @@ void ThreadPool::doWork(bool mainThread) } didWork = true; - } + return Continue; + }); + // We've been interrupted or there's no more work to do; either way we + // must return now. } }