Skip to content

Fix race condition in SharedMemoryHelpers.CreateOrOpenFile for named mutexes on Unix#129923

Open
Copilot wants to merge 8 commits into
mainfrom
copilot/fix-nuget-migrations-issue
Open

Fix race condition in SharedMemoryHelpers.CreateOrOpenFile for named mutexes on Unix#129923
Copilot wants to merge 8 commits into
mainfrom
copilot/fix-nuget-migrations-issue

Conversation

Copilot AI commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Two processes racing to create the same named mutex could hit a TOCTOU window where O_RDWR sees ENOENT but O_CREAT|O_EXCL then fails with EEXIST (another process won the race), producing an unhandled IOException.

This mirrors the existing retry pattern already used in EnsureDirectoryExists for the same class of race.

When O_CREAT|O_EXCL fails with EEXIST, another process created the file
between our ENOENT check and our creation attempt. Instead of throwing an
IOException, retry opening the existing file from the top of the loop.

This fixes the race condition that caused:
  System.IO.IOException: The file '...NuGet-Migrations' already exists.
when two processes try to create the same named mutex simultaneously.

Closes #91987

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 27, 2026 06:29
Copilot AI changed the title [WIP] Fix issue with opening device or file specified 'NuGet-Migrations' Fix race condition in SharedMemoryHelpers.CreateOrOpenFile for named mutexes on Unix Jun 27, 2026
Copilot AI requested a review from jkotas June 27, 2026 06:30
@jkotas jkotas marked this pull request as ready for review June 27, 2026 13:57
@jkotas

jkotas commented Jun 27, 2026

Copy link
Copy Markdown
Member

Best reviewed with w=1

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
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.

Pull request overview

This PR updates the Unix shared-memory file open/create path used by named mutexes to be resilient to cross-process creation races, by retrying when the exclusive create attempt fails with EEXIST.

Changes:

  • Wrap SharedMemoryHelpers.CreateOrOpenFile in a retry loop.
  • When O_CREAT|O_EXCL fails with EEXIST, retry by opening the now-existing file instead of throwing.

Copilot AI review requested due to automatic review settings June 27, 2026 14:01

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.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Copilot AI requested review from Copilot and removed request for Copilot June 27, 2026 14:06
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 27, 2026 14:53
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot June 27, 2026 15:27
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants