* [PATCH 1/3] fsmonitor: remove the right reverse entry of a watch on Linux
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
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
2 siblings, 0 replies; 4+ messages in thread
From: Abhishek Agarwal @ 2026-12-06 7:00 UTC (permalink / raw)
To: git
Cc: Junio C Hamano, Patrick Steinhardt, Paul Tarjan, Erik Cervin-Edin,
John Mellor, Nicolas Riebesel
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
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 2/3] fsmonitor: keep watch paths below a renamed directory up to date
2026-12-06 7:00 [PATCH 0/3] fsmonitor: fix rename handling in the Linux listener Abhishek Agarwal
2026-12-06 7:00 ` [PATCH 1/3] fsmonitor: remove the right reverse entry of a watch on Linux Abhishek Agarwal
@ 2026-12-06 7:00 ` Abhishek Agarwal
2026-12-06 7:00 ` [PATCH 3/3] fsmonitor: watch directories moved into the worktree on Linux Abhishek Agarwal
2 siblings, 0 replies; 4+ messages in thread
From: Abhishek Agarwal @ 2026-12-06 7:00 UTC (permalink / raw)
To: git
Cc: Junio C Hamano, Patrick Steinhardt, Paul Tarjan, Erik Cervin-Edin,
John Mellor, Nicolas Riebesel
An inotify watch belongs to a directory, not to its path, so after a
directory is renamed, the watches on its subdirectories keep working.
The Linux listener only records the new path of the renamed directory
itself, though. When register_inotify() then walks the renamed
directory, add_watch() asks for each subdirectory's watch with
IN_MASK_CREATE, gets EEXIST, and returns without updating its maps.
Those watches stay known by their old paths, so changes below them
are reported for paths that no longer exist, and "git status" misses
them until something resets the daemon's state.
When the directory is already watched, ask for its watch again without
IN_MASK_CREATE, which returns the existing watch descriptor and leaves
the watch as it is, and record the new path in both maps.
Erik's report [1] points out that this case was already asked about
when the listener was reviewed.
[1] https://lore.kernel.org/git/as4z1SskiPyrSKIa@vader/
Reported-by: Erik Cervin-Edin <erik@cervined.in>
Signed-off-by: Abhishek Agarwal <agab0323@gmail.com>
---
compat/fsmonitor/fsm-listen-linux.c | 102 ++++++++++++++++++++++------
t/t7527-builtin-fsmonitor.sh | 11 +++
2 files changed, 91 insertions(+), 22 deletions(-)
diff --git a/compat/fsmonitor/fsm-listen-linux.c b/compat/fsmonitor/fsm-listen-linux.c
index 8b5fe492d1..df91714f76 100644
--- a/compat/fsmonitor/fsm-listen-linux.c
+++ b/compat/fsmonitor/fsm-listen-linux.c
@@ -92,29 +92,17 @@ static int rename_entry_cmp(const void *cmp_data UNUSED,
return e1->cookie != e2->cookie;
}
-/*
- * Register an inotify watch, add watch descriptor to path mapping
- * and the reverse mapping.
- */
-static int add_watch(const char *path, struct fsm_listen_data *data)
-{
- const char *interned = strintern(path);
- struct watch_entry *w1, *w2;
+/* All events but reads and opens, and only for directories. */
+#define WATCH_MASK \
+ ((IN_ALL_EVENTS | IN_ONLYDIR) ^ IN_ACCESS ^ IN_CLOSE ^ IN_OPEN)
- /* add the inotify watch, don't allow watches to be modified */
- int wd = inotify_add_watch(data->fd_inotify, interned,
- (IN_ALL_EVENTS | IN_ONLYDIR | IN_MASK_CREATE)
- ^ IN_ACCESS ^ IN_CLOSE ^ IN_OPEN);
- if (wd < 0) {
- if (errno == ENOENT || errno == ENOTDIR)
- return 0; /* directory was deleted or is not a directory */
- if (errno == EEXIST)
- return 0; /* watch already exists, no action needed */
- if (errno == ENOSPC)
- return error(_("inotify watch limit reached; "
- "increase fs.inotify.max_user_watches"));
- return error_errno(_("inotify_add_watch('%s') failed"), interned);
- }
+/*
+ * Add watch descriptor to path mapping and the reverse mapping.
+ */
+static void record_watch(int wd, const char *interned,
+ struct fsm_listen_data *data)
+{
+ struct watch_entry *w1, *w2;
/* add watch descriptor -> directory mapping */
CALLOC_ARRAY(w1, 1);
@@ -129,7 +117,77 @@ static int add_watch(const char *path, struct fsm_listen_data *data)
w2->dir = interned;
hashmap_entry_init(&w2->ent, strhash(w2->dir));
hashmap_add(&data->revwatches, &w2->ent);
+}
+/*
+ * The directory is already watched, for example because it is below a
+ * directory that was renamed: a watch follows the directory, not its
+ * path, so we still know it by the path it had before the rename.
+ * Record its new path.
+ */
+static int update_watch_path(const char *interned,
+ struct fsm_listen_data *data)
+{
+ struct watch_entry k, *w1, *w2;
+
+ /* without IN_MASK_CREATE, this returns the existing watch */
+ int wd = inotify_add_watch(data->fd_inotify, interned, WATCH_MASK);
+ if (wd < 0) {
+ if (errno == ENOENT || errno == ENOTDIR)
+ return 0; /* deleted, or not a directory */
+ return error_errno(_("inotify_add_watch('%s') failed"),
+ interned);
+ }
+
+ k.wd = wd;
+ hashmap_entry_init(&k.ent, memhash(&k.wd, sizeof(int)));
+ w1 = hashmap_get_entry(&data->watches, &k, ent, NULL);
+ if (!w1) {
+ /* the old watch went away in the meantime */
+ record_watch(wd, interned, data);
+ return 0;
+ }
+ if (w1->dir == interned)
+ return 0; /* already known under this path */
+
+ k.dir = w1->dir;
+ hashmap_entry_init(&k.ent, strhash(k.dir));
+ w2 = hashmap_remove_entry(&data->revwatches, &k, ent, &wd);
+ if (!w2)
+ BUG("no reverse watch for '%s'", w1->dir);
+
+ trace_printf_key(&trace_fsmonitor, "watch for '%s' is now '%s'",
+ w1->dir, interned);
+ w1->dir = interned;
+ w2->dir = interned;
+ hashmap_entry_init(&w2->ent, strhash(w2->dir));
+ hashmap_add(&data->revwatches, &w2->ent);
+ return 0;
+}
+
+/*
+ * Register an inotify watch, add watch descriptor to path mapping
+ * and the reverse mapping.
+ */
+static int add_watch(const char *path, struct fsm_listen_data *data)
+{
+ const char *interned = strintern(path);
+
+ /* add the inotify watch, don't allow watches to be modified */
+ int wd = inotify_add_watch(data->fd_inotify, interned,
+ WATCH_MASK | IN_MASK_CREATE);
+ if (wd < 0) {
+ if (errno == ENOENT || errno == ENOTDIR)
+ return 0; /* directory was deleted or is not a directory */
+ if (errno == EEXIST)
+ return update_watch_path(interned, data);
+ if (errno == ENOSPC)
+ return error(_("inotify watch limit reached; "
+ "increase fs.inotify.max_user_watches"));
+ return error_errno(_("inotify_add_watch('%s') failed"), interned);
+ }
+
+ record_watch(wd, interned, data);
return 0;
}
diff --git a/t/t7527-builtin-fsmonitor.sh b/t/t7527-builtin-fsmonitor.sh
index e0c4fa0aac..b7041e1825 100755
--- a/t/t7527-builtin-fsmonitor.sh
+++ b/t/t7527-builtin-fsmonitor.sh
@@ -1497,4 +1497,15 @@ test_expect_success 'rename a directory again after its old path came back' '
check_fsmonitor_status back
'
+test_expect_success 'edits below a renamed directory are reported' '
+ test_when_finished "stop_daemon_delete_repo below" &&
+ start_watching below old old/sub &&
+ git -C below mv old new &&
+ git -C below commit -q -m rename &&
+ echo b >>below/new/sub/file &&
+ echo " M new/sub/file" >expect &&
+ git -C below status --porcelain >actual &&
+ test_cmp expect actual
+'
+
test_done
--
2.56.0
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 3/3] fsmonitor: watch directories moved into the worktree on Linux
2026-12-06 7:00 [PATCH 0/3] fsmonitor: fix rename handling in the Linux listener Abhishek Agarwal
2026-12-06 7:00 ` [PATCH 1/3] fsmonitor: remove the right reverse entry of a watch on Linux Abhishek Agarwal
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 ` Abhishek Agarwal
2 siblings, 0 replies; 4+ messages in thread
From: Abhishek Agarwal @ 2026-12-06 7:00 UTC (permalink / raw)
To: git
Cc: Junio C Hamano, Patrick Steinhardt, Paul Tarjan, Erik Cervin-Edin,
John Mellor, Nicolas Riebesel
A directory moved into the worktree from outside arrives as an
IN_MOVED_TO event without a matching IN_MOVED_FROM. rename_dir() then
only logs "no matching cookie", and while register_inotify() adds
watches for the subdirectories, nothing adds one for the directory
itself. Changes to files directly in it are never reported, so "git
status" does not show them, and according to the report [1], "git
checkout" and "git rm" do not notice them either and can discard them.
Add the watch in that case, too.
[1] https://lore.kernel.org/git/CAG_kaUYEjQhiPMatcPXr00q3oO77BM2Mu7KUZyP0aSGGwKTsJA@mail.gmail.com/
Reported-by: John Mellor <johnme@google.com>
Signed-off-by: Abhishek Agarwal <agab0323@gmail.com>
---
compat/fsmonitor/fsm-listen-linux.c | 3 +++
t/t7527-builtin-fsmonitor.sh | 15 +++++++++++++++
2 files changed, 18 insertions(+)
diff --git a/compat/fsmonitor/fsm-listen-linux.c b/compat/fsmonitor/fsm-listen-linux.c
index df91714f76..f57efc56cb 100644
--- a/compat/fsmonitor/fsm-listen-linux.c
+++ b/compat/fsmonitor/fsm-listen-linux.c
@@ -325,6 +325,9 @@ static void rename_dir(uint32_t cookie, const char *path,
/* Directory was moved from outside the watch tree */
trace_printf_key(&trace_fsmonitor,
"no matching cookie for rename to '%s'", path);
+ if (add_watch(path, data))
+ trace_printf_key(&trace_fsmonitor,
+ "failed to add watch for '%s'", path);
}
}
diff --git a/t/t7527-builtin-fsmonitor.sh b/t/t7527-builtin-fsmonitor.sh
index b7041e1825..ea1c7b8106 100755
--- a/t/t7527-builtin-fsmonitor.sh
+++ b/t/t7527-builtin-fsmonitor.sh
@@ -1508,4 +1508,19 @@ test_expect_success 'edits below a renamed directory are reported' '
test_cmp expect actual
'
+test_expect_success 'edits in a directory moved in from outside are seen' '
+ test_when_finished "stop_daemon_delete_repo into && rm -rf outside" &&
+ start_watching into &&
+ mkdir -p outside/pkg/sub &&
+ echo a >outside/pkg/top &&
+ echo a >outside/pkg/sub/file &&
+ mv outside/pkg into/pkg &&
+ git -C into add pkg &&
+ git -C into commit -q -m add &&
+ echo b >>into/pkg/top &&
+ echo " M pkg/top" >expect &&
+ git -C into status --porcelain >actual &&
+ test_cmp expect actual
+'
+
test_done
--
2.56.0
^ permalink raw reply related [flat|nested] 4+ messages in thread