Skip to content

[Breaking change]: On Linux, opening a file without FileAccess.Write then locking it, will now lock it with a shared lock #26529

Closed

Description

@carlossanlop

Description

On Unix, when the user opens a file using FileStream, only with a read-only FileAccess permission (no FileAccess.Write), and then attempts to lock a region of the file using FileStream.Lock, the operation should succeed because the file will be locked with a shared or read lock.

using (FileStream fs = File.OpenRead("testfile")) // Opening with FileAccess.Read only
{
    fs.Lock((long) 3, (long) 1); // Attempting to lock a region of the read-only file
}

Version

.NET 6 RC 1

Previous behavior

On Unix, when the user opened a file with FileStream with read permissions only, and then attempted to lock a region of the file using FileStream.Lock, we would attempt to lock the file with a write-lock, which would throw System.UnauthorizedException: Access to the path is denied. .
No exception thrown on Windows (code behaved well).

New behavior

Now, when the user opens a file using FileStream with read permissions only on Unix, and then attempts to lock a region of the file using FileStream.Lock, we lock the file with a read lock (also known as shared lock). No behavior change on Windows.

Type of breaking change

  • Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load/execute or different run-time behavior.
  • Source incompatible: Source code may encounter a breaking change in behavior when targeting the new runtime/component/SDK, such as compile errors or different run-time behavior.

Reason for change

FileStream.Lock(long position, long length) is the API we offer to users that want to lock a specific region of a file. We do not offer an API that lets the user choose the underlying locking method, so the user expects this API to correctly determine what is the appropriate locking method depending on the file permissions. The user does not have an easy workaround for this.

Recommended action

Before .NET 6.0, in order to be able to lock the file, the user would have to either check if the code was being executed in Windows or the FileStream was opened with FileAccess.Write permission, or wrap the FileStream.Lock call with a try catch to capture the System.UnauthorizedException.

On .NET 6.0 and above, such workarounds will not be necessary.

Feature area

Core .NET libraries

Affected APIs

System.IO.FileStream.Lock(System.Int64,System.Int64) (Unix only).

Activity

  1. added
    breaking-change
    on Oct 15, 2021
  2. added
    ⌚ Not Triaged
    🏁 Release: .NET 6
    binary incompatible
    on Oct 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

🏁 Release: .NET 6binary incompatiblebreaking-change

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions