Skip to content

Commit 62ed52e

Browse files
committed
Fix RunspaceHandle hang after command exception
This change fixes an issue in PowerShellContext which causes a RunspaceHandle to not be disposed if an exception is thrown during command execution. A 'finally' block is now used to properly dispose of the RunspaceHandle so that future command executions can proceed.
1 parent d92faae commit 62ed52e

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/PowerShellEditorServices/Session/PowerShellContext.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ public async Task<IEnumerable<TResult>> ExecuteCommand<TResult>(
208208
PSCommand psCommand,
209209
bool sendOutputToHost = false)
210210
{
211+
RunspaceHandle runspaceHandle = null;
212+
211213
// If the debugger is active and the caller isn't on the pipeline
212214
// thread, send the command over to that thread to be executed.
213215
if (Thread.CurrentThread.ManagedThreadId != this.pipelineThreadId &&
@@ -276,7 +278,7 @@ public async Task<IEnumerable<TResult>> ExecuteCommand<TResult>(
276278
GetStringForPSCommand(psCommand)));
277279

278280
// Set the runspace
279-
var runspaceHandle = await this.GetRunspaceHandle();
281+
runspaceHandle = await this.GetRunspaceHandle();
280282
if (runspaceHandle.Runspace.RunspaceAvailability != RunspaceAvailability.AvailableForNestedCommand)
281283
{
282284
this.powerShell.Runspace = runspaceHandle.Runspace;
@@ -298,6 +300,7 @@ await Task.Factory.StartNew<IEnumerable<TResult>>(
298300
);
299301

300302
runspaceHandle.Dispose();
303+
runspaceHandle = null;
301304

302305
if (this.powerShell.HadErrors)
303306
{
@@ -330,6 +333,13 @@ await Task.Factory.StartNew<IEnumerable<TResult>>(
330333
// Write the error to the host
331334
this.WriteExceptionToHost(e);
332335
}
336+
finally
337+
{
338+
if (runspaceHandle != null)
339+
{
340+
runspaceHandle.Dispose();
341+
}
342+
}
333343
}
334344

335345
// TODO: Better result

0 commit comments

Comments
 (0)