From fd5539796f7a2b3750d1889b55a563d84b628bee Mon Sep 17 00:00:00 2001 From: David Knaack Date: Thu, 8 Sep 2022 23:18:50 +0200 Subject: [PATCH] fix(pwsh): avoid potential deadlock in init (#4335) --- src/init/starship.ps1 | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/init/starship.ps1 b/src/init/starship.ps1 index a877aa7f..e97da7d1 100755 --- a/src/init/starship.ps1 +++ b/src/init/starship.ps1 @@ -53,15 +53,21 @@ $null = New-Module starship { } $process = [System.Diagnostics.Process]::Start($startInfo) + # Read the output and error streams asynchronously + # Avoids potential deadlocks when the child process fills one of the buffers + # https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.standardoutput?view=net-6.0#remarks + $stdout = $process.StandardOutput.ReadToEndAsync() + $stderr = $process.StandardError.ReadToEndAsync() + [System.Threading.Tasks.Task]::WaitAll(@($stdout, $stderr)) + # stderr isn't displayed with this style of invocation # Manually write it to console - $stderr = $process.StandardError.ReadToEnd().Trim() - if ($stderr -ne '') { + if ($stderr.Result.Trim() -ne '') { # Write-Error doesn't work here $host.ui.WriteErrorLine($stderr) } - $process.StandardOutput.ReadToEnd(); + $stdout.Result; } function Enable-TransientPrompt {