Skip to content

Write the received file from the stream for handle based FileStreams - #1869

Merged
SimonCropp merged 1 commit into
mainfrom
fix-handle-filestream-mismatch
Aug 17, 2026
Merged

Write the received file from the stream for handle based FileStreams#1869
SimonCropp merged 1 commit into
mainfrom
fix-handle-filestream-mismatch

Conversation

@SimonCropp

Copy link
Copy Markdown
Member

FileComparer.InnerCompare writes the received file on a mismatch with IoHelpers.CopyFile(fileStream.Name, …). Name 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 from the handle.

The first run of such a test passes: the New path goes through IoHelpers.WriteStream, which already guards for this (TryFileCopy bails on a non rooted name and copies through the handle instead). The next run, once a .verified. file exists and the content differs, throws:

System.Exception : Failed to compare files:
---- System.IO.FileNotFoundException : Could not find file '[Unknown]'.

So WriteStream replaces the raw copy here — same copy-by-path fast path, with the fallback the other paths already get.

HandleStreamTests.Mismatch opens a FileStream over another stream's SafeFileHandle and verifies it against a differing snapshot. Two notes on how it is built:

  • It uses a binary extension. With a text extension the stream is read into a StringBuilder up front and FileComparer is never reached, so the test would pass without proving anything.
  • The two contents are the same length, so the length fast path (which calls WriteStream and 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.Tests passes on both net48 (1221) and net11.0 (1300).

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.
@SimonCropp SimonCropp added this to the 32.0.0 milestone Aug 17, 2026
@SimonCropp
SimonCropp merged commit a3a8875 into main Aug 17, 2026
7 checks passed
@SimonCropp
SimonCropp deleted the fix-handle-filestream-mismatch branch August 17, 2026 13:08
This was referenced Aug 26, 2026
This was referenced Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant