Skip to content

FileStream position is not updated when sync write partially succeeds #127854

Closed

Description

@tmds

For async operations, FileStream position is updated if a write is only partial. For example:

For sync operations, if the write succeeds only partially the position on the FileStream is not updated:

RandomAccess.WriteAtOffset(_fileHandle, buffer, _filePosition);
_filePosition += buffer.Length;

cc @adamsitnik

Activity

  1. added
    untriaged
    on May 6, 2026
  2. dotnet-policy-service commented on May 6, 2026

    @dotnet-policy-service
    Contributor

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

  3. adamsitnik commented on May 6, 2026

    @adamsitnik
    Member

    For sync operations, if the write succeeds only partially the position on the FileStream is not updated:

    RandomAccess.WriteAtOffset is responsible for ensuring that it writes all bytes:

    // The write completed successfully but for fewer bytes than requested.
    // We need to try again for the remainder.
    buffer = buffer.Slice(bytesWritten);
    fileOffset += bytesWritten;

  4. tmds commented on May 6, 2026

    @tmds
    MemberAuthor

    When this loop round on a partial write, and on the next iteration it throws at FileStreamHelpers.CheckFileCall, then FileStream position does not match that of the partial write.

    Is that the intended behavior?

  5. adamsitnik commented on May 6, 2026

    @adamsitnik
    Member

    Is that the intended behavior?

    I am not 100% sure, from my experience we usually don't ensure a valid state after an exception is throw. At the same time, I would not mind passing the offset by ref to this method to get it updated on every iteration.

    @jkotas @stephentoub what is our official guidance?

  6. tmds commented on May 6, 2026

    @tmds
    MemberAuthor

    Because the async methods handle this, I'd expect the sync methods to handle it too.

  7. jkotas commented on May 7, 2026

    @jkotas
    Member

    I think it makes sense to try to maintain valid consistent state for these types of errors.

    we usually don't ensure a valid state after an exception is throw.

    It primarily applies to fatal state situations where it is not reasonable to do that. For example, if a lock throws an exception and we need to take the lock to update the state to be valid.

  8. removed
    untriaged
    on Jul 14, 2026
  9. added this to the Future milestone on Jul 14, 2026
  10. modified the milestones: Future, 11.0.0 on Jul 15, 2026
  11. added a commit that references this issue on Jul 15, 2026
    bbdd508
  12. locked and limited conversation to collaborators on Aug 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions