Repository navigation
Fix intermittent file-lock race in dotnet-watch tests - #55201
Merged
MichaelSimons merged 1 commit intoJul 8, 2026
Merged
Conversation
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]>
Contributor
There was a problem hiding this comment.
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.WriteAllTextto open files withFileShare.Read, allowing concurrent readers while the test writes. - Expands the method’s XML doc comment to explain the file-lock race being avoided.
tmat
approved these changes
Jul 8, 2026
Member
There was a problem hiding this comment.
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
enabled auto-merge (squash)
July 8, 2026 17:57
MichaelSimons
deleted the
michaelsimons-fix-dotnet-watch-test-file-lock-race
branch
July 8, 2026 19:32
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 the intermittent
IOException: The process cannot access the file ... because it is being used by another processfailure inMetadataUpdateHandlerTests.NoActions(and the same race family affectingRuntimeProcessLauncherTests.RelaunchOnCrash).Problem
DotNetWatchTestBase.WriteAllTextusesFile.Open(path, FileMode.OpenOrCreate)which defaults toFileShare.None, exclusively locking the file. When dotnet-watch's file watcher detects the change and Roslyn'sHotReloadMSBuildWorkspace.GetSourceTextAsynctries to read the file concurrently, it throws anIOExceptionif the test still holds the lock.Fix
Specify
FileShare.Readso that dotnet-watch can read the file while the test is writing it:Fixes #55164