Fix race condition in WriteLinesToFile transactional mode (#13323)#13477
Open
huulinhnguyen-dev wants to merge 8 commits intodotnet:mainfrom
Open
Conversation
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a race in the built-in WriteLinesToFile task’s transactional overwrite path where concurrent writers could cause File.Move to fail with “file already exists”, by falling back to File.Replace when the destination appears mid-operation.
Changes:
- Add a transactional overwrite recovery path: if
File.Movefails and the destination now exists, retry viaFile.Replacewith a small bounded retry loop. - Rename the flaky/misleading concurrent overwrite test to reflect actual
Overwrite="true"semantics.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Tasks/FileIO/WriteLinesToFile.cs | Adds a File.Replace-based fallback when Move fails due to a concurrent create, improving reliability under parallel builds. |
| src/Tasks.UnitTests/WriteLinesToFile_Tests.cs | Renames a test to better describe the concurrent overwrite behavior being validated. |
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.
Fixes #13323
Context
Concurrent parallel builds writing to the same file with
Overwrite="true"and transactional mode could throwIOException: Cannot create a file when that file already exists. This happened becauseFile.Movefailed when another thread created the target file between theReplacecheck and theMovecall.Changes Made
WriteLinesToFile.cs: whenFile.Movefails withIOException, retry usingFile.Replacesince the target file now existsWriteLinesToFile_Tests.cs: renameTransactionalModePreservesAllData→TransactionalModeSucceedsWithConcurrentOverwritesto accurately reflect test behavior (Overwrite="true"means only the last writer survives, not all data)Testing
Existing test
TransactionalModeSucceedsWithConcurrentOverwritescovers the concurrent overwrite scenario with parallel MSBuild projects.