fsmonitor: add tests for Linux

Add a smoke test that verifies the filesystem actually delivers inotify events to the daemon. On some configurations (e.g., overlayfs with older kernels), inotify watches succeed but events are never delivered. The daemon cookie wait will time out, but every subsequent test would fail. Skip the entire test file early when this is detected. Add a test that exercises rapid nested directory creation to verify the daemon correctly handles the EEXIST race between recursive scan and queued inotify events. When IN_MASK_CREATE is available and a directory watch is added during recursive registration, the kernel may also deliver a queued IN_CREATE event for the same directory. The second inotify_add_watch() returns EEXIST, which must be treated as harmless. An earlier version of the listener crashed in this scenario. Reduce --start-timeout from the default 60 seconds to 10 seconds so that tests fail promptly when the daemon cannot start. Harden the test helpers to work in environments without procps (e.g., Fedora CI): fall back to reading /proc/$pid/stat for the process group ID when ps is unavailable, guard stop_git() against an empty pgid, and redirect stderr from kill to /dev/null to avoid noise when processes have already exited. Use set -m to enable job control in the submodule-pull test so that the background git pull gets its own process group, preventing the shell wait from blocking on the daemon. setsid() in the previous commit detaches the daemon itself, but the intermediate git pull process still needs its own process group for the test shell to manage it correctly. Signed-off-by: Paul Tarjan <github@paulisageek.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Paul Tarjan committed Apr 15, 2026 at 13:27 UTC d21fc23546e72ef7067c6664485d2436fc67fdde
1 file changed +81 -7
t/t7527-builtin-fsmonitor.sh
+81 -7
@@ -10,9 +10,57 @@ then
10 test_done
11 fi
12
13 +# Verify that the filesystem delivers events to the daemon.
14 +# On some configurations (e.g., overlayfs with older kernels),
15 +# inotify watches succeed but events are never delivered. The
16 +# cookie wait will time out and the daemon logs a trace message.
17 +#
18 +# Use "timeout" (if available) to guard each step against hangs.
19 +maybe_timeout () {
20 + if type timeout >/dev/null 2>&1
21 + then
22 + timeout "$@"
23 + else
24 + shift
25 + "$@"
26 + fi
27 +}
28 +verify_fsmonitor_works () {
29 + git init test_fsmonitor_smoke || return 1
30 +
31 + GIT_TRACE_FSMONITOR="$PWD/smoke.trace" &&
32 + export GIT_TRACE_FSMONITOR &&
33 + maybe_timeout 30 \
34 + git -C test_fsmonitor_smoke fsmonitor--daemon start \
35 + --start-timeout=10
36 + ret=$?
37 + unset GIT_TRACE_FSMONITOR
38 + if test $ret -ne 0
39 + then
40 + rm -rf test_fsmonitor_smoke smoke.trace
41 + return 1
42 + fi
43 +
44 + maybe_timeout 10 \
45 + test-tool -C test_fsmonitor_smoke fsmonitor-client query \
46 + --token 0 >/dev/null 2>&1
47 + maybe_timeout 5 \
48 + git -C test_fsmonitor_smoke fsmonitor--daemon stop 2>/dev/null
49 + ! grep -q "cookie_wait timed out" "$PWD/smoke.trace" 2>/dev/null
50 + ret=$?
51 + rm -rf test_fsmonitor_smoke smoke.trace
52 + return $ret
53 +}
54 +
55 +if ! verify_fsmonitor_works
56 +then
57 + skip_all="filesystem does not deliver fsmonitor events (container/overlayfs?)"
58 + test_done
59 +fi
60 +
61 stop_daemon_delete_repo () {
62 r=$1 &&
15 - test_might_fail git -C $r fsmonitor--daemon stop &&
63 + { maybe_timeout 30 git -C $r fsmonitor--daemon stop 2>/dev/null || :; } &&
64 rm -rf $1
65 }
66
@@ -67,7 +115,7 @@ start_daemon () {
115 export GIT_TEST_FSMONITOR_TOKEN
116 fi &&
117
70 - git $r fsmonitor--daemon start &&
118 + git $r fsmonitor--daemon start --start-timeout=10 &&
119 git $r fsmonitor--daemon status
120 )
121 }
@@ -520,6 +568,28 @@ test_expect_success 'directory changes to a file' '
568 grep "^event: dir1$" .git/trace
569 '
570
571 +test_expect_success 'rapid nested directory creation' '
572 + test_when_finished "git fsmonitor--daemon stop; rm -rf rapid" &&
573 +
574 + start_daemon --tf "$PWD/.git/trace" &&
575 +
576 + # Rapidly create nested directories to exercise race conditions
577 + # where directory watches may be added concurrently during
578 + # event processing and recursive scanning.
579 + for i in $(test_seq 1 20)
580 + do
581 + mkdir -p "rapid/nested/dir$i/subdir/deep" || return 1
582 + done &&
583 +
584 + # Give the daemon time to process all events
585 + sleep 1 &&
586 +
587 + test-tool fsmonitor-client query --token 0 &&
588 +
589 + # Verify daemon is still running (did not crash)
590 + git fsmonitor--daemon status
591 +'
592 +
593 # The next few test cases exercise the token-resync code. When filesystem
594 # drops events (because of filesystem velocity or because the daemon isn't
595 # polling fast enough), we need to discard the cached data (relative to the
@@ -910,7 +980,10 @@ test_expect_success "submodule absorbgitdirs implicitly starts daemon" '
980 start_git_in_background () {
981 git "$@" &
982 git_pid=$!
913 - git_pgid=$(ps -o pgid= -p $git_pid)
983 + git_pgid=$(ps -o pgid= -p $git_pid 2>/dev/null ||
984 + awk '{print $5}' /proc/$git_pid/stat 2>/dev/null) &&
985 + git_pgid="${git_pgid## }" &&
986 + git_pgid="${git_pgid%% }"
987 nr_tries_left=10
988 while true
989 do
@@ -921,15 +994,16 @@ start_git_in_background () {
994 fi
995 sleep 1
996 nr_tries_left=$(($nr_tries_left - 1))
924 - done >/dev/null 2>&1 &
997 + done >/dev/null 2>&1 3>&- 4>&- 5>&- 6>&- 7>&- &
998 watchdog_pid=$!
999 wait $git_pid
1000 }
1001
1002 stop_git () {
930 - while kill -0 -- -$git_pgid
1003 + test -n "$git_pgid" || return 0
1004 + while kill -0 -- -$git_pgid 2>/dev/null
1005 do
932 - kill -- -$git_pgid
1006 + kill -- -$git_pgid 2>/dev/null
1007 sleep 1
1008 done
1009 }
@@ -944,7 +1018,7 @@ stop_watchdog () {
1018
1019 test_expect_success !MINGW "submodule implicitly starts daemon by pull" '
1020 test_atexit "stop_watchdog" &&
947 - test_when_finished "stop_git; rm -rf cloned super sub" &&
1021 + test_when_finished "set +m; stop_git; rm -rf cloned super sub" &&
1022
1023 create_super super &&
1024 create_sub sub &&