Registry Mirror
Skip to content

Conversation

@vbreuss

@vbreuss vbreuss commented Nov 29, 2026

Copy link
Copy Markdown
Member

On Unix, .NET takes an exclusive flock for a stream opened with FileShare.None and a shared flock for any other share mode. An exclusive stream therefore fails while any other stream holds the file, and any stream fails while an exclusive one holds it. The mock only refused a stream when the stream that was already open had FileShare.None:

using var shared = fileSystem.FileStream.New("foo.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
// Real Linux/macOS: IOException. The mock opened the stream.
using var exclusive = fileSystem.FileStream.New("foo.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None);

This also affects the APIs that open with FileShare.None, such as File.Create, File.Open and File.OpenWrite. Only exactly FileShare.None is exclusive; FileShare.Inheritable or FileShare.Delete alone take a shared lock.

On Unix the IOException now also carries the raw EWOULDBLOCK error number as HResult (11 on Linux, 35 on macOS) instead of the Windows sharing violation -2147024864.

…open

On Unix, .NET takes an exclusive `flock` for a stream opened with `FileShare.None` and a shared `flock` for any other share mode. An exclusive stream therefore fails while any other stream holds the file, and any stream fails while an exclusive one holds it. The mock only refused a stream when the stream that was already open had `FileShare.None`:

```csharp
using var shared = fileSystem.FileStream.New("foo.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
// Real Linux/macOS: IOException. The mock opened the stream.
using var exclusive = fileSystem.FileStream.New("foo.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None);
```

This also affects the APIs that open with `FileShare.None`, such as `File.Create`, `File.Open` and `File.OpenWrite`. Only exactly `FileShare.None` is exclusive; `FileShare.Inheritable` or `FileShare.Delete` alone take a shared lock.

On Unix the `IOException` now also carries the raw `EWOULDBLOCK` error number as HResult (11 on Linux, 35 on macOS) instead of the Windows sharing violation `-2147024864`.

The behavior was checked against the real file system on Linux (WSL, Ubuntu 24.04, .NET 10), and the new tests pass there against the real file system.
@vbreuss vbreuss self-assigned this Nov 29, 2026
@vbreuss vbreuss added the bug Something isn't working label Nov 29, 2026
@mergify

mergify Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

This pull request is merged.

@greptile-apps

greptile-apps Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5 · The PR appears safe to merge; no actionable issues were found.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

The PR makes the mock refuse exclusive streams while another stream holds the file on Linux and macOS.

  • Exclusive Unix streams conflict with every other open stream.

Reviews (2) · Last reviewed commit: "test: give the reason for the expected H..." · Reviewed by Greptile

@sonarqubecloud

Copy link
Copy Markdown

@vbreuss
vbreuss enabled auto-merge (squash) November 29, 2026 06:18
@vbreuss
vbreuss merged commit 8d5c18b into main Nov 29, 2026
26 checks passed
@vbreuss
vbreuss deleted the fix/exclusive-file-lock-on-unix branch November 29, 2026 06:25
@github-actions

Copy link
Copy Markdown

🚀 Benchmark Results

Compared to main, which ran on the same runner. Times that change by more than 10% are marked.

Memory

Benchmark main PR Δ
Initialize (N=100) 416,910 B 425,478 B 🔴 +8,568 B (+2%)
RoundTrip (Size=1024) 11,890 B 12,001 B 🔴 +111 B (+1%)
RoundTrip (Size=65536) 413,269 B 413,255 B 🟢 -14 B (±0%)
Enumerate (N=1000&Pattern=*.txt) 280,287 B 280,286 B 🟢 -1 B (±0%)

Time

Benchmark main PR Δ
RoundTrip (Size=1024) 10.62 μs 7.62 μs 🟢 -28%
Chunks 64.23 μs 46.09 μs 🟢 -28%
CompleteTestBenchmarks
Complete Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 39.25 μs 3.962 μs 3.308 μs 1.01 78.38 KB 1.00
SystemIOAbstractions 72.43 μs 0.806 μs 0.715 μs 1.86 166.7 KB 2.13
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 3.69GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

InitializationBenchmarks
Initialize N Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 10 34.28 μs 0.265 μs 0.235 μs 1.00 51.64 KB 1.00
SystemIOAbstractions 10 22.63 μs 0.185 μs 0.173 μs 0.66 48.59 KB 0.94
Testably 100 289.93 μs 1.577 μs 1.475 μs 1.00 415.51 KB 1.00
SystemIOAbstractions 100 173.19 μs 2.154 μs 1.909 μs 0.60 392.78 KB 0.95
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 3.69GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

MockCreationBenchmarks
CreateMock Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 3.298 μs 0.0387 μs 0.0343 μs 1.00 7.14 KB 1.00
SystemIOAbstractions 5.181 μs 0.0245 μs 0.0204 μs 1.57 8.15 KB 1.14
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 3.24GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v3

Job=InProcess  Toolchain=InProcessEmitToolchain  IterationCount=15  
LaunchCount=1  WarmupCount=10  

FileRoundTripBenchmarks
RoundTrip Size Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 1024 7.616 μs 2.2386 μs 2.0940 μs 1.08 11.72 KB 1.00
SystemIOAbstractions 1024 8.185 μs 3.3828 μs 3.1642 μs 1.16 16.54 KB 1.41
Testably 65536 163.817 μs 1.7947 μs 1.6788 μs 1.00 403.57 KB 1.00
SystemIOAbstractions 65536 71.715 μs 0.8692 μs 0.6786 μs 0.44 408.49 KB 1.01
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 3.24GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v3

Job=InProcess  Toolchain=InProcessEmitToolchain  InvocationCount=4096  
IterationCount=15  LaunchCount=1  UnrollFactor=1  
WarmupCount=10  

FileInfoBenchmarks
Inspect Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 2.371 μs 0.2093 μs 0.1958 μs 1.01 1.76 KB 1.00
SystemIOAbstractions 1.327 μs 0.0127 μs 0.0106 μs 0.56 2.55 KB 1.45
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V74 3.68GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  InvocationCount=65536  
IterationCount=15  LaunchCount=1  UnrollFactor=1  
WarmupCount=10  

FileStreamBenchmarks
Chunks Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 46.09 μs 3.645 μs 3.231 μs 1.00 202.25 KB 1.00
SystemIOAbstractions 93.83 μs 3.408 μs 3.021 μs 2.04 326.67 KB 1.62
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 2.45GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v3

Job=InProcess  Toolchain=InProcessEmitToolchain  InvocationCount=2048  
IterationCount=15  LaunchCount=1  UnrollFactor=1  
WarmupCount=10  

DirectoryTreeBenchmarks
MoveTree Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 366.35 μs 15.065 μs 14.092 μs 1.00 230.27 KB 1.00
SystemIOAbstractions 59.97 μs 0.688 μs 0.609 μs 0.16 106.62 KB 0.46
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 7763 2.64GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v3

Job=InProcess  Toolchain=InProcessEmitToolchain  InvocationCount=512  
IterationCount=15  LaunchCount=1  UnrollFactor=1  
WarmupCount=10  

Enumeration10Benchmarks
Enumerate N Pattern Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 10 ***** 5.284 μs 0.0680 μs 0.0603 μs 1.00 5.15 KB 1.00
SystemIOAbstractions 10 * 4.159 μs 0.0193 μs 0.0161 μs 0.79 3.93 KB 0.76
Testably 10 *.txt 4.236 μs 0.0178 μs 0.0166 μs 1.00 4.84 KB 1.00
SystemIOAbstractions 10 *.txt 6.372 μs 0.0349 μs 0.0326 μs 1.50 4.24 KB 0.88
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
Intel Xeon 6975P-C 3.24GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  InvocationCount=16384  
IterationCount=15  LaunchCount=1  UnrollFactor=1  
WarmupCount=10  

Enumeration1000Benchmarks
Enumerate N Pattern Mean Error StdDev Ratio Allocated Alloc Ratio
Testably 1000 ***** 471.3 μs 9.10 μs 8.51 μs 1.00 308.47 KB 1.00
SystemIOAbstractions 1000 * 194.8 μs 6.63 μs 5.54 μs 0.41 70.45 KB 0.23
Testably 1000 *.txt 491.9 μs 8.61 μs 8.05 μs 1.00 273.72 KB 1.00
SystemIOAbstractions 1000 *.txt 362.7 μs 4.29 μs 3.58 μs 0.74 66.9 KB 0.24
BenchmarkDotNet v0.15.8, Linux Ubuntu 26.04.1 LTS (Resolute Raccoon)
AMD EPYC 9V45 4.26GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 11.0.100
  [Host] : .NET 10.0.15 (10.0.15, 10.0.1526.53008), X64 RyuJIT x86-64-v4

Job=InProcess  Toolchain=InProcessEmitToolchain  InvocationCount=512  
IterationCount=15  LaunchCount=1  UnrollFactor=1  
WarmupCount=10  

github-actions Bot added a commit that referenced this pull request Nov 29, 2026
…x and macOS while the file is open (#1262) by Valentin Breuß
github-actions Bot added a commit that referenced this pull request Nov 29, 2026
…x and macOS while the file is open (#1262) by Valentin Breuß
@github-actions

Copy link
Copy Markdown

This is addressed in release v8.1.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working state: released

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant