Skip to content

Commit de4ac3f

Browse files
author
Kapil Borle
committed
Handle cmdlet invocation exception in analysis service
1 parent 84aac57 commit de4ac3f

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/PowerShellEditorServices/Analysis/AnalysisService.cs

+13-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
using Microsoft.PowerShell.EditorServices.Utility;
77
using System;
88
using System.Linq;
9-
using System.Management.Automation.Runspaces;
109
using System.Threading.Tasks;
1110
using Microsoft.PowerShell.EditorServices.Console;
11+
using System.Management.Automation.Runspaces;
1212
using System.Management.Automation;
1313
using System.Collections.Generic;
1414
using System.Text;
@@ -342,13 +342,21 @@ private PSObject[] InvokePowerShell(string command, IDictionary<string, object>
342342
powerShell.AddParameter(kvp.Key, kvp.Value);
343343
}
344344

345-
var result = powerShell.Invoke();
346-
if (result == null)
345+
var result = new PSObject[0];
346+
try
347+
{
348+
result = powerShell.Invoke()?.ToArray();
349+
}
350+
catch (CmdletInvocationException ex)
347351
{
348-
return new PSObject[0];
352+
// We do not want to crash EditorServices for exceptions caused by cmdlet invocation.
353+
// Two main reasons that cause the exception are:
354+
// * PSCmdlet.WriteOutput being called from another thread than Begin/Process
355+
// * CompositionContainer.ComposeParts complaining that "...Only one batch can be composed at a time"
356+
Logger.Write(LogLevel.Error, ex.Message);
349357
}
350358

351-
return result.ToArray(); ;
359+
return result;
352360
}
353361
}
354362

0 commit comments

Comments
 (0)