Skip to content

Commit f9fc163

Browse files
committed
Improve reliability of execution on pipeline thread while debugging
This change improves reliability of command execution on the pipeline thread when stopped at a breakpoint in the debugger. Previously there was a global task variable used to propagate execution results back to the caller of ExecuteCommand, but now we use a specific task variable that's tied to the original ExecuteCommand call to avoid reuse of the global task.
1 parent d1fba04 commit f9fc163

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/PowerShellEditorServices/Session/PowerShellContext.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public class PowerShellContext : IDisposable, IHostSupportsInteractiveSession
4545
private int pipelineThreadId;
4646
private TaskCompletionSource<DebuggerResumeAction> debuggerStoppedTask;
4747
private TaskCompletionSource<IPipelineExecutionRequest> pipelineExecutionTask;
48-
private TaskCompletionSource<IPipelineExecutionRequest> pipelineResultTask;
4948

5049
private object runspaceMutex = new object();
5150
private AsyncQueue<RunspaceHandle> runspaceWaitQueue = new AsyncQueue<RunspaceHandle>();
@@ -414,11 +413,9 @@ public async Task<IEnumerable<TResult>> ExecuteCommand<TResult>(
414413
executionOptions.WriteOutputToHost);
415414

416415
// Send the pipeline execution request to the pipeline thread
417-
this.pipelineResultTask = new TaskCompletionSource<IPipelineExecutionRequest>();
418416
this.pipelineExecutionTask.SetResult(executionRequest);
419417

420-
await this.pipelineResultTask.Task;
421-
return executionRequest.Results;
418+
return await executionRequest.Results;
422419
}
423420
else
424421
{
@@ -1653,8 +1650,6 @@ private void OnDebuggerStop(object sender, DebuggerStopEventArgs e)
16531650

16541651
Logger.Write(LogLevel.Verbose, "Pipeline thread execution completed.");
16551652

1656-
this.pipelineResultTask.SetResult(executionRequest);
1657-
16581653
if (this.CurrentRunspace.Runspace.RunspaceAvailability == RunspaceAvailability.Available)
16591654
{
16601655
if (this.CurrentRunspace.Context == RunspaceContext.DebuggedRunspace)
@@ -1711,8 +1706,12 @@ private class PipelineExecutionRequest<TResult> : IPipelineExecutionRequest
17111706
PSCommand psCommand;
17121707
StringBuilder errorMessages;
17131708
bool sendOutputToHost;
1709+
TaskCompletionSource<IEnumerable<TResult>> resultsTask;
17141710

1715-
public IEnumerable<TResult> Results { get; private set; }
1711+
public Task<IEnumerable<TResult>> Results
1712+
{
1713+
get { return this.resultsTask.Task; }
1714+
}
17161715

17171716
public PipelineExecutionRequest(
17181717
PowerShellContext powerShellContext,
@@ -1724,16 +1723,19 @@ public PipelineExecutionRequest(
17241723
this.psCommand = psCommand;
17251724
this.errorMessages = errorMessages;
17261725
this.sendOutputToHost = sendOutputToHost;
1726+
this.resultsTask = new TaskCompletionSource<IEnumerable<TResult>>();
17271727
}
17281728

17291729
public async Task Execute()
17301730
{
1731-
this.Results =
1731+
var results =
17321732
await this.powerShellContext.ExecuteCommand<TResult>(
17331733
psCommand,
17341734
errorMessages,
17351735
sendOutputToHost);
17361736

1737+
this.resultsTask.SetResult(results);
1738+
17371739
// TODO: Deal with errors?
17381740
}
17391741
}

0 commit comments

Comments
 (0)