Write the received file from the stream for handle based FileStreams - #1869
Merged
Conversation
The NotEqual path in InnerCompare copied by fileStream.Name, which is not a path for a FileStream built from a handle: .NET Framework reports "[Unknown]", and modern .NET falls back to that when the path cannot be resolved. The first run succeeded, since the New path goes through IoHelpers.WriteStream, and the next mismatch threw "Failed to compare files: Could not find file '[Unknown]'". WriteStream already keeps the copy-by-path fast path and falls back to the handle, so it replaces the raw copy here.
This was referenced Aug 26, 2026
This was referenced Aug 27, 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.
FileComparer.InnerComparewrites the received file on a mismatch withIoHelpers.CopyFile(fileStream.Name, …).Nameis not a path for aFileStreambuilt from a handle — .NET Framework reports[Unknown], and modern .NET falls back to that when the path cannot be resolved from the handle.The first run of such a test passes: the New path goes through
IoHelpers.WriteStream, which already guards for this (TryFileCopybails on a non rooted name and copies through the handle instead). The next run, once a.verified.file exists and the content differs, throws:So
WriteStreamreplaces the raw copy here — same copy-by-path fast path, with the fallback the other paths already get.HandleStreamTests.Mismatchopens aFileStreamover another stream'sSafeFileHandleand verifies it against a differing snapshot. Two notes on how it is built:StringBuilderup front andFileCompareris never reached, so the test would pass without proving anything.WriteStreamand is therefore already safe) does not short-circuit ahead of the comparison.On net48 it reproduces the crash above; on net11.0 the handle resolves back to a real path, so it passes either way there and only asserts the outcome. Full
Verify.Testspasses on both net48 (1221) and net11.0 (1300).