Skip to content

Commit

Permalink
fix: Prevent Interrupted from falling out of a thread
Browse files Browse the repository at this point in the history
... into std::terminate; a crash.

This also adds `checkInterrupt` calls to speed up shutdowns.
  • Loading branch information
roberth committed Oct 10, 2024
1 parent 08d5ef1 commit b6d157d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
11 changes: 6 additions & 5 deletions src/libstore/unix/build/local-derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "unix-domain-socket.hh"
#include "posix-fs-canonicalise.hh"
#include "posix-source-accessor.hh"
#include "signals.hh"

#include <regex>
#include <queue>
Expand Down Expand Up @@ -1504,7 +1505,7 @@ void LocalDerivationGoal::startDaemon()

daemonThread = std::thread([this, store]() {

while (true) {
loopUntilInterrupted([this, store]() {

/* Accept a connection. */
struct sockaddr_un remoteAddr;
Expand All @@ -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");
}

Expand All @@ -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");
});
}
Expand Down
11 changes: 7 additions & 4 deletions src/libutil/thread-pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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.
}

}
Expand Down

0 comments on commit b6d157d

Please sign in to comment.