[bug#77753] daemon: Catch SIGINT, SIGTERM, and SIGHUP for proper termination.
Commit Message
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@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’.
base-commit: 772b70455d0d5972fdad80d8529647dce20f409a
Comments
Ludovic Courtès <ludo@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@cbaines.net>
> Change-Id: I07e510a1242e92b6a629d60eb840e029c0f921be
Pushed as dd947985522886f9de6fdfdde3f0601e42219da5.
@@ -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));