Skip to content

Commit dec8250

Browse files
committed
(MODULES-8748) Improve pipe reading in the PowerShell Pipe Server
Previously the powershell manager was modified to read in data from the pipe in a single call. However in buffered environments, the pipe will read data in chunks. This is supported by the Read method [1] > This might be less than the number of bytes requested if that number of bytes > is not currently available In this commit, the reader will attempt to read all bytes in a single call and then continue to read the remaining bytes from the pipe until all of the expected bytes are read. [1] https://docs.microsoft.com/en-us/dotnet/api/system.io.pipes.pipestream.read?view=netcore-2.0
1 parent c9a869d commit dec8250

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

lib/puppet_x/templates/powershell/init_ps.ps1

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -723,10 +723,23 @@ function ConvertTo-PipeCommand
723723

724724
# Read blocks until all bytes are read or EOF / broken pipe hit - tested with 5MB and worked fine
725725
$parsed.RawData = New-Object Byte[] $parsed.Length
726-
$read = $Stream.Read($parsed.RawData, 0, $parsed.Length)
727-
if ($read -lt $parsed.Length)
726+
$readBytes = 0
727+
do {
728+
$attempt = $attempt + 1
729+
# This will block if there's not enough data in the pipe
730+
$read = $Stream.Read($parsed.RawData, $readBytes, $parsed.Length - $readBytes)
731+
if ($read -eq 0)
732+
{
733+
throw "Catastrophic failure: Expected $($parsed.Length - $readBytesh) raw bytes, but the pipe reached an end of stream"
734+
}
735+
736+
$readBytes = $readBytes + $read
737+
Write-SystemDebugMessage -Message "Read $($read) bytes from the pipe"
738+
} while ($readBytes -lt $parsed.Length)
739+
740+
if ($readBytes -lt $parsed.Length)
728741
{
729-
throw "Catastrophic failure: Expected $($parsed.Length) raw bytes, only received $read"
742+
throw "Catastrophic failure: Expected $($parsed.Length) raw bytes, only received $readBytes"
730743
}
731744

732745
# turn the raw bytes into the expected encoded string!

0 commit comments

Comments
 (0)