Skip to content

Unmap empty memory-mapped views on Unix - #136793

Open
RutulPatel8 wants to merge 4 commits into
dotnet:mainfrom
RutulPatel8:fix/mmf-unix-empty-view-leak-133744
Open

RutulPatel8 wants to merge 4 commits into
dotnet:mainfrom
RutulPatel8:fix/mmf-unix-empty-view-leak-133744

Conversation

@RutulPatel8

@RutulPatel8 RutulPatel8 commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #133744

An empty Unix view maps one byte because mmap rejects a zero-length mapping. Its safe handle reports a logical length of zero, so disposal previously called munmap(address, 0) and leaked the native mapping.

Use a minimum unmap length of one byte in SafeMemoryMappedViewHandle.ReleaseHandle, preserving the existing zero-length view semantics. The test asserts that an empty view still reports a zero-length handle.

Validation: the System.IO.MemoryMappedFiles library builds across its configured targets, and the Unix test project builds with zero warnings or errors. The Unix test cannot run on this Windows host.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 1, 2026
@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.

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.

🟢 Approval recommended

No active review issues remain for the empty-view mapping disposal fix.

No open issues 🎉

What changed in this PR

Fixes the Unix empty-view mapping leak described in #133744 by recording the actual native mapping length so disposal can unmap it.

Changes:

  • Records the one-byte native size used when mapping an empty view.
  • Adds a Linux regression test using mincore to verify the mapping exists before disposal and is released afterward.
File Description
src/​libraries/​System.IO.MemoryMappedFiles/​tests/​SafeMemoryMappedViewHandleTests.cs Adds the Linux disposal regression test and mincore interop declaration.
src/​libraries/​System.IO.MemoryMappedFiles/​src/​System/​IO/​MemoryMappedFiles/​MemoryMappedView.Unix.cs Records and uses the actual native size for empty mappings.

Review effort: Balanced

Assert.Equal(0, MinCore(address, 1, &residency));
}

Assert.Equal(-1, MinCore(address, 1, &residency));

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.

What guarantees that some other thread won't map something else at this address?

@RutulPatel8

Copy link
Copy Markdown
Contributor Author

I pushed e6d2209 to address the latest review: the empty-view native mapping length is now a shared internal constant used by creation and release, and the zero-length handle test runs on all platforms. The new CI run is in progress. @jkotas, could you take another look when it completes?

@jkotas

jkotas commented Nov 17, 2026

Copy link
Copy Markdown
Member

The newly added test is failing on Windows:

    System.IO.MemoryMappedFiles.Tests.SafeMemoryMappedViewHandleTests.EmptyViewHandleRetainsZeroLength [FAIL]
      Assert.Equal() Failure: Values differ
      Expected: 0
      Actual:   4096
      Stack Trace:
        /_/src/libraries/System.IO.MemoryMappedFiles/tests/SafeMemoryMappedViewHandleTests.cs(75,0): at System.IO.MemoryMappedFiles.Tests.SafeMemoryMappedViewHandleTests.EmptyViewHandleRetainsZeroLength()
        /_/src/coreclr/System.Private.CoreLib/src/System/Reflection/IntrinsicInvokeHelper.cs(837,0): at System.Reflection.IntrinsicInvokeHelper.Instance_Void_0(IntPtr fn, Object obj, IntPtr* _, RuntimeType __)
        /_/src/coreclr/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.CoreCLR.cs(33,0): at System.Reflection.MethodBaseInvoker.InvokeWithSharedThunk(Object obj, IntPtr* args)
        /_/src/libraries/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs(134,0): at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

So it looks like Windows implementation does not hide the size of the dummy region from the public API. It changes the breaking change point of view for your original fix. We want both Windows and Unix implementations to behave same way, and it makes more sense to align with Windows implementation since it is older (ie more code out there depends on its corner-case behaviors).

This branch has not been deployed

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

Labels

area-System.IO community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MemoryMappedView leak

3 participants