Fix flaky FileWatcherTests.DeleteSubfolder test - #54801
#54801Closed
MichaelSimons wants to merge 4 commits into
maindotnet/sdk:mainfrom
michaelsimons/fix-flaky-delete-subfolder-testdotnet/sdk:michaelsimons/fix-flaky-delete-subfolder-testCopy head branch name to clipboard
Closed
Fix flaky FileWatcherTests.DeleteSubfolder test#54801MichaelSimons wants to merge 4 commits intomaindotnet/sdk:mainfrom michaelsimons/fix-flaky-delete-subfolder-testdotnet/sdk:michaelsimons/fix-flaky-delete-subfolder-testCopy head branch name to clipboard
MichaelSimons wants to merge 4 commits into
maindotnet/sdk:mainfrom
michaelsimons/fix-flaky-delete-subfolder-testdotnet/sdk:michaelsimons/fix-flaky-delete-subfolder-testCopy head branch name to clipboard
Conversation
The TestOperation method previously signaled completion when it received any N unique events (where N = expectedChanges.Length). On Linux with polling, deleting a subdirectory can emit extra events like (subdir, Update) or (subdir, Delete) in addition to the expected file-level delete events. These spurious events filled the count quota before all expected events arrived, causing the assertion to fail. Fix: Change completion condition to wait until all expected events are present in the collected set (subset check), and change the assertion to verify all expected events were observed while tolerating extra OS events. Fixes #49233 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Reduces flakiness in FileWatcherTests.DeleteSubfolder by changing the test harness to wait until all expected filesystem events have been observed, rather than completing after receiving an arbitrary number of unique events (which can be skewed by extra OS-generated noise).
Changes:
- Track expected changes as a set and complete the operation only when the expected set is a subset of observed changes.
- Update test verification to assert that all expected changes were observed (tolerating additional events) and improve diagnostic output.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This was referenced Jun 16, 2026
tmat
reviewed
Jun 17, 2026
| await (Debugger.IsAttached ? task : task.TimeoutAfter(DefaultTimeout)); | ||
|
|
||
| AssertEx.SequenceEqual(expectedChanges, filesChanged.OrderBy(x => x.Path)); | ||
| // Verify all expected changes were observed (extra events from the OS are acceptable) |
Member
There was a problem hiding this comment.
Actually, extra events can cause different watch behavior, so they are generally not acceptable.
Are the events non-deterministic on Linux?
Member
There was a problem hiding this comment.
We can modify the logic so that a test can list specific optional events that may occur.
tmat
requested changes
Jun 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #49233
The
DeleteSubfoldertest was intermittently failing because the test harness (TestOperation) used a count-based completion condition that didn't account for extra filesystem events emitted by the OS.Impact
Per the Known Build Error tracker on the issue, this flaky test is hitting frequently:
These failures cause unnecessary CI noise and re-runs across PRs in the repo.
Root Cause
TestOperationsignaled completion when it received any N unique events (where N =expectedChanges.Length). On Linux with polling,Directory.Delete(subdir, recursive: true)can emit additional events — such as(subdir, Update)or(subdir, Delete)— beyond the expected file-level delete events. These extra events filled the count quota before all expected events arrived, causing the finalSequenceEqualassertion to fail with a mismatch like:Fix
Completion condition: Changed from
filesChanged.Count == expectedChanges.LengthtoexpectedSet.IsSubsetOf(filesChanged)— the test now waits until all expected events have been observed, regardless of how many extra events the OS generates.Assertion: Changed from strict sequence equality to verifying all expected changes are present in the collected set. Extra OS-level events are tolerated since they don't indicate incorrect behavior.
This makes the test resilient to platform-specific filesystem event noise while still verifying the core watcher behavior.