vscode-powershell icon indicating copy to clipboard operation
vscode-powershell copied to clipboard

Error on Integrated Console Start - PSInvalidOperationException Pipeline Issue

Open dgcode opened this issue 3 years ago • 14 comments

Prerequisites

  • [X] Write a descriptive title.
  • [X] Make sure you are able to repro it on the latest released version
  • [X] Search the existing issues, especially the pinned issues.

Exception report

Oops, something went wrong.
Please report this bug with ALL the details below, including both the 'Environment' and 'Exception' sections.
Please report on GitHub: https://github.com/PowerShell/PSReadLine/issues/new?template=Bug_Report.yaml        
Thank you!

### Environment
PSReadLine: 2.2.5
PowerShell: 2022.6.1
OS: Microsoft Windows 10.0.19044
BufferWidth: 196
BufferHeight: 22

Last 0 Keys:



### Exception

System.Management.Automation.PSInvalidOperationException: The pipeline was not run because a pipeline is already running. Pipelines cannot be run concurrently.
   at System.Management.Automation.Runspaces.PipelineBase.DoConcurrentCheck(Boolean syncCall, Object syncObject, Boolean isInLock)
   at System.Management.Automation.Runspaces.RunspaceBase.DoConcurrentCheckAndAddToRunningPipelines(PipelineBase pipeline, Boolean syncCall)
   at System.Management.Automation.Runspaces.PipelineBase.CoreInvoke(IEnumerable input, Boolean syncCall)
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke[T](IEnumerable input, IList`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke[T](IEnumerable input, PSInvocationSettings settings)
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility.PowerShellExtensions.InvokeAndClear[TResult](PowerShell pwsh, PSInvocationSettings invocationSettings) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\PowerShell\Utility\PowerShellExtensions.cs:line 76
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility.PowerShellExtensions.InvokeCommand[TResult](PowerShell pwsh, PSCommand psCommand, PSInvocationSettings invocationSettings) in 
D:\a\_work\1\s\src\PowerShellEditorServices\Services\PowerShell\Utility\PowerShellExtensions.cs:line 99
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.ExecuteNormally(CancellationToken cancellationToken) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 119
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.Run(CancellationToken cancellationToken) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 70
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.ExecuteSynchronously(CancellationToken executorCancellationToken) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 92
--- End of stack trace from previous location ---
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.get_Result() in D:\a\_work\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 58
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.ExecuteAndGetResult(CancellationToken cancellationToken) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 108
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost.InvokePSCommand[TResult](PSCommand psCommand, PowerShellExecutionOptions executionOptions, CancellationToken cancellationToken) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\PowerShell\Host\PsesInternalHost.cs:line 432
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost.InvokePSCommand(PSCommand psCommand, PowerShellExecutionOptions executionOptions, CancellationToken cancellationToken) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\PowerShell\Host\PsesInternalHost.cs:line 435
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost.OnPowerShellIdle(CancellationToken idleCancellationToken) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\PowerShell\Host\PsesInternalHost.cs:line 1078
   at Microsoft.PowerShell.PSConsoleReadLine.ReadKey()
   at Microsoft.PowerShell.PSConsoleReadLine.InputLoop()
   at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics, CancellationToken cancellationToken, Nullable`1 lastRunStatus)

Screenshot

image

Environment data

PS Version: 7.2.5
PS HostName: Visual Studio Code Host
PSReadLine Version: 2.2.5
PSReadLine EditMode: Windows
OS: 10.0.19041.1 (WinBuild.160101.0800)
BufferWidth: 121
BufferHeight: 65

Steps to reproduce

  • Start VS Code
  • Open a PowerShell script/launch the integrated console

Expected behavior

  • Console should start with no issues

Actual behavior

  • Console continuously spits out the stated error
  • Regular commands can be run without issue

dgcode avatar Jun 26 '22 21:06 dgcode

Additional context:

  • Issue only occurs when using the PowerShell plugin in VS Code since version v2022.5.1
  • I am using latest version Oh My Posh

I am not sure that this is an issue with PSReadLine or the PowerShell plugin in VS Code. Please let me know if you want me to migrate this issue to their repo.

dgcode avatar Jun 26 '22 21:06 dgcode

What happens if you load without profiles?

andyleejordan avatar Jun 27 '22 17:06 andyleejordan

The terminal loads without issue.

Following this thread further, I narrowed down the issue to the following:

# Attempt to load and start Az Predictor
try {
    if ($PSVersionTable.PSVersion.Major -gt 5) {
        Import-Module Az.Tools.Predictor
        Set-PSReadLineOption -PredictionSource History
    }
}
catch {
    Write-Warning "An Error occurred loading Az Predictor: $($Error[0].Exception.Message)" 
}

Since these specific cmdlets were from when Az.Tools.Predictor was in preview, I thought I'd check the documentation and can see that they recommend a different approach to starting it:

# Attempt to load and start Az Predictor
try {
    if ($PSVersionTable.PSVersion.Major -gt 5) {
        Enable-AzPredictor -AllSession
        Set-PSReadLineOption -PredictionViewStyle InlineView
    }
}
catch {
    Write-Warning "An Error occurred loading Az Predictor: $($Error[0].Exception.Message)" 
}

Tried this which resulted in the same error.

dgcode avatar Jun 27 '22 22:06 dgcode

~~Try taking out the Write-Warning. I have a hunch that won't work in a profile on startup of the extension.~~ (I don't know what I was thinking here.)

andyleejordan avatar Jun 27 '22 22:06 andyleejordan

Can confirm that even with a Write-Host, it still errors: image

And with no exception handling: image

dgcode avatar Jun 27 '22 23:06 dgcode

How about narrowing it down between: Import-Module Az.Tools.Predictor and Set-PSReadLineOption -PredictionViewStyle InlineView?

andyleejordan avatar Jun 28 '22 16:06 andyleejordan

With just those two lines in the profile, I still get the issue on version 2022.5.0 and above: image

dgcode avatar Jun 29 '22 00:06 dgcode

Details on the version of Az.Tools.Predictor I am using in case it's helpful: image

dgcode avatar Jun 29 '22 00:06 dgcode

I think you'll need to not import Az.Tools.Predictor when your host is the PSIC, seems broken.

andyleejordan avatar Jun 29 '22 20:06 andyleejordan

Or set [Microsoft.Azure.PowerShell.Tools.AzPredictor.AzPredictorData]::ShowSurveyOnIdle = $False?

andyleejordan avatar Jun 29 '22 20:06 andyleejordan

Sounds like @SeeminglyScience might have a fix.

andyleejordan avatar Jun 29 '22 20:06 andyleejordan

This issue was closed automatically as repro info was indicated as needed, but there has been no activity in over a week. Please feel free to reopen with any available information!

ghost avatar Jul 07 '22 22:07 ghost

Re-opening, have already provided repro info.

dgcode avatar Jul 07 '22 22:07 dgcode

Idea from Patrick: "When we create the initial PowerShell instance, we need to mark it as nested with RunspaceMode.CurrentRunspace. Will probably through an exception, will need to temporarily set a default runspace while we mark it as nested." Currently being done elsewhere in the code. In most cases we don't use the initial instance. Because this is being loaded in the profile specifically, it's hitting an OnIdle run before we've ran something else, hence using the non-nested runspace and failing.

andyleejordan avatar Aug 03 '22 18:08 andyleejordan

@dgcode I think I have a fix, I need to have @SeeminglyScience review it and then we'll test it with you in Preview.

andyleejordan avatar Sep 13 '22 01:09 andyleejordan

Sounds good, cheers!

dgcode avatar Sep 13 '22 01:09 dgcode