Repository navigation
Fix FileStream position not updated when sync write partially succeeds - #130687
Merged
Merged
Conversation
When a sync Write partially succeeds (e.g., some bytes are written then an error occurs), the FileStream position was not being updated at all. This was because OSFileStreamStrategy.Write updated _filePosition only after WriteAtOffset returned successfully. For async operations, this was already handled via OnIncompleteOperation. This change fixes the sync path by: 1. Adding a new internal WriteAtOffset(SafeFileHandle, ReadOnlySpan<byte>, ref long) overload to both RandomAccess.Unix.cs and RandomAccess.Windows.cs. The Unix overload updates fileOffset after each successful write so that partial writes update the caller's position even when a subsequent write fails. 2. Changing OSFileStreamStrategy.Write to use the ref overload and pass ref _filePosition directly, so position is updated incrementally. 3. Adding a test that verifies position is correctly tracked during sync writes. Fixes #127854 Co-authored-by: adamsitnik <[email protected]>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-io |
adamsitnik
reviewed
Jul 15, 2026
adamsitnik
left a comment
Member
There was a problem hiding this comment.
@copilot address my feedback
Co-authored-by: adamsitnik <[email protected]>
adamsitnik
marked this pull request as ready for review
July 15, 2026 11:24
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the synchronous FileStream write path so _filePosition can reflect partial progress if a platform RandomAccess.WriteAtOffset implementation performs multiple write syscalls and a later iteration throws.
Changes:
- Update
OSFileStreamStrategy.Write(ReadOnlySpan<byte>)to passref _filePositionintoRandomAccess.WriteAtOffsetinstead of post-incrementing after the call. - Add
WriteAtOffset(..., ref long fileOffset)overloads to the Unix and WindowsRandomAccessimplementations (Unix increments after each successful syscall; Windows increments after a successful call). - Refactor Unix
WriteAtOffsetso the existinglong fileOffsetoverload delegates to the newref longoverload.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/IO/Strategies/OSFileStreamStrategy.cs | Sync Write now updates _filePosition via ref as bytes are written rather than via a post-call increment. |
| src/libraries/System.Private.CoreLib/src/System/IO/RandomAccess.Windows.cs | Introduces a ref long overload to advance a caller-provided offset after a successful write. |
| src/libraries/System.Private.CoreLib/src/System/IO/RandomAccess.Unix.cs | Introduces a ref long overload and moves offset advancement to occur after each successful write iteration. |
jkotas
approved these changes
Jul 15, 2026
Member
|
/ba-g timeouts are unrelated |
This branch was previously deployed
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
OSFileStreamStrategy.Writeupdated_filePositiononly afterRandomAccess.WriteAtOffsetreturned. On Unix,WriteAtOffsetloops over partial writes — if an early iteration succeeded but a later one threw (e.g., ENOSPC), the position was left at its original value despite bytes having been written. The async path already handled this correctly viaOnIncompleteOperation.Changes
RandomAccess.Unix.cs: AddedWriteAtOffset(..., ref long fileOffset)overload containing the write loop.fileOffsetis incremented immediately after each successful syscall — before the next iteration that might throw — so partial progress is always reflected. The existinglongoverload delegates to this with a local copy, preserving behavior for all other callers.RandomAccess.Windows.cs: Added matchingref longoverload for consistency. WindowsWriteFileis all-or-nothing, so it delegates to the existing impl and increments only on success.OSFileStreamStrategy.cs:Writenow passesref _filePositionto the new overload; the separate_filePosition += buffer.Lengthpost-increment is removed.fixes #127854