diff --git a/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs b/src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs index 0c3a15988..3af5094c4 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,8 @@ public async Task> ExecuteCommandAsync( if (executionOptions.WriteErrorsToHost) { // Write the error to the host - this.WriteExceptionToHost(e); + // We must await this after the runspace handle has been released or we will deadlock + writeErrorsToConsoleTask = this.WriteExceptionToHostAsync(e); } } catch (Exception) @@ -896,6 +898,10 @@ public async Task> ExecuteCommandAsync( if (runspaceHandle != null) { runspaceHandle.Dispose(); + if (writeErrorsToConsoleTask != null) + { + await writeErrorsToConsoleTask.ConfigureAwait(false); + } } this.OnExecutionStatusChanged( @@ -1948,7 +1954,7 @@ internal void WriteOutput( } } - private void WriteExceptionToHost(RuntimeException e) + private Task WriteExceptionToHostAsync(RuntimeException e) { var psObject = PSObject.AsPSObject(e.ErrorRecord); @@ -1963,7 +1969,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(