From patchwork Fri Apr 11 21:37:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 41597 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 21D7F27BC4A; Fri, 11 Apr 2025 22:38:31 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-6.4 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_VALIDITY_CERTIFIED,RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE, SPF_HELO_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 9455527BC49 for ; Fri, 11 Apr 2025 22:38:30 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u3M4b-0001Qm-Ke; Fri, 11 Apr 2025 17:38:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u3M4Y-0001NW-Uq for guix-patches@gnu.org; Fri, 11 Apr 2025 17:38:03 -0400 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1u3M4Y-0008HN-6k for guix-patches@gnu.org; Fri, 11 Apr 2025 17:38:02 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:From:To:Subject; bh=JlqY/d4sm/47+oaQEJhf3IISpxdXckaMSAFAqzsrdzg=; b=mMHqp5Yx9q+BvAFn7X8aI6l0TYll/mw5/O0a2t+Ul6oRFP3gsWltOnYuvjwNAw2nYFPMgdTjqrxJBZFanrGKObkfcHU04Fx/y40Oz2xnel0nqzBz4fcBzxTnxlhvxUMNcvSg2rSjQSHAWR+zOTcm3MLBX/HXZt25/IGNsoORmEVu9a0PVToYfGAqO1WfJHlR18YaeHov8oIhpSWKFpf/76oQyeqdRM2VmTGTCjcgTSfYbZW+D17s051Us67Mzzwt9W26Ey1ea6E5aq6gFBZNeNqokxydaWDiBNXq5mzbL26yHnVVS3Gt91/GZLkAadv7qiuaKqDdZM+dXdz10w3NUQ==; Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1u3M4X-0002ej-Ro for guix-patches@gnu.org; Fri, 11 Apr 2025 17:38:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#77753] [PATCH] daemon: Catch SIGINT, SIGTERM, and SIGHUP for proper termination. Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Fri, 11 Apr 2025 21:38:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 77753 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 77753@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= , Christopher Baines X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.174440746310167 (code B ref -1); Fri, 11 Apr 2025 21:38:01 +0000 Received: (at submit) by debbugs.gnu.org; 11 Apr 2025 21:37:43 +0000 Received: from localhost ([127.0.0.1]:52031 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1u3M4F-0002dv-3m for submit@debbugs.gnu.org; Fri, 11 Apr 2025 17:37:43 -0400 Received: from lists.gnu.org ([2001:470:142::17]:35036) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1u3M4C-0002de-Fu for submit@debbugs.gnu.org; Fri, 11 Apr 2025 17:37:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u3M45-0000tP-Vu for guix-patches@gnu.org; Fri, 11 Apr 2025 17:37:34 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u3M44-0008F4-1c; Fri, 11 Apr 2025 17:37:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:Date:Subject:To:From:in-reply-to: references; bh=JlqY/d4sm/47+oaQEJhf3IISpxdXckaMSAFAqzsrdzg=; b=o06LuC5dYAk5Gj wx+ACUiHg0Dxo5Sm2mU3nEfJvcbm4vdNcTTbIjDAmI2SbeP8yBulZZ1+KiIMqM0t06EnoPAdl9Poe smPnyCu0sznjPzv2DD2bC/OkKZIjD7sz9jySGoOz8BuA46jGAM+49HBM35g5YKOQ8O5cT0/9zqPfF hlj0ULFfXOO1oYsYfg2ZtuaLTalibeXjgwrK9OgP1NXleCxzfYhnfMC4LAMdps3pvCjUFztaR3dMr mUa8GpGZ1cUC0VIQRLKMFNPR1rNPQqANChsLtLoaY6/9A7dXxfP1jLMpHqMu2P7yfbafracw/1w32 23fQT2iKzhejOEYfZ8hg==; From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Fri, 11 Apr 2025 23:37:20 +0200 Message-ID: <1385cb8951405f364ae02b9c36eb6e33ea204afa.1744407289.git.ludo@gnu.org> X-Mailer: git-send-email 2.49.0 MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches 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 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 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(new LocalStore(reserveSpace));