Skip to content

Fix flaky PhysicalFileProvider TokensFiredForOldAndNewNamesOnRename test - #130977

Merged
svick merged 3 commits into
dotnet:mainfrom
svick:physicalfileprovider-token-rename-test
Jul 21, 2026
Merged

svick merged 3 commits into
dotnet:mainfrom
svick:physicalfileprovider-token-rename-test

Conversation

@svick

@svick svick commented Jul 17, 2026

Copy link
Copy Markdown
Member

Fixes #129027.

Problem

TokensFiredForOldAndNewNamesOnRename waited a fixed 500ms after triggering the rename before asserting that the tokens had fired. PhysicalFilesWatcher.CancelToken cancels the token on a background thread-pool task, so under load (e.g. JitStress on slow arm64) the cancellation was not guaranteed to run within that window, causing the test to intermittently fail.

Fix

Replace the fixed delay with the deterministic RegisterChangeCallback + TaskCompletionSource pattern already used by the sibling TokensFiredForNewDirectoryContentsOnRename test, awaiting the callbacks with a generous timeout instead.

A second commit refactors the test's timeout values: the repeated 30-second token-fire timeout is extracted into a shared s_maxWaitForTokenToFire field, and the existing WaitTimeForTokenToFire/WaitTimeForTokenCallback constants are converted to TimeSpan fields for consistency. No behavioral change.

Note

This PR was generated with the assistance of GitHub Copilot.

svick and others added 2 commits July 17, 2026 17:39
The test waited a fixed 500ms after triggering the rename before asserting
that the tokens had fired. PhysicalFilesWatcher.CancelToken cancels the token
on a background thread-pool task, so under load (e.g. JitStress on slow arm64)
the cancellation was not guaranteed to run within that window, causing the
test to intermittently fail.

Replace the fixed delay with the deterministic RegisterChangeCallback +
TaskCompletionSource pattern already used by the sibling
TokensFiredForNewDirectoryContentsOnRename test, awaiting the callbacks with a
generous timeout instead.

Fixes dotnet#129027

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 7e73cf9d-189a-4b07-96d7-156844a6d647
Extract the repeated 30-second token-fire timeout into a single
s_maxWaitForTokenToFire field and convert the existing WaitTimeForTokenToFire
and WaitTimeForTokenCallback constants to TimeSpan fields for consistency.
No behavioral change.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 7e73cf9d-189a-4b07-96d7-156844a6d647
Copilot AI review requested due to automatic review settings July 17, 2026 15:43
@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.

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

This PR improves reliability of Microsoft.Extensions.FileProviders.Physical tests by removing a timing-based assertion from TokensFiredForOldAndNewNamesOnRename and replacing it with a deterministic callback + TaskCompletionSource wait (with a generous timeout). It also refactors the various timeout constants to TimeSpan fields for consistency and reuse.

Changes:

  • Refactor test timeouts from const int millisecond values to shared TimeSpan fields.
  • Fix TokensFiredForOldAndNewNamesOnRename flakiness by awaiting RegisterChangeCallback completion (instead of Task.Delay(500)).
  • Reuse a shared max-wait timeout (s_maxWaitForTokenToFire) in the rename and polling tests.

Address review feedback: use TaskCompletionSource<bool> instead of
TaskCompletionSource<object> for the token-fired signals, which keeps the
intent explicit and avoids boxing. Apply the same change to the existing
signals in TokensFiredForNewDirectoryContentsOnRename for consistency.

The non-generic TaskCompletionSource is not used because the test project
also targets .NET Framework, where it is unavailable.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 7e73cf9d-189a-4b07-96d7-156844a6d647
Copilot AI review requested due to automatic review settings July 17, 2026 15:59
@tarekgh tarekgh added this to the 11.0.0 milestone Jul 17, 2026

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 no new comments.

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

@github-actions

github-actions Bot commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow state for the Holistic Review Orchestrator.

{
  "version": 5,
  "last_dispatched_commit": "ccd435e01edb8d1f92a9d4eac902298d921d6d00",
  "last_dispatched_base_ref": "main",
  "last_dispatched_base_sha": "034f7f5c843988fd82ddfb8369b27c67ab986c86",
  "last_reviewed_commit": "ccd435e01edb8d1f92a9d4eac902298d921d6d00",
  "last_reviewed_base_ref": "main",
  "last_reviewed_base_sha": "034f7f5c843988fd82ddfb8369b27c67ab986c86",
  "last_recorded_worker_run_id": "29741184422",
  "review_attempt_commit": "",
  "review_attempt_base_ref": "",
  "review_attempt_count": 0,
  "max_review_attempts": 5,
  "review_history_format": "holistic-review-disclosure-v1",
  "review_history": [
    {
      "commit": "ccd435e01edb8d1f92a9d4eac902298d921d6d00",
      "review_id": 4734863974
    }
  ]
}

@github-actions github-actions Bot 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.

Holistic Review

Motivation: The TokensFiredForOldAndNewNamesOnRename test (issue #129027) waited a fixed 500ms after triggering a rename before asserting the tokens had fired. Because PhysicalFilesWatcher.CancelToken cancels tokens on a background thread-pool task, under load (e.g. JitStress on slow arm64) the cancellation could exceed that window, causing intermittent failures.

Approach: The fixed Task.Delay is replaced with the deterministic RegisterChangeCallback + TaskCompletionSource pattern already used by the sibling TokensFiredForNewDirectoryContentsOnRename test, awaiting both callbacks via Task.WhenAll(...).WaitAsync(s_maxWaitForTokenToFire) with a generous 30s timeout. A follow-up refactor extracts the repeated 30s timeout into a shared s_maxWaitForTokenToFire field and converts the existing WaitTimeForTokenToFire/WaitTimeForTokenCallback int constants into TimeSpan fields, and switches the token-fired TaskCompletionSource<object> signals to TaskCompletionSource<bool>.

Summary: This is a well-scoped, test-only change that correctly converts a timing-sensitive assertion into a deterministic wait. TaskCreationOptions.RunContinuationsAsynchronously is used to avoid running continuations inline on the watcher thread, matching best practice. The WaitAsync timeout preserves the test's ability to fail (rather than hang) if a token never fires, and the assertions on HasChanged still validate the observable behavior. The refactor is mechanical and non-behavioral; retaining TaskCompletionSource<bool> (rather than the non-generic form) is appropriately justified by .NET Framework targeting. No correctness, performance, or convention concerns.

LGTM.

Note

This review was generated by this repository's Holistic Review agentic workflow to complement the built-in Copilot review.

Generated by Holistic Review · 41.5 AIC · ⌖ 15.9 AIC · ⊞ 10K

@svick
svick merged commit 73197a5 into dotnet:main Jul 21, 2026
88 of 90 checks passed
@svick
svick deleted the physicalfileprovider-token-rename-test branch July 21, 2026 08:13
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.FileProviders.PhysicalFileProviderTests.TokensFiredForOldAndNewNamesOnRename

5 participants