All of lore.kernel.org
 help / color / mirror / Atom feed
From: Abhishek Agarwal <agab0323@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	Patrick Steinhardt <ps@pks.im>,
	Paul Tarjan <github@paulisageek.com>,
	Erik Cervin-Edin <erik@cervined.in>,
	John Mellor <johnme@google.com>,
	Nicolas Riebesel <nicolas@adamatec.com>
Subject: [PATCH 1/3] fsmonitor: remove the right reverse entry of a watch on Linux
Date: Sun,  6 Dec 2026 12:30:43 +0530	[thread overview]
Message-ID: <20261206070045.12607-2-agab0323@gmail.com> (raw)
In-Reply-To: <20261206070045.12607-1-agab0323@gmail.com>

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 '<path>'

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 <agab0323@gmail.com>
---
 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


  reply	other threads:[~2026-12-06  7:01 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06  7:00 [PATCH 0/3] fsmonitor: fix rename handling in the Linux listener Abhishek Agarwal
2026-12-06  7:00 ` Abhishek Agarwal [this message]
2026-12-06  7:00 ` [PATCH 2/3] fsmonitor: keep watch paths below a renamed directory up to date Abhishek Agarwal
2026-12-06  7:00 ` [PATCH 3/3] fsmonitor: watch directories moved into the worktree on Linux Abhishek Agarwal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261206070045.12607-2-agab0323@gmail.com \
    --to=agab0323@gmail.com \
    --cc=erik@cervined.in \
    --cc=git@vger.kernel.org \
    --cc=github@paulisageek.com \
    --cc=gitster@pobox.com \
    --cc=johnme@google.com \
    --cc=nicolas@adamatec.com \
    --cc=ps@pks.im \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.