Skip to content

[release/11.0] Mitigate macOS FSEvents race in TokensFiredForNewDirectoryContentsOnRename - #138435

Open
lewing wants to merge 1 commit into
release/11.0from
lewing-backport-fileprovider-test-11
Open

lewing wants to merge 1 commit into
release/11.0from
lewing-backport-fileprovider-test-11

Conversation

@lewing

@lewing lewing commented Nov 27, 2026

Copy link
Copy Markdown
Member

Fixes Issue #135283

main PR #135342

Description

Test-only backport of #135342: TokensFiredForNewDirectoryContentsOnRename failed on osx-arm64 in #138290 CI.

Customer Impact

None; test-only.

Regression

N/A; test-only.

Testing

./dotnet.sh build src/libraries/Microsoft.Extensions.FileProviders.Physical/tests/Microsoft.Extensions.FileProviders.Physical.Tests.csproj -p:TargetFramework=net11.0 -p:UseLocalTargetingRuntimePack=false --no-incremental — passed, 0 warnings and 0 errors. Tests not run.

Risk

Low; test-only.

Package authoring no longer needed in .NET 9

IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older versions.

Note

This PR description was generated with GitHub Copilot.

…ename (#135342)

## Summary

Mitigates the flaky
`Microsoft.Extensions.FileProviders.PhysicalFileProviderTests.TokensFiredForNewDirectoryContentsOnRename`
test on macOS.

### Root cause

The test creates a real directory/subdirectory/file structure on disk,
then starts a real `FileSystemWatcher`-backed
`PhysicalFilesWatcher`/`PhysicalFileProvider` watch, and asserts the
corresponding change tokens have not fired yet (before the test's
simulated rename). On macOS, the native watcher is backed by
`fseventsd`, a separate daemon that the process subscribes to
asynchronously. As documented by a .NET team member investigating a
related issue
(#30415 (comment)):

> We can get events for operations completed **before** watcher
started... even if we call `sync` to flush events when watcher is
started, we may [see] old events `fseventsd` did not process yet.

So even though the watcher requests `kFSEventStreamEventIdSinceNow`,
there is no documented guarantee it excludes events for changes made
shortly before the stream starts. This occasionally causes the "new
directory/subdirectory/file should not have changed yet" assertions to
fail.

This is architecturally specific to macOS: Windows
(`ReadDirectoryChangesW`) and Linux (`inotify`) are
handle/descriptor-based with no separate daemon replaying historical
events.

### Why not fix it in `FileSystemWatcher`/`PhysicalFilesWatcher` instead

- The macOS backend already uses the most responsive options available
in the public FSEvents API (`kFSEventStreamEventIdSinceNow`, `0.0f`
latency, `NoDefer`), so there isn't more headroom at that layer.
- A `PhysicalFilesWatcher`-side fix (e.g. snapshotting expected state at
watch-registration time and suppressing events that predate it) could
close this deterministically, but it's a meaningfully more invasive
change for a bug class that, in production, results in at most a
spurious/redundant change notification — not a missed one or a
correctness issue. That cost/benefit doesn't justify it here, so this PR
only addresses the test.

### Fix

The test only needs one injected event:
`fileSystemWatcher.CallOnRenamed(...)`, which invokes
`FileSystemWatcher.OnRenamed` directly. That method does not perform
`Filter` matching itself — pattern matching against `Filter` only
happens in `NotifyFileSystemEventArgs`/`NotifyRenameEventArgs`, which
wrap genuine native OS events before invoking the protected
`OnChanged`/`OnCreated`/`OnRenamed` methods (see
`src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs`).

So the test now sets `fileSystemWatcher.Filter` to a pattern that cannot
match any of its (randomly generated, GUID-named) test entries before
starting the watch. This deterministically suppresses any genuine native
events — including delayed `fseventsd` replays of the pre-watch
directory/file creation — while leaving the explicitly injected rename
event unaffected, since it bypasses `Filter` entirely. This removes the
need for an artificial delay or a dynamic skip, and keeps all the
pre-rename assertions active and deterministic on every platform.

Fixes #135283

> [!NOTE]
> This PR description and change were drafted with AI (GitHub Copilot)
assistance.

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: b2aac025-5dc1-420e-bfdf-19dd4b2f2f6f
(cherry picked from commit a60b70b)

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@lewing lewing added area-Extensions-FileSystem test-bug Problem in test source code (most likely) labels Nov 27, 2026
@lewing
lewing temporarily deployed to copilot-pat-pool November 27, 2026 19:35 — with GitHub Actions Inactive
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@lewing
lewing temporarily deployed to copilot-pat-pool November 27, 2026 19:36 — with GitHub Actions Inactive
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-extensions-filesystem
See info in area-owners.md if you want to be subscribed.

@lewing lewing added the Servicing-approved Approved for servicing release label Nov 30, 2026

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 4a7abddf Deployed Nov 27, 2026 by lewing via conclusion #13460
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Extensions-FileSystem Servicing-approved Approved for servicing release test-bug Problem in test source code (most likely)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants