Skip to content

Fix maxNumberOfServerInstances==1 implying first-instance semantics on Unix for NamedPipeServerStream#131258

Draft
adamsitnik with Copilot wants to merge 1 commit into
mainfrom
copilot/fix-socket-behavior
Draft

Fix maxNumberOfServerInstances==1 implying first-instance semantics on Unix for NamedPipeServerStream#131258
adamsitnik with Copilot wants to merge 1 commit into
mainfrom
copilot/fix-socket-behavior

Conversation

Copilot AI commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

On Windows, maxNumberOfServerInstances: 1 automatically sets FILE_FLAG_FIRST_PIPE_INSTANCE, so a second process attempting to bind the same pipe name gets UnauthorizedAccessException. On Unix, first-instance semantics were only applied when PipeOptions.FirstPipeInstance was explicitly set -- a second process with maxNumberOfServerInstances: 1 would silently Unlink() the existing socket and rebind, hijacking the pipe.

Root cause: In SharedServer.Get, isFirstPipeInstance was derived solely from PipeOptions.FirstPipeInstance, ignoring maxCount.

Fix: One-line change in NamedPipeServerStream.Unix.cs -- treat maxCount == 1 as first-instance, matching Windows behavior:

// Before
bool isFirstPipeInstance = (pipeOptions & PipeOptions.FirstPipeInstance) != 0;

// After
bool isFirstPipeInstance = (pipeOptions & PipeOptions.FirstPipeInstance) != 0 || maxCount == 1;

With this change, when a cross-process second server creation is attempted with maxNumberOfServerInstances: 1, socket.Bind() fails against the existing socket (no pre-unlink), and the existing SocketException catch converts it to UnauthorizedAccessException.

Test added: NamedPipe_MaxOneInstance_Throws_WhenNameIsUsedAcrossProcesses in NamedPipeTest.CrossProcess.cs -- uses RemoteExecutor to verify cross-process enforcement.

Note: Orphaned sockets after a server crash remain a known limitation on Unix (vs. Windows kernel objects which auto-clean). This equally affects PipeOptions.FirstPipeInstance and is pre-existing behavior.

… Unix, add cross-process test

Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 23, 2026 12: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.

Copilot was unable to review this pull request because there is no eligible user to bill. To allow Copilot reviews on bot-authored pull requests, enable direct organization billing in your organization's Copilot settings.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI changed the title Fix #131203: maxNumberOfServerInstances==1 implies first-instance semantics on Unix Fix maxNumberOfServerInstances==1 implying first-instance semantics on Unix for NamedPipeServerStream Jul 23, 2026
Copilot AI requested a review from adamsitnik July 23, 2026 12:02
@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.

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