Skip to content

Support file redirection to local named pipe for external applications#27017

Open
jborean93 wants to merge 2 commits intoPowerShell:masterfrom
jborean93:named-pipe-redirection
Open

Support file redirection to local named pipe for external applications#27017
jborean93 wants to merge 2 commits intoPowerShell:masterfrom
jborean93:named-pipe-redirection

Conversation

@jborean93
Copy link
Collaborator

@jborean93 jborean93 commented Mar 12, 2026

PR Summary

Fixes the logic when opening a file for a redirected file path to ensure only 1 HANDLE is opened for local named pipes. This allows redirection to a named pipe where there is only 1 listener.

Also optimizes the number of file HANDLES being opened from 3 to 2 for other files types.

PR Context

Currently trying to redirect to a named pipe will result in the error

All pipe instances are busy. : '\\.\pipe\testpipe'.

This is because the redirection setup will call CreateFileW on the provided path 3 times with the first succeeding then the subsequent 2 fails as the named pipe no longer has an active server listener. This PR bypasses the readonly attribute tests for named pipes as they do not apply to those file types.

While this fixes redirection from an external executable, redirecting from the normal output stream to a named pipe still fails with the same error. This is because the native command processor bypasses Out-File to write the raw bytes directly whereas the normal file redirection goes through Out-File -Path $redirectionPath and the provider API Out-File calls will open the pipe handle as part of the -Path globbing matches. Maybe in the future this could also be solved but that's out of scope for this PR.

Going into a bit more detail, redirection of non-external applications end up calling command.SessionState.Path.GetResolvedProviderPathFromPSPath(filePath, out provider) because > $path is compiled to | Out-File -Path $path. Something around this resolution is opening yet another HANDLE to the pipe causing the subsequent open operation later on to create the StreamWriter to fail as there are no more pipe instances to handle that. This could be solved in a few ways but as this PR is focused just on getting redirection working for external application redirection like can be done in cmd I didn't want to make it more complicated.

PR Checklist

Fixes the logic when opening a file for a redirected file path to ensure
only 1 HANDLE is opened for local named pipes. This allows redirection
to a named pipe where there is only 1 listener.

Also optimizes the number of file HANDLES being opened from 3 to 2 for
other files types.
@jborean93 jborean93 requested a review from a team as a code owner March 12, 2026 21:27
Copilot AI review requested due to automatic review settings March 12, 2026 21:27
Copy link
Contributor

Copilot AI left a comment

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 fixes file redirection to local named pipes (e.g., \\.\pipe\...) and mailslots for external applications in PowerShell on Windows. The root cause was that MasterStreamOpenImpl opened multiple file handles (via File.Exists and new FileInfo()) before the actual FileStream, which failed for named pipes that only allow one client connection. The fix detects named pipe/mailslot paths and skips the read-only attribute check, also optimizing regular files from 3 handle opens to 2 by removing the File.Exists call and relying on FileInfo.Attributes instead.

Changes:

  • Skip the read-only file attribute check for Windows named pipes and mailslots to avoid opening extra handles
  • Optimize regular file handling by replacing File.Exists + FileInfo with just FileInfo (checking Attributes != -1 for existence)
  • Add comprehensive tests for named pipe and mailslot redirection scenarios

Reviewed changes

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

File Description
src/System.Management.Automation/utils/PathUtils.cs Detect named pipe/mailslot paths to skip read-only attribute checks; replace File.Exists with FileInfo.Attributes check
test/powershell/Language/Parser/RedirectionOperator.Tests.ps1 Add tests for redirecting external application output to named pipes, mailslots, and case-insensitive pipe paths

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Mar 20, 2026
@microsoft-github-policy-service
Copy link
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

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

Labels

Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants