PowerShell / PSScriptAnalyzer

Download ScriptAnalyzer from PowerShellGallery
https://www.powershellgallery.com/packages/PSScriptAnalyzer/
MIT License
1.85k stars 375 forks source link

Invoke-ScriptAnalyzer keeps powershell_ise process open #1416

Open gpetrou opened 4 years ago

gpetrou commented 4 years ago

Steps to reproduce

Use the following code in PowerShell ISE: Import-Module "PATH_TO\psscriptanalyzer\1.18.3\PSScriptAnalyzer.psm1" Invoke-ScriptAnalyzer "PATH_TO\Dummy.psm1" Dummy just has a: function DummyFunction() {} Close the PowerShell ISE.

Expected behavior

The powershell_ise process terminates.

Actual behavior

The powershell_ise process stays open.

Environment data

PSVersion 5.1.14409.1018
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1018
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

PSScriptAnalyzer Version 1.18.3

bergmeister commented 4 years ago

I can confirm, a simpler repro is to just execute Invoke-ScriptAnalyzer -ScriptDefiniton 'foo'. @gpetrou Can you please describe the impact and severity of this to you?

Investigation: I tried different versions of PSSA and it seems this behaviour started first in 1.18.1 (i.e. the bug is not present in 1.18.0). We (the maintainers, i.e. Jim, Rob and myself) know that there is a few singletons in there for the command cache, therefore my suspicion is that there are pinned objects that make .Net decide to block the process from stopping. By looking at the changes, it is probably most likely that the culprit might be PR #1178, which introduced the caching of a runspace pool (which was an important performance tweak as it halved the time to analyse for cold runs). This behaviour does not apply to the process of VS-Code or conhost.exe (which is hosting powershell.exe/pwsh.exe) therefore I am also thinking that some sort of blame could also go to the ISE. @SydneyhSmith Can you ask the team for feedback on what they think about this (i.e. should we even aim to fix this given that the ISE is considered legacy, of course assuming that the impact of this bug is low)?

gpetrou commented 4 years ago

I use the ISE when developing scripts and it is annoying having to manually kill the process. I guess I should switch to VS-Code :) Is there a command I could perhaps run before the script finishes to release objects that are still used?

bergmeister commented 4 years ago

@gpetrou Yes, switching to VS-Code would help, this guide should help you: https://docs.microsoft.com/en-us/powershell/scripting/components/vscode/how-to-replicate-the-ise-experience-in-vscode?view=powershell-7 But coming back to my question: What problems does the left-over process cause to you in terms of impact? The process by itself should not harm you and the memory footprint shouldn't be big unless you load a lot of data into memory. You can technically run [gc]::Collect() but it does not help in this case.