From 2c27d7e66e0f9ad0c084d634a5efccc9c27c887d Mon Sep 17 00:00:00 2001 From: Leon <183293296+lowlyleon@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:39:02 +0100 Subject: [PATCH 1/2] win: stop watching last access time in fs events ReadDirectoryChangesW was asked for FILE_NOTIFY_CHANGE_LAST_ACCESS, so reading a file could report UV_CHANGE. NTFS only writes the access time back once the stored one is more than an hour old, which makes these events look random. No other platform watches for reads (IN_ACCESS, NOTE_READ, FILE_ACCESS), so stop asking for it on Windows too. Fixes: https://github.com/libuv/libuv/issues/5449 Refs: https://github.com/libuv/libuv/pull/3083 Co-Authored-By: Claude Opus 5.5 --- src/win/fs-event.c | 2 - test/test-fs-event.c | 97 ++++++++++++++++++++++++++++++++++++++++++++ test/test-list.h | 2 + 3 files changed, 99 insertions(+), 2 deletions(-) diff --git a/src/win/fs-event.c b/src/win/fs-event.c index 3238d8da746..1a1afdcfb17 100644 --- a/src/win/fs-event.c +++ b/src/win/fs-event.c @@ -58,7 +58,6 @@ static void uv__fs_event_queue_readdirchanges(uv_loop_t* loop, FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE | - FILE_NOTIFY_CHANGE_LAST_ACCESS | FILE_NOTIFY_CHANGE_CREATION | FILE_NOTIFY_CHANGE_SECURITY, NULL, @@ -393,7 +392,6 @@ int uv_fs_event_start(uv_fs_event_t* handle, FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE | - FILE_NOTIFY_CHANGE_LAST_ACCESS | FILE_NOTIFY_CHANGE_CREATION | FILE_NOTIFY_CHANGE_SECURITY, NULL, diff --git a/test/test-fs-event.c b/test/test-fs-event.c index 58b9ce5a630..20cfdde3e67 100644 --- a/test/test-fs-event.c +++ b/test/test-fs-event.c @@ -709,6 +709,103 @@ TEST_IMPL(fs_event_watch_dir_recursive_burst) { #endif } +#ifdef _WIN32 +#define ACCESS_DIR "watch_dir_access" +#define ACCESS_STALE ACCESS_DIR "/stale" +#define ACCESS_MARKER ACCESS_DIR "/marker" + +static void fs_event_cb_access(uv_fs_event_t* handle, + const char* filename, + int events, + int status) { + ASSERT_OK(status); + ASSERT_EQ(UV_CHANGE, events); + /* Events arrive in order, so a reported read would come before this. */ + ASSERT_STR_EQ(filename, "marker"); + ++fs_event_cb_called; + uv_close((uv_handle_t*) handle, close_cb); +} + +static void access_delete_files(void) { + delete_file(ACCESS_STALE); + delete_file(ACCESS_MARKER); + delete_dir(ACCESS_DIR); +} +#endif + +TEST_IMPL(fs_event_watch_dir_ignore_access) { +#ifndef _WIN32 + RETURN_SKIP("Tests the Windows ReadDirectoryChangesW filter."); +#else + uv_loop_t* loop; + uv_fs_t req; + uv_file file; + uv_buf_t buf; + char data[4]; + double atime; + double mtime; + int r; + + loop = uv_default_loop(); + access_delete_files(); + create_dir(ACCESS_DIR); + create_file(ACCESS_STALE); + touch_file(ACCESS_STALE); + create_file(ACCESS_MARKER); + + /* NTFS only writes the access time back once the stored one is more than + an hour old. */ + r = uv_fs_stat(NULL, &req, ACCESS_STALE, NULL); + ASSERT_OK(r); + mtime = req.statbuf.st_mtim.tv_sec + req.statbuf.st_mtim.tv_nsec / 1e9; + atime = mtime - 2 * 3600; + uv_fs_req_cleanup(&req); + r = uv_fs_utime(NULL, &req, ACCESS_STALE, atime, mtime, NULL); + ASSERT_OK(r); + uv_fs_req_cleanup(&req); + + r = uv_fs_event_init(loop, &fs_event); + ASSERT_OK(r); + r = uv_fs_event_start(&fs_event, fs_event_cb_access, ACCESS_DIR, 0); + ASSERT_OK(r); + + r = uv_fs_open(NULL, &req, ACCESS_STALE, UV_FS_O_RDONLY, 0, NULL); + ASSERT_GE(r, 0); + file = r; + uv_fs_req_cleanup(&req); + buf = uv_buf_init(data, sizeof(data)); + r = uv_fs_read(NULL, &req, file, &buf, 1, -1, NULL); + ASSERT_GT(r, 0); + uv_fs_req_cleanup(&req); + r = uv_fs_close(NULL, &req, file, NULL); + ASSERT_OK(r); + uv_fs_req_cleanup(&req); + + r = uv_fs_stat(NULL, &req, ACCESS_STALE, NULL); + ASSERT_OK(r); + if (req.statbuf.st_atim.tv_sec < (long) (mtime - 3600)) { + uv_fs_req_cleanup(&req); + uv_close((uv_handle_t*) &fs_event, close_cb); + uv_run(loop, UV_RUN_DEFAULT); + access_delete_files(); + MAKE_VALGRIND_HAPPY(loop); + RETURN_SKIP("Last access time updates are disabled."); + } + uv_fs_req_cleanup(&req); + + touch_file(ACCESS_MARKER); + uv_run(loop, UV_RUN_DEFAULT); + + ASSERT_EQ(1, fs_event_cb_called); + ASSERT_EQ(1, close_cb_called); + + access_delete_files(); + + MAKE_VALGRIND_HAPPY(loop); + return 0; +#endif +} + #ifdef _WIN32 static char short_path_file[MAX_PATH]; diff --git a/test/test-list.h b/test/test-list.h index 369847b7b17..94207cea72d 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -454,6 +454,7 @@ TEST_DECLARE (fs_event_watch_delete_dir_win) #endif TEST_DECLARE (fs_event_watch_dir_recursive) TEST_DECLARE (fs_event_watch_dir_recursive_burst) +TEST_DECLARE (fs_event_watch_dir_ignore_access) #ifdef _WIN32 TEST_DECLARE (fs_event_watch_dir_short_path) #endif @@ -1243,6 +1244,7 @@ TASK_LIST_START #endif TEST_ENTRY (fs_event_watch_dir_recursive) TEST_ENTRY (fs_event_watch_dir_recursive_burst) + TEST_ENTRY (fs_event_watch_dir_ignore_access) #ifdef _WIN32 TEST_ENTRY (fs_event_watch_dir_short_path) #endif From 0e95e39327b84ff2370b1b1e980a26d4a033a4b4 Mon Sep 17 00:00:00 2001 From: Leon <183293296+lowlyleon@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:39:02 +0100 Subject: [PATCH 2/2] test: enable last access updates in Windows CI GitHub's Windows runners have last access updates disabled, so fs_event_watch_dir_ignore_access skips there. Turn them on before the tests run. The setting takes effect without a reboot. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/CI-win.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/CI-win.yml b/.github/workflows/CI-win.yml index 71265940d4b..c884e24bdc1 100644 --- a/.github/workflows/CI-win.yml +++ b/.github/workflows/CI-win.yml @@ -46,6 +46,9 @@ jobs: ls -l build ls -l build\\RelWithDebInfo + - name: Enable NTFS last access updates + if: ${{ matrix.config.arch != 'arm64' }} + run: fsutil behavior set disablelastaccess 0 - name: platform_output_a if: ${{ matrix.config.arch != 'arm64' }} shell: cmd @@ -129,6 +132,8 @@ jobs: uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8 with: name: mingw-${{ matrix.config.arch }} + - name: Enable NTFS last access updates + run: fsutil behavior set disablelastaccess 0 - name: Test shell: cmd run: |