PowerShell/vscode-powershell

Using PowerShell Preview v2021.11.1, PSReadLine throws an error every so often

Closed this issue · 21 comments

Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest released version
  • 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.0-beta4
PowerShell: 2021.11.1
OS: Microsoft Windows 10.0.22000
BufferWidth: 230
BufferHeight: 26

Last 63 Keys:

 g e t - m o d u l e Enter
 g e t Backspace Backspace Backspace Backspace Backspace $ p s v e r s i o Tab Enter
 UpArrow . p s Tab Enter
 UpArrow Spacebar | g m Enter
 UpArrow Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace g i t q Backspace Tab Enter


### 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.Invoke[T](IEnumerable input, PSInvocationSettings settings)
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility.PowerShellExtensions.InvokeAndClear[TResult](PowerShell pwsh, PSInvocationSettings invocationSettings) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Utility\PowerShellExtensions.cs:line 50
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.ExecuteNormally(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 120
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.Run(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 55
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.ExecuteSynchronously(CancellationToken executorCancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 104
--- End of stack trace from previous location ---
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.get_Result() in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 60
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost.OnPowerShellIdle(CancellationToken idleCancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Host\PsesInternalHost.cs:line 832
   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.0
PS HostName: Visual Studio Code Host
PSReadLine Version: 2.2.0-beta4
PSReadLine EditMode: Windows
OS: 10.0.22000.1 (WinBuild.160101.0800)
BufferWidth: 230
BufferHeight: 26PowerShell Preview
v2021.11.1

Steps to reproduce

On my system.

Starting VSCode, using the Windows Store Powershell or Powershell preview installation.
Using the PowerShell Preview v2021.11.1 extension.

Without doing anything the error provided will pop up every now and then. (writing this generated 5 errors already)
Just having the
It breaks any running scripts I have going.

Using the normal PowerShell extension everything works fine.

Expected behavior

No PSreadline errors while using the PowerShell Preview v2021.11.1 extension.

Actual behavior

I'm unable to use the PowerShell Preview v2021.11.1 extension because of this PSReadline error happening a couple of time every few minute.

@andschwa can you please take a look?

@daxian-dbw Transferred, will look into.

@energetic-it Can you give me more information on how to reproduce this?

@daxian-dbw I think maybe our OnPowerShellIdle function needs to wait to process events until we know that the rest of the startup process has finished and nothing else is running in the pipeline.

The pipeline was not run because a pipeline is already running. Pipelines cannot be run concurrently.

Clearly there's some race happening. My current guess I'd want to investigate if I can reproduce this is that this hacky section of code:

            // We didn't end up executing anything in the background,
            // so we need to run a small artificial pipeline instead
            // to force event processing
            if (runPipelineForEventProcessing)
            {
                InvokePSCommand(new PSCommand().AddScript("0", useLocalScope: true), PowerShellExecutionOptions.Default, CancellationToken.None);
            }

Isn't locked and therefore it's running its fake task after other stuff started running.

Adding comment to follow...

First mentioned here: PowerShell/PSReadLine#3091

I have this consistently. Let me know if there is anything I can provide in the way of logs etc ?

 $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.0-preview.1
PSEdition                      Core
GitCommitId                    7.3.0-preview.1
OS                             Microsoft Windows 10.0.22526
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


gmo psreadline

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     2.2.0      beta4      PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PSReadLineKeyHandler, Set-PSReadLineKeyHandler…}


code-insiders --list-extensions --show-versions
ms-vscode.powershell-preview@2021.12.0
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.0-beta4
PowerShell: 2021.12.0
OS: Microsoft Windows 10.0.22526
BufferWidth: 338
BufferHeight: 63

Last 12 Keys:

 g e t Backspace Backspace Backspace Backspace Backspace c l s Enter


### 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.Invoke[T](IEnumerable input, PSInvocationSettings settings)
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility.PowerShellExtensions.InvokeAndClear[TResult](PowerShell pwsh, PSInvocationSettings invocationSettings) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Utility\PowerShellExtensions.cs:line 50
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.ExecuteNormally(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 120
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.Run(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 55
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.ExecuteSynchronously(CancellationToken executorCancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 104
--- End of stack trace from previous location ---
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousTask`1.get_Result() in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousTask.cs:line 60
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost.OnPowerShellIdle(CancellationToken idleCancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShell\Host\PsesInternalHost.cs:line 825
   at Microsoft.PowerShell.PSConsoleReadLine.ReadKey()
   at Microsoft.PowerShell.PSConsoleReadLine.InputLoop()
   at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics, CancellationToken cancellationToken, Nullable`1 lastRunStatus)

@energetic-it Can you give me more information on how to reproduce this?

What can I do to help out?
For me, following my steps above worked.
Perhaps I wasn't clear somewhere?

@andschwa For that to run, there needs to be event subscribers registered.
@energetic-it and @brwilkinson do you have any event subscribers registered by default? Get-EventSubscriber -Force should return any subscribers.

Good call, I do.. as below, thank you so much 😊

Get-EventSubscriber -Force  | foreach action | foreach command

                if ([Microsoft.Azure.PowerShell.Tools.AzPredictor.AzPredictorData]::ShowSurveyOnIdle) {
                    [Microsoft.Azure.PowerShell.Tools.AzPredictor.AzPredictorData]::ShowSurveyOnIdle = $False
                    Write-Host ''
                    Write-Host ''; Write-Host `Survey: -ForegroundColor $Host.PrivateData.VerboseBackgroundColor -BackgroundColor $host.PrivateData.VerboseForegroundColor -NoNewLine; Write-Host ' How was your experience using the Az Predictor module?'
                    Write-Host ''
                    Write-Host 'Run ' -NoNewline; Write-Host Open-AzPredictorSurvey -ForegroundColor $Host.PrivateData.VerboseBackgroundColor -BackgroundColor $host.PrivateData.VerboseForegroundColor -NoNewline; Write-Host ' to give us your feedback.'
                    Write-Host ''
                    Write-Host '(Use "Ctrl + C" to return to the prompt)'
                }

Thank you so much....

As it turns out I actually have a separate issue open to remove that functionality, so I believe it will be going away in the next release as per this thread.

Azure/azure-powershell#16586

I will follow up over there in regards to this, thanks again, really appreciate the support.

@brwilkinson I believe that event handler comes from the Az.Tools.Predictor. Is this particular exception gone after you remove that module or start PowerShell without auto-load that module?

makes sense for sure, thank you.

I added to the following Unregister-Event -Force to my profile, so I will let you know, how it goes.

    if ( $PSVersionTable.psversion.Major -ge 7 -and $PSVersionTable.PSVersion.Minor -ge 2)
    {
        Enable-AzPredictor
        Set-PSReadLineOption -PredictionViewStyle ListView
        Set-PSReadLineOption -PredictionSource HistoryAndPlugin
        Get-EventSubscriber -Force -SourceIdentifier PowerShell.OnIdle -EA 0 | Unregister-Event -Force
    }

I am so thankful Dongbo, that you were able to discover this... 🙏🏼👍🏼

I haven't any occurrences after unregistering the event subscriber, straight after loading the azPredictor module.

I had the same EventSubscriber, the workaround Ben posted works for me as well.
Thanks Dongbo!

Ah ha! Glad we've figured this out. Now, we certainly shouldn't be crashing due to events, so there is clearly a bug we can solve here, and looks like we have the info we now need to reproduce this. Thanks all!

@andschwa The fix will be tricky. Please take a look at my comment about the problem: PowerShell/PSReadLine#1679 (comment)

We have PowerShell/PowerShellEditorServices#1591 tracking the issue.

@SydneyhSmith we know what the root cause is here, and the workaround is to unregister the events. Since we don't have a timeline for a true bug fix (as that would require a patch to PowerShell itself), we need to determine the best mitigation strategy:

  • Override the PowerShell cmdlets to register an event and make it a no-op with a warning (we should check if this user's report would have been prevented by this action)
  • Document this as a known issue in the troubleshooting doc with a "how to figure out and fix" section
  • Both of the above 😁

@SydneyhSmith Sydney Smith (SHE/HER) FTE we know what the root cause is here, and the workaround is to unregister the events. Since we don't have a timeline for a true bug fix (as that would require a patch to PowerShell itself), we need to determine the best mitigation strategy:

There may already work being done on this.

Azure/azure-powershell#16586

@brwilkinson Ben Wilkinson FTE we introduced this in the preview but there is no setting to remove the message. We will remove this in the upcoming preview.
cc @kceiw

@dcaro or @kceiw did you happen to have any status update on the above? Perhaps we can figure out a timeline?

  • the original request was to remove the message 'azpredictor' rating prompt, however I assume this eventsubscriber, is also prompting for feedback is related and could also be removed?

This is in progress as an Azure PowerShell dev and I have been working on conditionally disabling their event registration so this bug does not show up in the extension. There isn't a good solution, but at least we have a workaround.

@SydneyhSmith We're waiting for Azure PowerShell to release v7.2.1 which should contain the referenced fix (is that correct @VeryEarly?), and then we'll have at least worked around the most prevalent cause of this issue.

Az.Tools.Predictor 1.0 GA'd and should no longer be one of the causes of this issue. Probably other cases that might trigger this so not entirely fixed. There is work arounds linked above

This issue has been marked as external. It has been automatically closed for housekeeping purposes.