GNU bug report logs - #77753
[PATCH] daemon: Catch SIGINT, SIGTERM, and SIGHUP for proper termination.

Previous Next

Package: guix-patches;

Reported by: Ludovic Courtès <ludo <at> gnu.org>

Date: Fri, 11 Apr 2025 21:38:01 UTC

Severity: normal

Tags: patch

Done: Ludovic Courtès <ludo <at> gnu.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 77753 in the body.
You can then email your comments to 77753 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to guix-patches <at> gnu.org:
bug#77753; Package guix-patches. (Fri, 11 Apr 2025 21:38:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Ludovic Courtès <ludo <at> gnu.org>:
New bug report received and forwarded. Copy sent to guix-patches <at> gnu.org. (Fri, 11 Apr 2025 21:38:01 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Ludovic Courtès <ludo <at> gnu.org>
To: guix-patches <at> gnu.org
Cc: Ludovic Courtès <ludo <at> gnu.org>,
 Christopher Baines <mail <at> cbaines.net>
Subject: [PATCH] daemon: Catch SIGINT, SIGTERM,
 and SIGHUP for proper termination.
Date: Fri, 11 Apr 2025 23:37:20 +0200
Previously the daemon would not install handlers for these signals.  It
would thus terminate abruptly when receiving them, without properly
closing the SQLite database.

Consequently, the database’s WAL file, which is normally deleted by the
last client closing the database (via ‘sqlite3_close’), would not be
deleted when the guix-daemon process is terminated; instead, it would
persist and possibly keep growing beyond reason.

This patch fixes that.

* nix/nix-daemon/nix-daemon.cc (handleSignal, setTerminationSignalHandler):
New functions.
(processConnection): Call it.

Reported-by: Christopher Baines <mail <at> cbaines.net>
Change-Id: I07e510a1242e92b6a629d60eb840e029c0f921be
---
 nix/nix-daemon/nix-daemon.cc | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

Hello,

While discussing it with Maxim and Chris on guix-sysadmin, we
realized that the daemon was not always properly closing its
database, which was a likely explanation of its WAL file not
being deleted and growing too much on the build farm.

This patch appears to fix that.

Thoughts?

Ludo’.

diff --git a/nix/nix-daemon/nix-daemon.cc b/nix/nix-daemon/nix-daemon.cc
index 4cb05c802e..e29237e65d 100644
--- a/nix/nix-daemon/nix-daemon.cc
+++ b/nix/nix-daemon/nix-daemon.cc
@@ -165,6 +165,24 @@ static void setSigPollAction(bool enable)
 #endif
 }
 
+static void handleSignal(int signum)
+{
+    string name = program_invocation_short_name;
+    auto message = name + ": PID " + std::to_string(getpid())
+	+ " caught signal " + std::to_string(signum) + "\n";
+    writeFull(STDERR_FILENO, (unsigned char *) message.c_str(), message.length());
+    _isInterrupted = 1;
+    blockInt = 1;
+}
+
+static void setTerminationSignalHandler()
+{
+    auto signals = { SIGINT, SIGTERM, SIGHUP };
+    for (int signum: signals) {
+	signal(signum, handleSignal);
+    }
+}
+
 
 /* startWork() means that we're starting an operation for which we
    want to send out stderr to the client. */
@@ -803,6 +821,10 @@ static void processConnection(bool trusted, uid_t userId)
             throw Error("if you run `nix-daemon' as root, then you MUST set `build-users-group'!");
 #endif
 
+	/* Catch SIGTERM & co. to ensure proper termination: closing the store
+	   and its database, thereby deleting its WAL file.  */
+	setTerminationSignalHandler();
+
         /* Open the store. */
         store = std::shared_ptr<StoreAPI>(new LocalStore(reserveSpace));
 

base-commit: 772b70455d0d5972fdad80d8529647dce20f409a
-- 
2.49.0





Reply sent to Ludovic Courtès <ludo <at> gnu.org>:
You have taken responsibility. (Mon, 14 Apr 2025 21:08:03 GMT) Full text and rfc822 format available.

Notification sent to Ludovic Courtès <ludo <at> gnu.org>:
bug acknowledged by developer. (Mon, 14 Apr 2025 21:08:03 GMT) Full text and rfc822 format available.

Message #10 received at 77753-done <at> debbugs.gnu.org (full text, mbox):

From: Ludovic Courtès <ludo <at> gnu.org>
To: 77753-done <at> debbugs.gnu.org
Cc: Christopher Baines <mail <at> cbaines.net>
Subject: Re: [bug#77753] [PATCH] daemon: Catch SIGINT, SIGTERM, and SIGHUP
 for proper termination.
Date: Mon, 14 Apr 2025 23:07:25 +0200
Ludovic Courtès <ludo <at> gnu.org> writes:

> Previously the daemon would not install handlers for these signals.  It
> would thus terminate abruptly when receiving them, without properly
> closing the SQLite database.
>
> Consequently, the database’s WAL file, which is normally deleted by the
> last client closing the database (via ‘sqlite3_close’), would not be
> deleted when the guix-daemon process is terminated; instead, it would
> persist and possibly keep growing beyond reason.
>
> This patch fixes that.
>
> * nix/nix-daemon/nix-daemon.cc (handleSignal, setTerminationSignalHandler):
> New functions.
> (processConnection): Call it.
>
> Reported-by: Christopher Baines <mail <at> cbaines.net>
> Change-Id: I07e510a1242e92b6a629d60eb840e029c0f921be

Pushed as dd947985522886f9de6fdfdde3f0601e42219da5.




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Tue, 13 May 2025 11:24:16 GMT) Full text and rfc822 format available.

This bug report was last modified 62 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.