From d72a88b5629c85740ea81e2011b8d06cd8ee0228 Mon Sep 17 00:00:00 2001
From: Eelco Dolstra <eelco.dolstra@logicblox.com>
Date: Wed, 10 Jun 2015 15:55:46 +0200
Subject: [PATCH] Don't try to handle SIGINT

It just makes things unnecessarily complicated. We can just exit
without cleaning anything up, since the only thing to do is unmark
builds and build steps as busy. But we can do that by having systemd
call "hydra-queue-runner --unlock" from ExecStopPost.
---
 src/hydra-queue-runner/hydra-queue-runner.cc | 68 ++------------------
 1 file changed, 7 insertions(+), 61 deletions(-)

diff --git a/src/hydra-queue-runner/hydra-queue-runner.cc b/src/hydra-queue-runner/hydra-queue-runner.cc
index 788fe975..9e5553b6 100644
--- a/src/hydra-queue-runner/hydra-queue-runner.cc
+++ b/src/hydra-queue-runner/hydra-queue-runner.cc
@@ -27,40 +27,6 @@ bool has(const C & c, const V & v)
 }
 
 
-std::mutex exitRequestMutex;
-std::condition_variable exitRequest;
-std::atomic<bool> exitRequested(false);
-
-static std::atomic_int _int(0);
-
-void sigintHandler(int signo)
-{
-    _int = 1;
-}
-
-
-void signalThread()
-{
-    struct sigaction act;
-    act.sa_handler = sigintHandler;
-    sigemptyset(&act.sa_mask);
-    act.sa_flags = 0;
-    if (sigaction(SIGINT, &act, 0))
-        throw SysError("installing handler for SIGINT");
-
-    while (true) {
-        sleep(1000000);
-        if (_int) break;
-    }
-
-    {
-        std::lock_guard<std::mutex> lock(exitRequestMutex);
-        exitRequested = true;
-    }
-    exitRequest.notify_all();
-}
-
-
 typedef enum {
     bsSuccess = 0,
     bsFailed = 1,
@@ -402,7 +368,7 @@ void State::queueMonitor()
 {
     auto store = openStore(); // FIXME: pool
 
-    while (!exitRequested) {
+    while (true) {
         getQueuedBuilds(store);
 
         {
@@ -685,7 +651,7 @@ void State::makeRunnable(Step::ptr step)
 
 void State::dispatcher()
 {
-    while (!exitRequested) {
+    while (true) {
         printMsg(lvlError, "dispatcher woken up");
 
         {
@@ -1003,25 +969,11 @@ void State::run()
 
     auto queueMonitorThread = std::thread(&State::queueMonitor, this);
 
-    auto dispatcherThread = std::thread(&State::dispatcher, this);
+    std::thread(&State::dispatcher, this).detach();
 
-    /* Wait for SIGINT. */
-    {
-        std::unique_lock<std::mutex> lock(exitRequestMutex);
-        while (!exitRequested)
-            exitRequest.wait(lock);
-    }
-
-    printMsg(lvlError, "exiting...");
-
-    /* Shut down the various threads. */
-    { std::lock_guard<std::mutex> lock(queueMonitorMutex); } // barrier
-    queueMonitorWakeup.notify_all();
     queueMonitorThread.join();
 
-    wakeDispatcher();
-    dispatcherThread.join();
-
+    printMsg(lvlError, "exiting...");
     printMsg(lvlError, format("psql connections = %1%") % dbPool.count());
 }
 
@@ -1031,15 +983,9 @@ int main(int argc, char * * argv)
     return handleExceptions(argv[0], [&]() {
         initNix();
 
-        std::thread(signalThread).detach();
-
-        /* Ignore signals. This is inherited by the other threads. */
-        sigset_t set;
-        sigemptyset(&set);
-        sigaddset(&set, SIGHUP);
-        sigaddset(&set, SIGINT);
-        sigaddset(&set, SIGTERM);
-        sigprocmask(SIG_BLOCK, &set, NULL);
+        signal(SIGINT, SIG_DFL);
+        signal(SIGTERM, SIG_DFL);
+        signal(SIGHUP, SIG_DFL);
 
         settings.buildVerbosity = lvlVomit;
         settings.useSubstitutes = false;