Skip to content

Unmap empty memory-mapped views on Unix - #136793

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

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

Conversation

@RutulPatel8

Copy link
Copy Markdown
Contributor

Fixes #133744

When a zero-length Unix view is created, mmap maps one byte because a zero-length mapping is invalid. The view handle was initialized with the requested native size of zero, so disposal called munmap(address, 0) and leaked the mapping.

Record the actual one-byte native mapping size. Add a Linux regression test that checks the address is mapped while the view is alive and unmapped after disposal using mincore.

Validation: System.IO.MemoryMappedFiles library and test projects build successfully across their configured targets. The Linux test could not run on this Windows host; CI should execute it.

@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?

using MemoryMappedFile mmf = MemoryMappedFile.CreateNew(null, 4096);
using MemoryMappedViewStream view = mmf.CreateViewStream(4096, 0);

// ReleaseHandle passes ByteLength to munmap. A zero-length view still maps one byte.

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.

Can we avoid this Unix-specific behavior change?

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