From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:403:478a::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id GWoXJR1KIWXFAQAA9RJhRA:P1 (envelope-from ) for ; Sat, 07 Oct 2023 14:07:57 +0200 Received: from aspmx1.migadu.com ([2001:41d0:403:478a::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id GWoXJR1KIWXFAQAA9RJhRA (envelope-from ) for ; Sat, 07 Oct 2023 14:07:57 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 1186856D6A for ; Sat, 7 Oct 2023 14:07:57 +0200 (CEST) Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=zancanaro.id.au header.s=k1 header.b=RfeQ4tC6; dmarc=fail reason="SPF not aligned (relaxed)" header.from=zancanaro.id.au (policy=none); spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1696680477; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:list-id:list-help: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=FUXcxK4ThRsG/mdzQsqSMvKac9bKZ/ZdHWTBgcjDdi8=; b=K+N5G15WlNkzs1y0XNikfSECAO0npfkcCQMOkKJ/1mdV/4+xwfkyWuAIrd+O1/74QIHMad JahsYS8JcVsTBPzkfDKmzpwJG7nnS9kbjUTQpUgwV2JnW88nro6ASmhbz/8KzUhXO9Udyl rfDebLPw/VLlwbAwcjE7RmYsGRkWK1xdW+2YOWVtLHVyPqDdYw2OnqVdpMmHGf0u9rTtDh 8KunQeqjTts7DJKaGUD6uJ84fFrTg/MDQHjaf4oQQF2wvW8mHwLqcaJ+sF396mWONP8OQG ROWH1S8pwft1SGgRhW9Jxy9yaXl6AKpo6enl0IhPQjxAMKvT7uC3ai1tOndLbQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1696680477; a=rsa-sha256; cv=none; b=Wrl31y5EgtuMVWtnKBVORaTpeL07rhiHOsmEzr+b6Pyr3xKMRWzYP9OUyLnLXhi94G/g5K TdtNl7zesmcXfYOrGnWbCc3PwqW115MA2kisB5mKR9HNr+HnNGd++hOK0zWJs+SlSorOhS 3CGrF2K9+Ti46HhxYLDPtK+TNQBWMpE3MhoYn1lfrcfwP+iyM+K5T1jISYfs32ACd748T5 y7S+fkNbQpigMWd7J348hSDn4Z4lQ7dk8lyilJfB+x3hkdNqSpGb2H/l1BwnizVtLWG77u esA6p/UJK/iND8rTKK2FuZ+Qt0TopaHUj4BMfCvvFm9WkQ/ZguXO1nimb/+gtg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=zancanaro.id.au header.s=k1 header.b=RfeQ4tC6; dmarc=fail reason="SPF not aligned (relaxed)" header.from=zancanaro.id.au (policy=none); spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" 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 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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+larch=yhetil.org@gnu.org Sender: guix-patches-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Scanner: mx0.migadu.com X-Migadu-Spam-Score: -4.12 X-Spam-Score: -4.12 X-Migadu-Queue-Id: 1186856D6A X-TUID: 02XHTfkpoZgp * 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(-) 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 base-commit: b566e1a98a74d84d3978cffefd05295602c9445d -- 2.41.0