From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.174 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796540489; b=B/cz82mM3FnTE5vswkqLndr5HstkYRgkTtRu3CkGDN6tRMIodURkvTUBGtXWJP9VU5Yy w+XuQCDU0BWAeL3GDkf8F4JHKBbcdhjLIymMCj7xCWOMyAC7JKYrxc4qhpo2xylpeimT3 DvIJkzS+rOrFMcmiuvu47nAIO1UYa+TsQ4zavupSZhITIqwcKIbt/y0Hfu4Td/B9/Q/MA OtlFYd9PjOmnL/dKxq45yiSpsNYW/FbcsWko4SjZ431VnoJju6TnBNlREg2wWgp0yCQey udUXGoETNExwZYVk5HGfSR00aznXCkMJx1QGtm10xNYetsskLsn/6WH4ekO3GWrxkIg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796540489; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=qIJoHftLZySB6U4jD28a6ER5SCJaeLEeggEELepXmhM=; b=ztje/G9DJMZbwQdM+vcpMP2C7zb2CxWyuSQhUSPV9HMZaPGeqC2wbuhXrhBmBFY5FHCX xsIH+YB9p81qYHSD7NFCjgVODooYf5jYnY4NXIc+3PDHMvbMtBxIjtGJBI1J2jp50hVwc UsObVanwj9JLTssXgLBZ+toK9WwR2XMiUuNYSor6IR0ZHmfGZEWCTPGngpkM3/iTtd7Ks 0EVp1bzqlns4yEFIBLuk4i3tZxS8CAC4nBd7I9EBgdpxPx2qmjGd7yFddgY1cYePhA434 /1JX8gSdoSS2UK/r5dBbjFzdDkkyXUhGtdV4PNo/8sbGlRI+HykTdDD/Iz00jap/aMg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=BILT+TyP; arc=none smtp.remote-ip=74.125.82.174 Received: from mail-dy1-f174.google.com (mail-dy1-f174.google.com [74.125.82.174]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B73E6606A31F for ; Sun, 6 Dec 2026 07:01:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B73E6606A31F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=BILT+TyP Received: by mail-dy1-f174.google.com with SMTP id 5a478bee46e88-3869f03af6aso2829502eec.1 for ; Sat, 05 Dec 2026 23:01:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796540488; x=1797145288; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=qIJoHftLZySB6U4jD28a6ER5SCJaeLEeggEELepXmhM=; b=BILT+TyP3Cdhr/3HZ8UMF5bonOx2aqNSaAn0wjkohwVnA5E3y2BryF6O5rbs1kmnI4 9Z4/5DrtH50/E9dcolMW3mddj4FmOdmt17Gn0TzFOMuTHc/f5Btcpa5xaVv8XcyyqHQ9 PpSDOci2Nsvqv0HGKQP/BuSBXOmJq3uQygucaCZhIeGeXQ7k2kARCLNtUlVKKykE57Xn O17LXdwyYRHC/sUfoYGMsyIgt14zw6NHdkNVkOTs4g+4hwBx+1bQq5TaLyLpsWO4dRc6 V3M0INYJtX3qpU0rk1F0Zi9EawdX/p0qsjR92W1CE7oIBUGuupgpRbXFC9/GfG+8ONS4 P4EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796540488; x=1797145288; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=qIJoHftLZySB6U4jD28a6ER5SCJaeLEeggEELepXmhM=; b=qJ6ShqQL5JGB5tMSIvRh2eVCYOJ5knKpwzSAwC06L54ZxbCmbq0hMH1qZv/2wa86C7 tx5zQc6QtthUVw5cecKUwgBHNd5T6gJBOruEz0tm8X81lobFLHGTzLkIqQZDAf0HLC75 Zq7g/EZZH2VGb4HAxic2grRYq/iIZCzzDewlY9yD/WRdsZYM03A7Oez6HrOXJ6sO+ELO +kFPtfHJmGbITM1hC1NKhMNt4TKt7omdUQk/9CVXgZEt6elRF7CECXYWl4vtvqSHu6ms CuKCg/87wN6O0ooGhpLgrhNjZ4I7o+YzJjkVU1L1v+p0TYXefjuW0FsameF7Rd3DQEKX N7ug== X-Gm-Message-State: AB4q+uH8rB4jYJ7Wz+NSgKBlK/dmRQ90CrStAA30ycmjhWehsoC68ntx M5EYUkOGpYbEJ/iVligHYfwK/mevNY/ZNjkVXe9c1omQgYph+yybRkp3I4OndrTE X-Gm-Gg: AZFdTZc0iDsqiaf38bvZ9l24t9qYkHFRnsrEQd75K418kCAdWoQC3vsKQMxc+UBRKX/ WsCOrYOy7RJ2rOLSx28m4LUNV7Hjki6kh1vqkFgkbOTTJiTxtTcoZigEAtT5erCmRRvKC+oKzYI vT9WRQqMd3MDtbMctEomqzdQHknCKIYC+nu2hhWxfUVeO0YgWwwWxNlpWF/oRDYBKP8ND/dK5S/ +i0choxeeurwV1VUFScggrLdXE8JQLNPG0Po7oUZVej5ldA/Gosl9XAgjV8gK8i6Bc5Iam+zeLA GjnGYfqkffX/UnmRRD6aXpJ5uqXNvQMheMY0jevXK9QzjqZI5oz+Oy5lfCJMt91n75eNdwMIRqP HEfcm8r3YF6C63lwiL03kF6MWQ6HSyglw2DwcOECWHBaJPQ6rYoT/NXdzjJAGSSkUosz/TkWNDS R0kRbXIgs4CqtdLGLrEq9xBqEVEqaS7g+UBkKS3qxIyRefgGk5x57aqZ1ucae6wswuYKi2EDvU4 Bmr7a0gjx8Zq/0je8sMbnR7Rg0zdJ+i1YFu/2RVQYMoxGtGqQ5yk8C3HiPoC0mlA9BbqU9tmL0Y gj99zXRtlg4fFWbcSsT3gmEb0qRo X-Received: by 2002:a05:7300:dc10:b0:384:a3c5:35b8 with SMTP id 5a478bee46e88-38742cf7d1amr13582559eec.8.1796540487566; Sat, 05 Dec 2026 23:01:27 -0800 (PST) Received: from aeron-host ([103.177.178.109]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-387521bb368sm41675801eec.10.2026.12.05.23.01.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 23:01:27 -0800 (PST) From: Abhishek Agarwal To: git@vger.kernel.org Cc: Junio C Hamano , Patrick Steinhardt , Paul Tarjan , Erik Cervin-Edin , John Mellor , Nicolas Riebesel Subject: [PATCH 1/3] fsmonitor: remove the right reverse entry of a watch on Linux Date: Sun, 6 Dec 2026 12:30:43 +0530 Message-ID: <20261206070045.12607-2-agab0323@gmail.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261206070045.12607-1-agab0323@gmail.com> References: <20261206070045.12607-1-agab0323@gmail.com> X-Mailing-List: git@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The Linux listener keeps two maps of its inotify watches: "watches", from watch descriptor to directory path, and "revwatches", from path back to watch. remove_watch() looks up the reverse entry by path alone. Two watches can be known under the same path for a while, though, and then remove_watch() can take away the entry of the other watch. From then on the maps disagree, and the next rename of that path finds a watch that is already gone, so the daemon aborts with BUG: compat/fsmonitor/fsm-listen-linux.c:144: double remove of watch for '' Renaming a directory onto an empty directory B and then renaming B again is enough: for a moment both the new B and the replaced empty B are known as B, and when the latter goes away, the entry of the former is removed. Stale paths of watches below a renamed directory lead to the same state (the next commit fixes those). That is what happens in the "move_directory_up" cases of t7527, which run right after "move_directory_contents_deeper". The daemon dies there, too, but the tests only notice when the crash leaves a core file in the worktree. Let revwatches_entry_cmp() also compare the watch descriptor when the caller passes one as keydata, and pass it from remove_watch(). The new tests start the daemon with GIT_TEST_FSMONITOR_TOKEN, so they can tell whether the daemon that answers at the end is the one that was started: a daemon that died is started again by the next query, and the new one uses a different token. Signed-off-by: Abhishek Agarwal --- compat/fsmonitor/fsm-listen-linux.c | 14 ++++-- t/t7527-builtin-fsmonitor.sh | 68 +++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/compat/fsmonitor/fsm-listen-linux.c b/compat/fsmonitor/fsm-listen-linux.c index e3dca14b62..8b5fe492d1 100644 --- a/compat/fsmonitor/fsm-listen-linux.c +++ b/compat/fsmonitor/fsm-listen-linux.c @@ -60,16 +60,24 @@ static int watch_entry_cmp(const void *cmp_data UNUSED, return e1->wd != e2->wd; } +/* + * Entries are looked up by path. When "keydata" points to a watch + * descriptor, the entry must also belong to that watch: two watches can + * be known under the same path for a while, e.g. when a directory is + * renamed onto an empty directory that is still being watched. + */ static int revwatches_entry_cmp(const void *cmp_data UNUSED, const struct hashmap_entry *eptr, const struct hashmap_entry *entry_or_key, - const void *keydata UNUSED) + const void *keydata) { const struct watch_entry *e1, *e2; e1 = container_of(eptr, const struct watch_entry, ent); e2 = container_of(entry_or_key, const struct watch_entry, ent); - return strcmp(e1->dir, e2->dir); + if (strcmp(e1->dir, e2->dir)) + return 1; + return keydata && e1->wd != *(const int *)keydata; } static int rename_entry_cmp(const void *cmp_data UNUSED, @@ -148,7 +156,7 @@ static void remove_watch(struct watch_entry *w, struct fsm_listen_data *data) k2.dir = w->dir; hashmap_entry_init(&k2.ent, strhash(k2.dir)); - w2 = hashmap_remove_entry(&data->revwatches, &k2, ent, NULL); + w2 = hashmap_remove_entry(&data->revwatches, &k2, ent, &w->wd); if (!w2) BUG("double remove of reverse watch for '%s'", w->dir); diff --git a/t/t7527-builtin-fsmonitor.sh b/t/t7527-builtin-fsmonitor.sh index f2770e9056..e0c4fa0aac 100755 --- a/t/t7527-builtin-fsmonitor.sh +++ b/t/t7527-builtin-fsmonitor.sh @@ -1429,4 +1429,72 @@ test_expect_success UNTRACKED_CACHE 'untracked cache is checked after a trivial test_cmp expect actual ' +# The daemon started by start_watching (with "--tk true") must still be +# the one answering: a daemon that died is started again by the next +# query, and the new one uses a token id that is not "test_00000001". +same_daemon () { + test-tool -C "$1" fsmonitor-client query \ + --token "builtin:test_00000001:0" >"$1.token" && + nul_to_q <"$1.token" >"$1.token.q" && + test_grep "^builtin:test_00000001:" "$1.token.q" +} + +# Agree with a full scan, and still be the same daemon. +check_fsmonitor_status () { + git -C "$1" status --porcelain >"$1.actual" && + git -C "$1" -c core.fsmonitor=false status --porcelain >"$1.expect" && + test_cmp "$1.expect" "$1.actual" && + same_daemon "$1" +} + +# Commit a file in each given directory of a new repository $1, then +# start a daemon for it and turn on core.fsmonitor. +start_watching () { + git init "$1" && + ( + cd "$1" && + shift && + for d in "$@" + do + mkdir -p "$d" && + echo a >"$d/file" || exit 1 + done && + git add . && + git commit -q --allow-empty -m initial + ) && + start_daemon -C "$1" --tk true && + git -C "$1" config core.fsmonitor true && + git -C "$1" update-index --fsmonitor && + git -C "$1" status +} + +# Rename a directory onto an existing empty one; "mv" cannot do this portably. +rename_onto () { + "$PERL_PATH" -e 'rename($ARGV[0], $ARGV[1]) or die "rename: $!\n"' \ + "$1" "$2" +} + +test_expect_success !MINGW,PERL_TEST_HELPERS 'rename onto an empty directory' ' + test_when_finished "stop_daemon_delete_repo onto" && + start_watching onto A A/sub && + mkdir onto/B && + rename_onto onto/A onto/B && + git -C onto status && + mv onto/B onto/C && + check_fsmonitor_status onto +' + +test_expect_success 'rename a directory again after its old path came back' ' + test_when_finished "stop_daemon_delete_repo back" && + start_watching back T1 T1/T2 T1/T2/T3 T1/T2/T3/T4 && + mkdir back/T1/new && + mv back/T1/T2 back/T1/new/ && + git -C back status && + git -C back reset -q --hard && + git -C back clean -q -fd && + git -C back status && + mv back/T1/T2/T3 back/T1/ && + check_fsmonitor_status back +' + test_done -- 2.56.0