From patchwork Sat Oct 7 11:59:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Carlo Zancanaro X-Patchwork-Id: 54579 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 4501027BBEA; Sat, 7 Oct 2023 13:07:58 +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=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,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 11F0927BBE2 for ; Sat, 7 Oct 2023 13:07:57 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qp65y-0004LU-1o; Sat, 07 Oct 2023 08:07:46 -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 1qp65w-0004Kp-K9 for guix-patches@gnu.org; Sat, 07 Oct 2023 08:07:44 -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 1qp65v-0004dH-Bt for guix-patches@gnu.org; Sat, 07 Oct 2023 08:07:43 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qp66E-00069u-Lp for guix-patches@gnu.org; Sat, 07 Oct 2023 08:08:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#66387] [PATCH] home: services: Fix race condition when detecting first login Resent-From: Carlo Zancanaro Original-Sender: "Debbugs-submit" Resent-CC: , guix-patches@gnu.org Resent-Date: Sat, 07 Oct 2023 12:08:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 66387 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 66387@debbugs.gnu.org Cc: ( , Andrew Tropin , Ludovic =?utf-8?q?Court=C3=A8s?= X-Debbugs-Original-To: guix-patches@gnu.org X-Debbugs-Original-Xcc: ( , Andrew Tropin , Ludovic =?utf-8?q?Court=C3=A8s?= Received: via spool by submit@debbugs.gnu.org id=B.169668043523615 (code B ref -1); Sat, 07 Oct 2023 12:08:02 +0000 Received: (at submit) by debbugs.gnu.org; 7 Oct 2023 12:07:15 +0000 Received: from localhost ([127.0.0.1]:53516 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qp65S-00068o-O1 for submit@debbugs.gnu.org; Sat, 07 Oct 2023 08:07:15 -0400 Received: from lists.gnu.org ([2001:470:142::17]:36482) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qp65Q-00068Y-GL for submit@debbugs.gnu.org; Sat, 07 Oct 2023 08:07:13 -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 1qp650-0002nX-BV for guix-patches@gnu.org; Sat, 07 Oct 2023 08:06:46 -0400 Received: from voltorb.zancanaro.id.au ([45.77.50.64]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qp64u-0004Vq-7V for guix-patches@gnu.org; Sat, 07 Oct 2023 08:06:42 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; s=k1; bh=FUXcxK4ThRsG/md zQsqSMvKac9bKZ/ZdHWTBgcjDdi8=; h=date:subject:to:from; d=zancanaro.id.au; b=RfeQ4tC6VjEL+D8OIJG58Xx54mIO0nqPREz16O1cAUCKSJ/Ju EFFEunjBRvLIM9IZbWyjVi6SjbPWTxO4tARjEINGx635aI5qLmVkoGHJxi1SZ2yZk//ais uFYi88HrkmoeukbpjdD75rN/SY8ueDfSJI5cLmzfhD5wN9CoPSPk= Received: by voltorb.zancanaro.id.au (OpenSMTPD) with ESMTPSA id 3ca1b165 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Sat, 7 Oct 2023 12:06:13 +0000 (UTC) From: Carlo Zancanaro Date: Sat, 7 Oct 2023 22:59:05 +1100 Message-ID: X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Received-SPF: pass client-ip=45.77.50.64; envelope-from=carlo@zancanaro.id.au; helo=voltorb.zancanaro.id.au X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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 * gnu/home/services.scm (compute-on-first-login-script): Use open to atomically check whether a file exists and create it if not. --- I run Guix Home on NixOS with SDDM as my display manager. When I log in, I find that there are two shepherd processes running. Looking at the on-first-login script I noticed a race condition that I suspect was causing the issue. I believe the "open" incantation I have used is atomic, except on NFS, so this should ensure that the gexps are only run once. I have confirmed that this patch fixes my problem. With this patch, when I login I only have a single shepherd process. gnu/home/services.scm | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) base-commit: b566e1a98a74d84d3978cffefd05295602c9445d diff --git a/gnu/home/services.scm b/gnu/home/services.scm index 8d53f2f4d3..95ef66e091 100644 --- a/gnu/home/services.scm +++ b/gnu/home/services.scm @@ -2,6 +2,7 @@ ;;; Copyright © 2021-2023 Andrew Tropin ;;; Copyright © 2021 Xinglu Chen ;;; Copyright © 2022-2023 Ludovic Courtès +;;; Copyright © 2023 Carlo Zancanaro ;;; ;;; This file is part of GNU Guix. ;;; @@ -412,20 +413,29 @@ (define (compute-on-first-login-script _ gexps) #~(begin (use-modules (guix i18n) (guix diagnostics)) + + (define (claim-first-run file-name) + (catch #t + (lambda () + ;; This incantation will raise an error if the file at + ;; flag-file-path already exists, and will create it otherwise. + (close (open file-name (logior O_CREAT O_EXCL))) + #t) + (lambda (e) + #f))) + #$%initialize-gettext (let* ((xdg-runtime-dir (or (getenv "XDG_RUNTIME_DIR") (format #f "/run/user/~a" (getuid)))) (flag-file-path (string-append - xdg-runtime-dir "/on-first-login-executed")) - (touch (lambda (file-name) - (call-with-output-file file-name (const #t))))) + xdg-runtime-dir "/on-first-login-executed"))) ;; XDG_RUNTIME_DIR dissapears on logout, that means such trick ;; allows to launch on-first-login script on first login only ;; after complete logout/reboot. (if (file-exists? xdg-runtime-dir) - (unless (file-exists? flag-file-path) - (begin #$@gexps (touch flag-file-path))) + (when (claim-first-run flag-file-path) + #$@gexps) ;; TRANSLATORS: 'on-first-login' is the name of a service and ;; shouldn't be translated (warning (G_ "XDG_RUNTIME_DIR doesn't exists, on-first-login script