From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp10.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id sHQRM1yTQGMWcAAAbAwnHQ (envelope-from ) for ; Fri, 07 Oct 2022 23:00:12 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp10.migadu.com with LMTPS id EAUnMlyTQGP6JgEAG6o9tA (envelope-from ) for ; Fri, 07 Oct 2022 23:00:12 +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 4200C53BD for ; Fri, 7 Oct 2022 23:00:12 +0200 (CEST) Received: from localhost ([::1]:45882 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oguS3-0007UM-Db for larch@yhetil.org; Fri, 07 Oct 2022 17:00:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54242) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oguRw-0007RT-By for bug-guix@gnu.org; Fri, 07 Oct 2022 17:00:05 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:38495) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oguRw-00044f-2E for bug-guix@gnu.org; Fri, 07 Oct 2022 17:00:04 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oguRu-00047I-Tt for bug-guix@gnu.org; Fri, 07 Oct 2022 17:00:02 -0400 Subject: bug#25018: GC incorrectly removes the temporary root file of the calling process Resent-From: Maxim Cournoyer Original-Sender: "Debbugs-submit" Resent-To: bug-guix@gnu.org Resent-Date: Fri, 07 Oct 2022 21:00:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: cc-closed 25018 X-GNU-PR-Package: guix X-GNU-PR-Keywords: To: ludo@gnu.org (Ludovic =?UTF-8?Q?Court=C3=A8s?=) Cc: h.goebel@crazy-compilers.com, Eelco Dolstra , 25018-done@debbugs.gnu.org Mail-Followup-To: 25018@debbugs.gnu.org, maxim.cournoyer@gmail.com, ludo@gnu.org Received: via spool by 25018-done@debbugs.gnu.org id=D25018.166517636915745 (code D ref 25018); Fri, 07 Oct 2022 21:00:02 +0000 Received: (at 25018-done) by debbugs.gnu.org; 7 Oct 2022 20:59:29 +0000 Received: from localhost ([127.0.0.1]:37570 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oguRN-00045t-Aq for submit@debbugs.gnu.org; Fri, 07 Oct 2022 16:59:29 -0400 Received: from mail-qk1-f172.google.com ([209.85.222.172]:45662) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oguRM-00045g-Bf for 25018-done@debbugs.gnu.org; Fri, 07 Oct 2022 16:59:28 -0400 Received: by mail-qk1-f172.google.com with SMTP id y1so3639849qky.12 for <25018-done@debbugs.gnu.org>; Fri, 07 Oct 2022 13:59:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:user-agent:message-id :in-reply-to:date:references:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=bultWQ5bt03bHCnJSm+ifciVBZmcA/FdP2OFWfieEoM=; b=QCj2wyANvpcWjsCEU5sbuKdfUwrL/1c6vEJY9GWk/IObU1bbRuqZpIgVQxxj9ZEF2d 5/V0d7eEiqxfeeCYxZIvL06RLlds6+sNbqso+T4kMB8u7VF4x44Wmjtw9n17QKlm1Xf3 qy3eiETNKbcIa73gv2irwRhY2k6wUOy8MCI2W8c0u+pORXzGedeNzQ3r3zkKlfDrHqoj DFIsgRz6EpZVbfeb8NdVYn7THQM9WwMgSfAEprMofrR49m96hYzF0kn4HZMW9+AqV1ld sUmtuG6HYPbJJPnrtY+EVTBN0l8a4ulqFqTulJdScWHbni8Ybo4OIpMm5/+0EgXSvXHe 0Yyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:user-agent:message-id :in-reply-to:date:references:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=bultWQ5bt03bHCnJSm+ifciVBZmcA/FdP2OFWfieEoM=; b=YIOT4xooXbvLP3sYafubt1qjyWdMlP7PHpcroRqUXxEgVVWzqdG2/Q1AXYM0wLCEQM +dkwI4LR+XzqBM+wPzvvb2BuKrNvrJji0dKhyVJQAVhMCC2jZanYAgfkCArJQOkv0ea7 ktZXeJwJpHQrPGt2G6Eiij2PnmJTXgnUd0JKWVnIY4b4UFc5bYmy1jgz5M/D4HE7VKV+ RUoOlTfI1JcR9KKlv+B1cPZV55p3S6ucDJm6NjAsi5ZrkiVoxBBnU7WErBsGX3RqWQyw Ko4xf/PcLakq6W1hCx7bt0ot2Iq0Igo9uycInyq2pM2mkrRNUraVqOYsdD6xm7Qth/fW EYqw== X-Gm-Message-State: ACrzQf2xaxcIi/dGBfNq9EogqFhV5o9ymcps4rZxQSnE0ZWlQ3yXGJ0+ Sv+XGl+tQarytHt3Ecddgvm/JIC9GjFJ/g== X-Google-Smtp-Source: AMsMyM7mJz6QckdZ0H2MfPhVHxPhD4g5KfouL0EVAb6jVEkcYIAVv8PxkMXLu/y4cu7wncZB2van+w== X-Received: by 2002:a37:348:0:b0:6e5:a3ed:c5d9 with SMTP id 69-20020a370348000000b006e5a3edc5d9mr5115500qkd.284.1665176362661; Fri, 07 Oct 2022 13:59:22 -0700 (PDT) Received: from hurd ([2607:fad8:4:3::1005]) by smtp.gmail.com with ESMTPSA id z6-20020ac87ca6000000b003913996dce3sm2855700qtv.6.2022.10.07.13.59.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Oct 2022 13:59:22 -0700 (PDT) From: Maxim Cournoyer References: <87d1hl55ur.fsf@gnu.org> Date: Fri, 07 Oct 2022 16:59:20 -0400 In-Reply-To: <87d1hl55ur.fsf@gnu.org> ("Ludovic =?UTF-8?Q?Court=C3=A8s?="'s message of "Thu, 24 Nov 2016 15:07:24 +0100") Message-ID: <87pmf34afr.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-guix@gnu.org List-Id: Bug reports for GNU Guix List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guix-bounces+larch=yhetil.org@gnu.org Sender: "bug-Guix" X-Migadu-Flow: FLOW_IN X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1665176412; 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-to: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=bultWQ5bt03bHCnJSm+ifciVBZmcA/FdP2OFWfieEoM=; b=Xx+9LXcAM4w1tdVnxlc/+fNHfdNF/QjtJprVAh+b3j8HBWRbizHYBqNj+NObxKmbDfNvDK AIaFPVjDTQi6VCNh/BHJU5BbqhlXYHGjBS0Y3G4mal+kvKLOGB3EGTHN+B85DGnIu6ifRx SnCeFvq4gtl30ijebtyJaer18XZ0dCUoC1JsFWv+D28KM6q+JwK90CNWh6nM/u+HBaoSPa jGiQsoaGZAenxJHGkytU4c2eLYJfSBJgkrSV6tGOTRBx4vB9QkuV8DhTlcf3JqpXfEWM+J n/+0BSH0Io3qGTCd8hfmi04PuVpmHpRwaI+Sd51RKGstMOR0FUPXzUBsxKqLUw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1665176412; a=rsa-sha256; cv=none; b=OUHtWPWy6ydetS76t7A09AT+V9Y1+Aqd3owhxTTdTIV4V2peKynJxPUOFWKta29O/EJQ28 GRp0qFBrS7xPVRZYv4TAW1Y/cE9rNk8BBN70/n7na39ru43AasLw909tvlC4UyM+VjnB84 yWrBy1gOw7W22iUcbrDqpCs4BVwxemykxORMQj77K/xj4wjEbyWFpwFXjjdvJFJtHxNs1s 8C2K7AGikZwax7OlbcedA/4ZrHoVDmk7vae4gaQverMRBJ/132zc0RwiPpVwjf9+hqKQSP BT+F+PanElvJIjYR8bcH/iMtQhqxQ6w/0UIYR46CCu38ExtxZP19CShf093fbw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20210112 header.b=QCj2wyAN; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of "bug-guix-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="bug-guix-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: 5.63 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gmail.com header.s=20210112 header.b=QCj2wyAN; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of "bug-guix-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="bug-guix-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: 4200C53BD X-Spam-Score: 5.63 X-Migadu-Scanner: scn0.migadu.com X-TUID: EVKOkC2xmB10 Hi Ludo, ludo@gnu.org (Ludovic Court=C3=A8s) writes: > Hello, > > The =E2=80=98readTempRoots=E2=80=99 function in gc.cc has this: > > /* Try to acquire a write lock without blocking. This can > only succeed if the owning process has died. In that case > we don't care about its temporary roots. */ > if (lockFile(*fd, ltWrite, false)) { > printMsg(lvlError, format("removing stale temporary roots fil= e `%1%'") % path); > unlink(path.c_str()); > > There=E2=80=99s a thinko here: locking the file also succeeds when the lo= ck is > already held by the calling process. > > In that case, this code ends up removing the temporary root file of > calling process, which is bad. Here=E2=80=99s a sample session: > > scheme@(guile-user)> ,use(guix) > scheme@(guile-user)> (define s (open-connection)) > scheme@(guile-user)> (current-build-output-port (current-error-port)) > $2 =3D # > scheme@(guile-user)> (set-build-options s #:verbosity 10) > $3 =3D #t > scheme@(guile-user)> (add-text-to-store s "foo" "bar!") > acquiring global GC lock `/var/guix/gc.lock' > acquiring read lock on `/var/guix/temproots/4259' > acquiring write lock on `/var/guix/temproots/4259' > downgrading to read lock on `/var/guix/temproots/4259' > locking path `/gnu/store/0siy93lggjw7sfdg8gsvrzafaa974h2d-foo' > lock acquired on `/gnu/store/0siy93lggjw7sfdg8gsvrzafaa974h2d-foo.lock' > `/gnu/store/0siy93lggjw7sfdg8gsvrzafaa974h2d-foo' has hash `c756ef12a70ba= d10c9ac276ecd1213ea7cc3a2e6c462ba47e4f9a88756a055d0' > lock released on `/gnu/store/0siy93lggjw7sfdg8gsvrzafaa974h2d-foo.lock' > $4 =3D "/gnu/store/0siy93lggjw7sfdg8gsvrzafaa974h2d-foo" > scheme@(guile-user)> (delete-paths s (list $4)) > acquiring global GC lock `/var/guix/gc.lock' > finding garbage collector roots... > executing `/gnu/store/l99rkv2713nl53kr3gn4akinvifsx19h-guix-0.11.0-3.7ca3= /libexec/guix/list-runtime-roots' to find additional roots > [=E2=80=A6] > reading temporary root file `/var/guix/temproots/4259' > removing stale temporary roots file `/var/guix/temproots/4259' > [=E2=80=A6] > considering whether to delete `/gnu/store/0siy93lggjw7sfdg8gsvrzafaa974h2= d-foo' > | invalidating path `/gnu/store/0siy93lggjw7sfdg8gsvrzafaa974h2d-foo' > | deleting `/gnu/store/0siy93lggjw7sfdg8gsvrzafaa974h2d-foo' > | recursively deleting path `/gnu/store/0siy93lggjw7sfdg8gsvrzafaa974h2= d-foo' > | | /gnu/store/0siy93lggjw7sfdg8gsvrzafaa974h2d-foo > deleting `/gnu/store/trash' > recursively deleting path `/gnu/store/trash' > | /gnu/store/trash > deleting unused links... > deleting unused link `/gnu/store/.links/1l2ml1b8ga7rwi3vlqn4wsic6z7a2c9cs= vi7mk4i1b8blw9fymn7' > note: currently hard linking saves 6699.22 MiB > $5 =3D ("/gnu/store/0siy93lggjw7sfdg8gsvrzafaa974h2d-foo") > $6 =3D 4096 > > Notice the =E2=80=9Cremoving stale temporary roots file=E2=80=9D message. > > Eelco: shouldn=E2=80=99t it be changed along the lines of the attached pa= th? > > > Thanks, > Ludo=E2=80=99. > > diff --git a/nix/libstore/gc.cc b/nix/libstore/gc.cc > index 72eff52..d92388f 100644 > --- a/nix/libstore/gc.cc > +++ b/nix/libstore/gc.cc > @@ -2,6 +2,7 @@ > #include "misc.hh" > #include "local-store.hh" >=20=20 > +#include > #include > #include > #include > @@ -225,10 +226,10 @@ static void readTempRoots(PathSet & tempRoots, FDs = & fds) > //FDPtr fd(new AutoCloseFD(openLockFile(path, false))); > //if (*fd =3D=3D -1) continue; >=20=20 > - /* Try to acquire a write lock without blocking. This can > - only succeed if the owning process has died. In that case > - we don't care about its temporary roots. */ > - if (lockFile(*fd, ltWrite, false)) { > + /* Try to acquire a write lock without blocking. This can only > + succeed if the owning process has died, in which case we don'= t care > + about its temporary roots, or if we are the owning process. = */ > + if (i.name !=3D std::to_string(getpid()) && lockFile(*fd, ltWrit= e, false)) { > printMsg(lvlError, format("removing stale temporary roots fi= le `%1%'") % path); > unlink(path.c_str()); > writeFull(*fd, "d"); > I'm not Eelco, but your change LGTM. Note that the upstream version still uses the original code [0]. I've installed the change, tested that it had the expected result: --8<---------------cut here---------------start------------->8--- reading temporary root file `/var/guix/temproots/8386' waiting for read lock on `/var/guix/temproots/8386' got temporary root `/gnu/store/0siy93lggjw7sfdg8gsvrzafaa974h2d-foo' considering whether to delete `/gnu/store/0siy93lggjw7sfdg8gsvrzafaa974h2d-= foo' | cannot delete `/gnu/store/0siy93lggjw7sfdg8gsvrzafaa974h2d-foo' because= it's a root | cannot delete `/gnu/store/0siy93lggjw7sfdg8gsvrzafaa974h2d-foo' because= it's still reachable ice-9/boot-9.scm:1685:16: In procedure raise-exception: ERROR: 1. &store-protocol-error: message: "cannot delete path `/gnu/store/0siy93lggjw7sfdg8gsvrzafaa97= 4h2d-foo' since it is still alive" status: 1 --8<---------------cut here---------------end--------------->8--- and pushed! Closing. [0] https://github.com/NixOS/nix/blob/master/src/libstore/gc.cc#L194 --=20 Thanks, Maxim