Skip to content

Make test file watchers more resilient - #131304

Merged
rosebyte merged 7 commits into
dotnet:mainfrom
rosebyte:fix/126726-onchange-relative-path-flaky
Aug 3, 2026
Merged

rosebyte merged 7 commits into
dotnet:mainfrom
rosebyte:fix/126726-onchange-relative-path-flaky

Conversation

@rosebyte

Copy link
Copy Markdown
Member

Fixes #126726

@azure-pipelines

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves resiliency of Microsoft.Extensions.Configuration functional tests that validate reload-on-change behavior, aiming to reduce flakiness in file watcher–driven tests (notably on Linux/containerized/stress environments).

Changes:

  • Replace one-shot file writes + WaitForChange polling with a new WaitForReload helper that re-applies file writes on each retry until the expected reload is observed.
  • Update the relative-path change-token test to use a TaskCompletionSource signal and dispose the ChangeToken.OnChange subscription.
  • Re-enable several previously [ActiveIssue]-skipped reload tests by making their wait logic more tolerant of missed/delayed notifications.

Copilot AI review requested due to automatic review settings July 24, 2026 14:22

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

@svick

svick commented Jul 24, 2026

Copy link
Copy Markdown
Member

I'm not convinced writing to the file repeatedly is necessary here. PhysicalFilesWatcher shouldn't be losing any notifications and its tests are no longer flaky (AFAIK), even though they don't do anything similar. What I would do is to increase timeout from the ~20 s it's now to the 30 s used by PFW tests.

Other notes:

  • The change to explicitly use AppContext.BaseDirectory looks good (though this shouldn't be the cause of flakiness).
  • It could make sense to ensure different runs of tests don't interfere with each other by using unique file names (in cases where unique directory names through DisposableFileSystem don't make sense).
  • There are other tests that have [ActiveIssue] against the closed Using FileWatcher on non-windows has a very large impact on startup time #42036 that should probably be reactivated.

@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.

Copilot AI review requested due to automatic review settings July 29, 2026 13:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (3)

src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs:544

  • The skip message references System.IO.FileSystem.Watcher, but the API name is System.IO.FileSystemWatcher (no dot). This message is user-visible in test output; please fix the typo for clarity.
        [SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.tvOS, "System.IO.FileSystem.Watcher is not supported on Browser/iOS/tvOS")]

src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs:915

  • The skip message references System.IO.FileSystem.Watcher, but the API name is System.IO.FileSystemWatcher (no dot). This message is user-visible in test output; please fix the typo for clarity.
        [SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.tvOS, "System.IO.FileSystem.Watcher is not supported on Browser/iOS/tvOS")]

src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs:513

  • The skip message references System.IO.FileSystem.Watcher, but the API name is System.IO.FileSystemWatcher (no dot). This message is user-visible in test output; please fix the typo for clarity.

This issue also appears in the following locations of the same file:

  • line 544
  • line 915
        [SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.tvOS, "System.IO.FileSystem.Watcher is not supported on Browser/iOS/tvOS")]

Resolves the conflict in ConfigurationTests.cs caused by dotnet#126093, which
added six OnLoadException tests immediately after ReloadOnChangeWorksAfterError
and removed OnLoadErrorCanIgnoreErrors.

Both sides kept:
- upstream's six new tests, with their [ActiveIssue]/[PlatformSpecific]
  attributes untouched;
- this branch's [SkipOnPlatform] guard on TouchingFileWillReload, replacing
  the [ActiveIssue] quarantine it carried on main.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 21a523e1-0a42-4c98-b0a0-9850b1c3c03b
Copilot AI review requested due to automatic review settings July 30, 2026 16:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (3)

src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs:737

  • The skip reason mentions a non-existent type name (System.IO.FileSystem.Watcher). The .NET type is System.IO.FileSystemWatcher, so the message is misleading.
        [SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.tvOS, "System.IO.FileSystem.Watcher is not supported on Browser/iOS/tvOS")]

src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs:1108

  • The skip reason mentions a non-existent type name (System.IO.FileSystem.Watcher). The .NET type is System.IO.FileSystemWatcher, so the message is misleading.
        [SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.tvOS, "System.IO.FileSystem.Watcher is not supported on Browser/iOS/tvOS")]

src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs:490

  • The skip reason mentions a non-existent type name (System.IO.FileSystem.Watcher). The .NET type is System.IO.FileSystemWatcher, so the message is misleading.

This issue also appears in the following locations of the same file:

  • line 737
  • line 1108
        [SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.tvOS, "System.IO.FileSystem.Watcher is not supported on Browser/iOS/tvOS")]

Copilot AI review requested due to automatic review settings July 31, 2026 11:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (2)

src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs:1278

  • The cleanup catch { } around File.Delete swallows all exceptions, which can hide unexpected problems (e.g., invalid paths). It’s safer to only swallow the common cleanup failures and let other exceptions surface.
                try
                {
                    File.Delete(watchedFilePath);
                }
                catch

src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs:1152

  • The cleanup catch { } around Directory.Delete swallows all exceptions (including unexpected ones like ArgumentException). In tests it's reasonable to ignore expected cleanup failures, but it should be limited to the known I/O-related exceptions to avoid masking real bugs (e.g., an invalid path being produced).

This issue also appears on line 1274 of the same file.

                try
                {
                    Directory.Delete(userSecretsFolder, true);
                }
                catch

@rosebyte
rosebyte enabled auto-merge (squash) July 31, 2026 12:07
Copilot AI review requested due to automatic review settings July 31, 2026 12:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (3)

src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs:1265

  • After renaming the max-wait field for consistency, update this call site to use the new name.
                await Task.WhenAny(reloadedSignal.Task, Task.Delay(s_maxWaitForReload));

src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs:25

  • s_maxWaitForReload introduces an s_-prefixed static field in a file that otherwise uses _-prefixed private fields (including other static readonly fields). This makes the naming inconsistent within the same test class.

This issue also appears on line 1265 of the same file.

        private static readonly TimeSpan s_maxWaitForReload = TimeSpan.FromSeconds(30);

src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs:1135

  • This failure message mentions "create-delete-create", but this test is validating reload after updating the user secrets file. Updating the message will make failures more actionable.
                await WaitForChange(
                    () => config["UserSecretKey1"] == "UserSecretValue2",
                    "Reload failed after create-delete-create.");

Copilot AI review requested due to automatic review settings August 3, 2026 15:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (1)

src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs:1144

  • In TouchingFileWillReloadForUserSecrets, disposing the IConfigurationRoot stops the ChangeToken registrations, but it doesn’t dispose the PhysicalFileProvider instance created by AddUserSecrets (the FileConfigurationProvider intentionally does not dispose Source.FileProvider). That can leave file watchers alive and make Directory.Delete(userSecretsFolder, ...) fail (currently swallowed), leaking secrets folders over time.

Consider grabbing the provider’s Source.FileProvider before disposing the config and dispose it explicitly.

                // Stop watching before removing the folder the watcher is pointed at.
                (config as IDisposable)?.Dispose();

@rosebyte
rosebyte merged commit 3241014 into dotnet:main Aug 3, 2026
84 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Aug 4, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 3, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test Failure: Microsoft.Extensions.Configuration.Test.ConfigurationTests.OnChangeGetFiredForRelativeLinuxPath

4 participants