Skip to content

Fix intermittent file-lock race in dotnet-watch tests - #55201

Merged
MichaelSimons merged 1 commit into
mainfrom
michaelsimons-fix-dotnet-watch-test-file-lock-race
Jul 8, 2026
Merged

MichaelSimons merged 1 commit into
mainfrom
michaelsimons-fix-dotnet-watch-test-file-lock-race

Conversation

@MichaelSimons

Copy link
Copy Markdown
Member

Summary

Fixes the intermittent IOException: The process cannot access the file ... because it is being used by another process failure in MetadataUpdateHandlerTests.NoActions (and the same race family affecting RuntimeProcessLauncherTests.RelaunchOnCrash).

Problem

DotNetWatchTestBase.WriteAllText uses File.Open(path, FileMode.OpenOrCreate) which defaults to FileShare.None, exclusively locking the file. When dotnet-watch's file watcher detects the change and Roslyn's HotReloadMSBuildWorkspace.GetSourceTextAsync tries to read the file concurrently, it throws an IOException if the test still holds the lock.

Fix

Specify FileShare.Read so that dotnet-watch can read the file while the test is writing it:

File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)

Fixes #55164

The WriteAllText helper in DotNetWatchTestBase used File.Open with the
default FileShare.None, which exclusively locks the file. When dotnet-watch
detects the file change and the Roslyn HotReloadMSBuildWorkspace tries to
read the file (via GetSourceTextAsync), it hits an IOException if the test
still holds the lock.

Fix by specifying FileShare.Read so that dotnet-watch can read the file
concurrently while the test is writing it.

Fixes #55164

Co-authored-by: Copilot App <[email protected]>
Copilot AI review requested due to automatic review settings July 8, 2026 16:06
@MichaelSimons
MichaelSimons requested review from a team and tmat as code owners July 8, 2026 16:06

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

Reduces flakiness in dotnet-watch Hot Reload tests by preventing an exclusive file lock during test-driven source updates, which can race with dotnet-watch/Roslyn reading the same file.

Changes:

  • Updates DotNetWatchTestBase.WriteAllText to open files with FileShare.Read, allowing concurrent readers while the test writes.
  • Expands the method’s XML doc comment to explain the file-lock race being avoided.

@tmat tmat left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This makes it better, so approving.

It seems like there is still a potential race condition where the file watcher sees the file change and watch reads the stale content before it's fully written.

More sophisticated approach might be needed if we see tests failing because of this race.

HotReloadMSBuildWorkspace.GetSourceTextAsync might need to attempt to read the file content a few times if it is locked.

@MichaelSimons
MichaelSimons enabled auto-merge (squash) July 8, 2026 17:57
@MichaelSimons
MichaelSimons merged commit 3b043e5 into main Jul 8, 2026
29 checks passed
@MichaelSimons
MichaelSimons deleted the michaelsimons-fix-dotnet-watch-test-file-lock-race branch July 8, 2026 19:32
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MetadataUpdateHandlerTests.NoActions: IOException file used by another process (Program.cs)

3 participants