PowerShell / vscode-powershell

Provides PowerShell language and debugging support for Visual Studio Code
https://marketplace.visualstudio.com/items/ms-vscode.PowerShell
MIT License
1.71k stars 491 forks source link

Debug PSTask PSInvalidOperationException #3363

Open AlbertWaelti opened 3 years ago

AlbertWaelti commented 3 years ago

Issue Type: Bug

When debugging a PSTaskJob (Runspace) a get the following exception: .PSInvalidOperationException: The pipeline was not run because a pipeline is already running. Pipelines cannot be run concurrently.

TestScript.ps1

$files = @("file1.txt","file2.txt","file3.txt");

$files | ForEach-Object -AsJob -Parallel {

    Wait-Debugger
    Write-Output "Scanning $($_)";
}

===================================== Command to reproduce in Powershell Integrated Terminal:

PS> .\TestScript.ps1
PS> Get-Runspace
 Id Name            ComputerName    Type          State         Availability
 -- ----            ------------    ----          -----         ------------
  1 Runspace1       localhost       Local         Opened        Available   
  4 Runspace4       localhost       Local         Opened        Busy        
  6 PSTask:1        localhost       Local         Opened        InBreakpoint
  7 PSTask:2        localhost       Local         Opened        InBreakpoint
  8 PSTask:3        localhost       Local         Opened        InBreakpoint
PS> Get-Runspace -Id 6 | Debug-Runspace

Debugging Runspace: PSTask:1
To end the debugging session type the 'Detach' command at the debugger prompt, or type 'Ctrl+C' otherwise.

Entering debugged runspace on local machine NB0001
[DBG]: [Process:17600]: [PSTask:1]: PS C:\Users\Albert\Source\Repos\Test>> l

    1:
    2:
    3:*     Wait-Debugger
    4:      Write-Output "Scanning $($_)";
    5:

[DBG]: [Process:17600]: [PSTask:1]: PS C:\Users\Albert\Source\Repos\Test>> v

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

The Debugger crashes and returns the same exception in a loop

Extension version: 2021.2.2 VS Code version: Code 1.56.2 (054a9295330880ed74ceaedda236253b4f39a335, 2021-05-12T17:13:13.157Z) OS version: Windows_NT x64 10.0.19041

System Info |Item|Value| |---|---| |CPUs|Intel(R) Core(TM) i7-3840QM CPU @ 2.80GHz (8 x 2794)| |GPU Status|2d_canvas: enabled
gpu_compositing: enabled
multiple_raster_threads: enabled_on
oop_rasterization: enabled
opengl: enabled_on
rasterization: enabled
skia_renderer: enabled_on
video_decode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled| |Load (avg)|undefined| |Memory (System)|15.91GB (5.58GB free)| |Process Argv|--crash-reporter-id e3696b06-ea94-400b-b7e5-70c7ac80803d| |Screen Reader|no| |VM|0%|
SydneyhSmith commented 3 years ago

Thanks @AlbertWaelti thanks for providing the detailed repro steps-- we will mark this as a bug, looks like quite the complicated issue--thanks!

andyleejordan commented 3 years ago

Some info, I tried to repro with PowerShell 7 outside VS Code and could not:

> ./test.ps1

Id     Name            PSJobTypeName   State         HasMoreData     Location
--     ----            -------------   -----         -----------     --------  
1      Job1            PSTaskJob       Running       False           PowerShell

@andys-mac-mini ~/src 
> Get-Runspace

 Id Name            ComputerName    Type          State         Availability
 -- ----            ------------    ----          -----         ------------
  1 Runspace1       localhost       Local         Opened        Busy
  2 PSTask:1        localhost       Local         Opened        InBreakpoint
  3 PSTask:2        localhost       Local         Opened        InBreakpoint
  4 PSTask:3        localhost       Local         Opened        InBreakpoint

@andys-mac-mini ~/src 
> Get-Runspace -id 2 | Debug-Runspace
Debugging Runspace: PSTask:1
To end the debugging session type the 'Detach' command at the debugger prompt, or type 'Ctrl+C' otherwise.

Entering debug mode. Use h or ? for help. 

At line:3 char:5
+     Wait-Debugger
+     ~~~~~~~~~~~~~
[DBG]: [Process:14283]: [PSTask:1]: PS /Users/andschwa/src>> l

    1:  
    2:  
    3:*     Wait-Debugger
    4:      Write-Output "Scanning $($_)";
    5:  

[DBG]: [Process:14283]: [PSTask:1]: PS /Users/andschwa/src>> v
At line:4 char:5
+     Write-Output "Scanning $($_)";
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[DBG]: [Process:14283]: [PSTask:1]: PS /Users/andschwa/src>> 

but in the integrated terminal in VS Code, it repros exactly as posted:

[DBG]: [Process:14352]: [PSTask:1]: PS /Users/andschwa/src>> 

An error occurred while reading input:

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.Runspaces.Pipeline.Invoke()
   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 System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at Microsoft.PowerShell.EditorServices.Services.PowerShellContextService.ExecuteCommandAsync[TResult](PSCommand psCommand, StringBuilder errorMessages, ExecutionOptions executionOptions) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShellContext\PowerShellContextService.cs:line 918
   at Microsoft.PowerShell.EditorServices.Services.PowerShellContextService.ExecuteCommandAsync[TResult](PSCommand psCommand, StringBuilder errorMessages, ExecutionOptions executionOptions) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShellContext\PowerShellContextService.cs:line 918
   at Microsoft.PowerShell.EditorServices.Services.PowerShellContextService.ExecuteCommandAsync[TResult](PSCommand psCommand, StringBuilder errorMessages, ExecutionOptions executionOptions) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShellContext\PowerShellContextService.cs:line 918
   at Microsoft.PowerShell.EditorServices.Services.PowerShellContext.PSReadLinePromptContext.InvokeReadLineAsync(Boolean isCommandLine, CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShellContext\Session\PSReadLinePromptContext.cs:line 158
   at Microsoft.PowerShell.EditorServices.Services.PowerShellContextService.InvokeReadLineAsync(Boolean isCommandLine, CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShellContext\PowerShellContextService.cs:line 1173
   at Microsoft.PowerShell.EditorServices.Services.PowerShellContext.EditorServicesPSHostUserInterface.StartReplLoopAsync(CancellationToken cancellationToken) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShellContext\Session\Host\EditorServicesPSHostUserInterface.cs:line 876