PowerShell / vscode-powershell

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

The state of the current PowerShell instance is not valid for this operation #1762

Closed PrzemyslawKlys closed 4 years ago

PrzemyslawKlys commented 5 years ago

System Details

System Details Output

### VSCode version: 1.31.1 1b8e8302e405050205e69b59abb3559592bb9e60 x64

### VSCode extensions:
aaron-bond.better-comments@2.0.3
abusaidm.html-snippets@0.2.1
adelphes.google-search-ext@0.2.1
akamud.vscode-theme-onedark@2.1.0
AlanWalk.markdown-toc@1.5.6
alefragnani.Bookmarks@10.2.2
andrew-lis.workbench@0.1.7
auchenberg.vscode-browser-preview@0.3.0
azemoh.one-monokai@0.3.7
ban.spellright@3.0.24
batisteo.vscode-django@0.19.0
bierner.github-markdown-preview@0.0.2
bierner.markdown-checkbox@0.1.1
bierner.markdown-emoji@0.0.7
bierner.markdown-preview-github-styles@0.1.4
bierner.markdown-yaml-preamble@0.0.2
DavidAnson.vscode-markdownlint@0.24.0
DavidWang.ini-for-vscode@0.0.4
daylerees.rainglow@1.5.2
donjayamanne.githistory@0.4.6
donjayamanne.jupyter@1.1.4
donjayamanne.python-extension-pack@1.4.0
DotJoshJohnson.xml@2.4.0
dracula-theme.theme-dracula@2.16.0
dunstontc.dark-plus-syntax@0.0.149
eamodio.gitlens@9.5.1
ecmel.vscode-html-css@0.2.0
EditorConfig.EditorConfig@0.13.0
emilast.LogFileHighlighter@2.4.3
emmanuelbeziat.vscode-great-icons@2.1.46
Equinusocio.vsc-material-theme@2.7.0
esbenp.prettier-vscode@1.8.1
felixfbecker.php-debug@1.12.6
formulahendry.code-runner@0.9.7
gerane.Theme-dimmed-monokai@0.0.2
gharveymn.nightswitch@1.1.0
GitHub.vscode-pull-request-github@0.4.0
GlenBuktenica.unicode-substitutions@2.0.1
GrapeCity.gc-excelviewer@2.1.29
hdg.live-html-previewer@0.3.0
hnw.vscode-auto-open-markdown-preview@0.0.4
HookyQR.beautify@1.4.9
IBM.output-colorizer@0.1.2
ikappas.phpcs@1.0.5
ionutvmi.reg@0.0.2
JerryHong.autofilename@0.1.3
joaompinto.asciidoctor-vscode@2.3.3
johnpapa.winteriscoming@0.8.8
Kelvin.vscode-sshfs@1.12.3
kisstkondoros.vscode-codemetrics@1.17.4
KnisterPeter.vscode-github@0.30.1
liximomo.remotefs@0.0.13
magicstack.MagicPython@1.1.0
mariorodeghiero.vue-theme@1.1.2
mechatroner.rainbow-csv@1.0.0
mkloubert.vs-deploy@14.0.0
mkloubert.vscode-remote-workspace@0.41.0
ms-mssql.mssql@1.4.0
ms-python.python@2019.1.0
ms-vscode.csharp@1.17.1
ms-vscode.PowerShell@1.11.0
ms-vscode.PowerShell-Preview@2.0.0
ms-vsliveshare.vsliveshare@0.3.1182
ms-vsts.team@1.144.1
msjsdiag.debugger-for-chrome@4.11.2
quicktype.quicktype@12.0.46
redhat.java@0.38.0
redhat.vscode-yaml@0.2.1
robertohuertasm.vscode-icons@8.1.0
seedess.vscode-remote-editor@0.0.1
Shan.code-settings-sync@3.2.5
stkb.rewrap@1.9.1
streetsidesoftware.code-spell-checker@1.6.10
streetsidesoftware.code-spell-checker-polish@1.0.0
swyphcosmo.spellchecker@1.2.13
teabyii.ayu@0.18.0
techer.open-in-browser@2.0.0
TomasHubelbauer.zip-file-system@1.0.0
vangware.dark-plus-material@2.0.4
VisualStudioExptTeam.vscodeintellicode@1.1.3
vscjava.vscode-java-debug@0.16.0
vscjava.vscode-java-dependency@0.3.0
vscjava.vscode-java-pack@0.5.0
vscjava.vscode-java-test@0.14.0
vscjava.vscode-maven@0.14.2
WakaTime.vscode-wakatime@1.2.5
wayou.vscode-todo-highlight@1.0.4
wesbos.theme-cobalt2@2.1.6
whizkydee.material-palenight-theme@1.9.4
wholroyd.jinja@0.0.8
xaver.clang-format@1.9.0
yzhang.markdown-all-in-one@2.1.0
zhuangtongfa.Material-theme@2.18.2
Zignd.html-css-class-completion@1.11.0

### PSES version:

### PowerShell version:

Name                           Value
----                           -----
PSVersion                      5.1.17763.316
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.316
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Issue Description

I am experiencing a problem with...

2019-02-17 16:37:29.889 [ERROR] tid:4 in 'OnListenTaskCompleted' C:\PowerShellEditorServices\src\PowerShellEditorServices.Protocol\MessageProtocol\ProtocolEndpoint.cs: line 391
    ProtocolEndpoint message loop terminated due to unhandled exception:

    System.AggregateException: One or more errors occurred. ---> System.Management.Automation.InvalidPowerShellStateException: The state of the current PowerShell instance is not valid for this operation.
       at Microsoft.PowerShell.EditorServices.PowerShellContext.<ExecuteCommandAsync>d__75`1.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at Microsoft.PowerShell.EditorServices.PowerShellContext.<ExecuteCommandAsync>d__75`1.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.PowerShell.EditorServices.PowerShellContext.<SetWorkingDirectoryAsync>d__106.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.PowerShell.EditorServices.Protocol.Server.DebugAdapter.<HandleLaunchRequestAsync>d__26.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.MessageDispatcher.<DispatchMessageAsync>d__7.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.ProtocolEndpoint.<ListenForMessagesAsync>d__36.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.PowerShell.EditorServices.Utility.AsyncContext.Start(Func`1 asyncMainFunc, ILogger logger)
       at System.Threading.Tasks.Task.Execute()
       --- End of inner exception stack trace ---
    ---> (Inner Exception #0) System.Management.Automation.InvalidPowerShellStateException: The state of the current PowerShell instance is not valid for this operation.
       at Microsoft.PowerShell.EditorServices.PowerShellContext.<ExecuteCommandAsync>d__75`1.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at Microsoft.PowerShell.EditorServices.PowerShellContext.<ExecuteCommandAsync>d__75`1.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.PowerShell.EditorServices.PowerShellContext.<SetWorkingDirectoryAsync>d__106.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.PowerShell.EditorServices.Protocol.Server.DebugAdapter.<HandleLaunchRequestAsync>d__26.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.MessageDispatcher.<DispatchMessageAsync>d__7.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol.ProtocolEndpoint.<ListenForMessagesAsync>d__36.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.PowerShell.EditorServices.Utility.AsyncContext.Start(Func`1 asyncMainFunc, ILogger logger)
       at System.Threading.Tasks.Task.Execute()<---

2019-02-17 16:37:29.890 [ERROR] tid:4 in 'ProtocolEndpoint_UnhandledException' C:\PowerShellEditorServices\src\PowerShellEditorServices.Host\EditorServicesHost.cs: line 445
    PowerShell Editor Services is terminating due to an unhandled exception, see previous logs for details.

1550417828-fa668236-6b83-4c9b-8993-eaa5e9276e8e1550417825456.zip

I left VSCode open for a while, when I came back to it it crashed. I usually have 5 or so instances and leave them alone for hours (even 24h+).

Expected Behaviour

No crash

PrzemyslawKlys commented 5 years ago

After 5 minutes another crash so I doubt it's related to wait time.

PrzemyslawKlys commented 5 years ago

1550430327-19aa7f42-38ed-4dc4-a885-9a5c8fb57ab41550249737126.zip

function Get-ComputerOperatingSystem {
    param(
        $ComputerName = $Env:COMPUTERNAME
    )

    $Data6 = get-WmiObject win32_operatingsystem -ComputerName $ComputerName | select Caption, Organization, InstallDate, OSArchitecture, Version, SerialNumber, BootDevice, WindowsDirectory, CountryCode
    return $Data6
}

And I simply added at the bottom of it this:

Get-ComputerOperatingSystem

Pressed F5 and crash. It was like 15 seconds after the session restart. Today I've probably got about 20 crashes by now working with WMI/CMI.

SydneyhSmith commented 5 years ago

@PrzemyslawKlys thanks as always for reporting these issues! Just wanted to clarify, is this issue happening in the PowerShell extension (not the Preview version)? It is likely this is a result of a threading issue in PSES, one we have been wanting to look more into, and hope to soon. You are probably hitting it because of a bad interaction with the WMI cmdlets.

PrzemyslawKlys commented 5 years ago

I'm using the Preview version.