diff --git a/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs b/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs index c5fdcc698..560cc8d7a 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousPowerShellTask.cs @@ -86,6 +86,7 @@ private IReadOnlyList ExecuteNormally(CancellationToken cancellationTok var invocationSettings = new PSInvocationSettings { AddToHistory = PowerShellExecutionOptions.AddToHistory, + Host = _psesHost }; if (PowerShellExecutionOptions.ThrowOnError) diff --git a/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousTask.cs b/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousTask.cs index d8efdbf4b..5082a4ec0 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousTask.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Execution/SynchronousTask.cs @@ -1,11 +1,11 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -using Microsoft.Extensions.Logging; using System; using System.Runtime.ExceptionServices; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution { @@ -23,6 +23,7 @@ internal abstract class SynchronousTask : ISynchronousTask private readonly TaskCompletionSource _taskCompletionSource; private readonly CancellationToken _taskRequesterCancellationToken; + private CancellationTokenSource _cancellationSource; private bool _executionCanceled; @@ -79,8 +80,8 @@ public void ExecuteSynchronously(CancellationToken executorCancellationToken) return; } - using var cancellationSource = CancellationTokenSource.CreateLinkedTokenSource(_taskRequesterCancellationToken, executorCancellationToken); - if (cancellationSource.IsCancellationRequested) + _cancellationSource = CancellationTokenSource.CreateLinkedTokenSource(_taskRequesterCancellationToken, executorCancellationToken); + if (executorCancellationToken.IsCancellationRequested) { SetCanceled(); return; @@ -88,7 +89,7 @@ public void ExecuteSynchronously(CancellationToken executorCancellationToken) try { - TResult result = Run(cancellationSource.Token); + TResult result = Run(executorCancellationToken); SetResult(result); } catch (OperationCanceledException) diff --git a/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs b/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs index da263e698..7568a7196 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs @@ -714,7 +714,22 @@ public void WriteWithPrompt(PSCommand command, CancellationToken cancellationTok private string InvokeReadLine(CancellationToken cancellationToken) { - return _readLineProvider.ReadLine.ReadLine(cancellationToken); + if (Monitor.TryEnter(_readLineProvider)) + { + try + { + return _readLineProvider.ReadLine.ReadLine(cancellationToken); + } + finally + { + Monitor.Exit(_readLineProvider); + } + } + else + { + var legacyReadLine = new LegacyReadLine(this, ReadKey, OnPowerShellIdle); + return legacyReadLine.ReadLine(cancellationToken); + } } private void InvokeInput(string input, CancellationToken cancellationToken)