Skip to content

Fix FileStream position not updated when sync write partially succeeds - #130687

Merged
adamsitnik merged 2 commits into
mainfrom
copilot/fix-runtime-issue-127854
Jul 15, 2026
Merged

adamsitnik merged 2 commits into
mainfrom
copilot/fix-runtime-issue-127854

Conversation

Copilot AI commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

OSFileStreamStrategy.Write updated _filePosition only after RandomAccess.WriteAtOffset returned. On Unix, WriteAtOffset loops 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 via OnIncompleteOperation.

Changes

  • RandomAccess.Unix.cs: Added WriteAtOffset(..., ref long fileOffset) overload containing the write loop. fileOffset is incremented immediately after each successful syscall — before the next iteration that might throw — so partial progress is always reflected. The existing long overload delegates to this with a local copy, preserving behavior for all other callers.
  • RandomAccess.Windows.cs: Added matching ref long overload for consistency. Windows WriteFile is all-or-nothing, so it delegates to the existing impl and increments only on success.
  • OSFileStreamStrategy.cs: Write now passes ref _filePosition to the new overload; the separate _filePosition += buffer.Length post-increment is removed.

fixes #127854

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]>
Copilot AI requested review from Copilot and removed request for Copilot July 14, 2026 13:50
@azure-pipelines

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

@adamsitnik adamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot address my feedback

Copilot AI requested review from Copilot and removed request for Copilot July 15, 2026 11:14
Copilot AI requested a review from adamsitnik July 15, 2026 11:14

@adamsitnik adamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@adamsitnik
adamsitnik marked this pull request as ready for review July 15, 2026 11:24
Copilot AI review requested due to automatic review settings July 15, 2026 11:24
@azure-pipelines

Copy link
Copy Markdown
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.

Copilot AI temporarily deployed to copilot-pat-pool July 15, 2026 11:25 Inactive
Copilot AI temporarily deployed to copilot-pat-pool July 15, 2026 11:25 Inactive
@adamsitnik
adamsitnik enabled auto-merge (squash) July 15, 2026 11:26
@adamsitnik
adamsitnik requested a review from tmds July 15, 2026 11:26
@adamsitnik
adamsitnik requested a review from jkotas July 15, 2026 11:26
@adamsitnik adamsitnik added this to the 11.0.0 milestone Jul 15, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 pass ref _filePosition into RandomAccess.WriteAtOffset instead of post-incrementing after the call.
  • Add WriteAtOffset(..., ref long fileOffset) overloads to the Unix and Windows RandomAccess implementations (Unix increments after each successful syscall; Windows increments after a successful call).
  • Refactor Unix WriteAtOffset so the existing long fileOffset overload delegates to the new ref long overload.

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.

@adamsitnik

Copy link
Copy Markdown
Member

/ba-g timeouts are unrelated

@adamsitnik
adamsitnik merged commit bbdd508 into main Jul 15, 2026
143 of 147 checks passed
@adamsitnik
adamsitnik deleted the copilot/fix-runtime-issue-127854 branch July 15, 2026 15:57
@dotnet-milestone-bot dotnet-milestone-bot Bot modified the milestones: 11.0.0, 11.0-preview7 Jul 16, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 16, 2026

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 7a3cb93e Deployed Jul 15, 2026 by Copilot via pat_pool #15145
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FileStream position is not updated when sync write partially succeeds

4 participants