Skip to content

fix: handle shared file contents across handles#1507

Merged
vbreuss merged 3 commits into
mainfrom
fix/1131-mockfilestream-shared-contents
Jul 12, 2026
Merged

fix: handle shared file contents across handles#1507
vbreuss merged 3 commits into
mainfrom
fix/1131-mockfilestream-shared-contents

Conversation

@vbreuss

@vbreuss vbreuss commented Jul 12, 2026

Copy link
Copy Markdown
Member

Each MockFileStream kept its own MemoryStream buffer that was only synced to the shared MockFileData.Contents on flush, so a second handle opened with FileShare.ReadWrite never saw writes made through another handle. On top of that, InternalFlush unconditionally wrote its buffer back, so a read-only handle's Flush could clobber another handle's writes.

Track the last-synced Contents reference and reload the buffer from it on every read entry point (Read, ReadByte, ReadAsync, span overloads) when another handle has replaced it. InternalFlush now writes back only when the buffer actually differs from the last synced snapshot, matching the no-op behavior of a real FileStream.Flush with no pending writes.


Each MockFileStream kept its own MemoryStream buffer that was only synced to the shared MockFileData.Contents on flush, so a second handle opened with FileShare.ReadWrite never saw writes made through another handle. On top of that, InternalFlush unconditionally wrote its buffer back, so a read-only handle's Flush could clobber another handle's writes.

Track the last-synced Contents reference and reload the buffer from it on every read entry point (Read, ReadByte, ReadAsync, span overloads) when another handle has replaced it. InternalFlush now writes back only when the buffer actually differs from the last synced snapshot, matching the no-op behavior of a real FileStream.Flush with no pending writes.
@github-actions

github-actions Bot commented Jul 12, 2026

Copy link
Copy Markdown

Test Results

    31 files  ± 0      31 suites  ±0   17s ⏱️ -4s
 1 155 tests + 1   1 147 ✅  - 6    1 💤 ±0  7 ❌ +7 
11 097 runs  +10  10 251 ✅ +3  839 💤 ±0  7 ❌ +7 

For more details on these failures, see this check.

Results for commit 7b85ec1. ± Comparison against base commit 9cb6266.

♻️ This comment has been updated with latest results.

vbreuss added 2 commits July 12, 2026 15:03
The shared-contents regression test ignored the byte count returned by
Stream.Read, which the analyzer flags as CA2022 (inexact read) and the
build treats as an error. Capture the count and assert it equals the
requested length.
The shared-contents fix added public overrides to MockFileStream
(ReadByte and ReadAsync(byte[]) on all frameworks; Read(Span) and
ReadAsync(Memory) where FEATURE_SPAN is defined). Regenerate the
expected public API surface via the AcceptApiChanges test so
VerifyPublicApiForTestingHelpers passes.
@sonarqubecloud

Copy link
Copy Markdown

@vbreuss vbreuss enabled auto-merge (squash) July 12, 2026 13:16
@vbreuss vbreuss merged commit ec68061 into main Jul 12, 2026
11 checks passed
@vbreuss vbreuss deleted the fix/1131-mockfilestream-shared-contents branch July 12, 2026 13:17
@github-actions

Copy link
Copy Markdown

This is addressed in release v22.2.0.

@github-actions github-actions Bot added the state: released Issues that are released label Jul 12, 2026
This was referenced Jul 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

state: released Issues that are released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The mock FileStream class does not handle shared file contents correctly.

1 participant