From 26d64aa9e6bf64dd61c6f96c52efcbdac8a604c8 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Wed, 11 Mar 2020 16:14:32 -0700 Subject: [PATCH 1/2] Ensure that errors are written to the console --- .../PowerShellContext/PowerShellContextService.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs index 0c3a15988..c308c15bc 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs @@ -645,6 +645,7 @@ public async Task> ExecuteCommandAsync( executionOptions)).ConfigureAwait(false); } + Task writeErrorsToConsoleTask = null; try { // Instruct PowerShell to send output and errors to the host @@ -836,7 +837,7 @@ public async Task> ExecuteCommandAsync( if (executionOptions.WriteErrorsToHost) { // Write the error to the host - this.WriteExceptionToHost(e); + writeErrorsToConsoleTask = this.WriteExceptionToHostAsync(e); } } catch (Exception) @@ -896,6 +897,10 @@ public async Task> ExecuteCommandAsync( if (runspaceHandle != null) { runspaceHandle.Dispose(); + if (writeErrorsToConsoleTask != null) + { + await writeErrorsToConsoleTask.ConfigureAwait(false); + } } this.OnExecutionStatusChanged( @@ -1948,7 +1953,7 @@ internal void WriteOutput( } } - private void WriteExceptionToHost(RuntimeException e) + private Task WriteExceptionToHostAsync(RuntimeException e) { var psObject = PSObject.AsPSObject(e.ErrorRecord); @@ -1963,7 +1968,7 @@ private void WriteExceptionToHost(RuntimeException e) psObject.Properties.Add(note); } - ExecuteCommandAsync(new PSCommand().AddCommand("Microsoft.PowerShell.Core\\Out-Default").AddParameter("InputObject", psObject)); + return ExecuteCommandAsync(new PSCommand().AddCommand("Microsoft.PowerShell.Core\\Out-Default").AddParameter("InputObject", psObject)); } private void WriteError( From a1a2f7686d71d48b9a4e18ee367e2e3fd202a385 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Wed, 11 Mar 2020 16:15:32 -0700 Subject: [PATCH 2/2] Add comment --- .../Services/PowerShellContext/PowerShellContextService.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs index c308c15bc..3af5094c4 100644 --- a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs +++ b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs @@ -837,6 +837,7 @@ public async Task> ExecuteCommandAsync( if (executionOptions.WriteErrorsToHost) { // Write the error to the host + // We must await this after the runspace handle has been released or we will deadlock writeErrorsToConsoleTask = this.WriteExceptionToHostAsync(e); } }