Repository navigation
PSShouldProcess hangs #1287
Copy link
Copy link
Closed
Labels
Description
Steps to reproduce
This fault is intermittent and causes me most trouble when it occurs on AppVeyor.
$debugPreference = 'continue'
Invoke-ScriptAnalyzer -Path .\path\to\psm1.psm1 -VerboseExpected behavior
Script analyzer completes with no errors, there are none in the code I'm testing.
Actual behavior
Script analyzer hangs before or during the PSShouldProcess rule.
2|PS> $debugPreference = 'continue'
3|PS> Invoke-ScriptAnalyzer -Path .\path\to\psm1.psm1 -Verbose
...
VERBOSE: Analyzing file: C:\Development\path\to\psm1.psm1
VERBOSE: Running PSAvoidDefaultValueSwitchParameter rule.
VERBOSE: Running PSAvoidGlobalAliases rule.
VERBOSE: Running PSAvoidDefaultValueForMandatoryParameter rule.
VERBOSE: Running PSAvoidUsingEmptyCatchBlock rule.
VERBOSE: Running PSAvoidGlobalFunctions rule.
VERBOSE: Running PSAvoidGlobalVars rule.
VERBOSE: Running PSAvoidInvokingEmptyMembers rule.
VERBOSE: Running PSAvoidAssignmentToAutomaticVariable rule.
VERBOSE: Running PSAvoidNullOrEmptyHelpMessageAttribute rule.
VERBOSE: Running PSReservedParams rule.
VERBOSE: Running PSAvoidShouldContinueWithoutForce rule.
VERBOSE: Running PSAvoidUsingConvertToSecureStringWithPlainText rule.
VERBOSE: Running PSAvoidUsingComputerNameHardcoded rule.
VERBOSE: Running PSAvoidUsingInvokeExpression rule.
VERBOSE: Running PSAvoidUsingWriteHost rule.
VERBOSE: Running PSAvoidUsingPlainTextForPassword rule.
VERBOSE: Running PSAvoidUsingUsernameAndPasswordParams rule.
VERBOSE: Running PSMissingModuleManifestField rule.
VERBOSE: Running PSAvoidUsingDeprecatedManifestFields rule.
VERBOSE: Running PSPossibleIncorrectUsageOfRedirectionOperator rule.
VERBOSE: Running PSMisleadingBacktick rule.
VERBOSE: Running PSAvoidUsingWMICmdlet rule.
VERBOSE: Running PSUseApprovedVerbs rule.
VERBOSE: Running PSUseCompatibleCmdlets rule.
VERBOSE: Running PSPossibleIncorrectUsageOfAssignmentOperator rule.
VERBOSE: Running PSUseLiteralInitializerForHashtable rule.
VERBOSE: Running PSPossibleIncorrectComparisonWithNull rule.
VERBOSE: Running PSAvoidTrailingWhitespace rule.
VERBOSE: Running PSUseBOMForUnicodeEncodedFile rule.
VERBOSE: Running PSUseToExportFieldsInManifest rule.
VERBOSE: Running PSUseUTF8EncodingForHelpFile rule.
VERBOSE: Running PSUsePSCredentialType rule.
VERBOSE: Running PSUseSupportsShouldProcess rule.
VERBOSE: Running PSUseShouldProcessForStateChangingFunctions rule.
VERBOSE: Running PSUseOutputTypeCorrectly rule.
VERBOSE: Running PSUseDeclaredVarsMoreThanAssignments rule.
VERBOSE: Running PSProvideCommentHelp rule.
VERBOSE: Running PSReservedCmdletChar rule.
VERBOSE: Running PSShouldProcess rule.
Script Analyzer cannot be terminated using Control and C.
Environment data
Tested under PowerShell 5.1 and 6.2 as shown below.
> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.18362.145
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.18362.145
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.18.1
1.18.0
1.17.1
1.16.1
1.4.0And:
> $PSVersionTable
Name Value
---- -----
PSVersion 6.2.1
PSEdition Core
GitCommitId 6.2.1
OS Microsoft Windows 10.0.18362
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.18.1
1.18.0
1.17.1
1.16.1Reactions are currently unavailable
Activity
ChrisLGardner commentedon Jul 12, 2019
I'm seeing this same issue on my local machine but not when I run a similar script via Azure DevOps.
bergmeister commentedon Jul 12, 2019
Invoke-ScriptAnalyzerand usingCtrl+Cto cancel the command one has to wait for the current file being analysis to be finished (i.e. if one used e.g. the-Recurseoption it would not proceed to analyse more files). I believe this is due to threads being created for each rule and PSSA waiting for them all to finish, I don't think it is a problem ofPSShouldProcess, it just seems to be the last rule being kicked off in the background, when running it again it might be a different rule. Can you please be more precise what the exact problem is? I'd have to look at actual code to see if it is possible to use a cancellation token to cancel analysis of one file when all rules are running, but I suspect this might be difficult, a configurable timeout might be easier in this caseindented-automation commentedon Jul 12, 2019
Apologies, I was going to come back to this and dig deeper than I had above.
When I say hang I mean it stops, cannot be cancelled (without closing the console), and never completes (with the caveat that the longest I've waited is 2 hours).
This is the module I'm executing script analyzer against:
https://github.com/indented-automation/Indented.Nexus
Normally I run it against the merged version of the module. A single psm1 file based on the content of that repo.
Just trying to see if I can isolate it to a single piece of code. I thought I had, script analyzer seems to reliably hang when run against this file in isolation:
https://github.com/indented-automation/Indented.Nexus/blob/master/Indented.Nexus/class/NexusNugetSource.ps1
But I bumped that out of the psm1 and it's still hanging. Give me a few minutes to see if I can isolate what in the psm1 it doesn't like.
indented-automation commentedon Jul 12, 2019
Okay, I've trimmed down the code a lot. This snippet reproduces the problem for me:
There are some combinations with this one.
If I add the module name in front of the package management commands (e.g.
PackageManagement\Get-PackageSource) it works.I'll wipe all other copies of package management off this computer a bit later and repeat this experiment.
bergmeister commentedon Jul 13, 2019
Hmm, I cannot reproduce this with PS 6.2.1 or 5.1 on Windows 10 1903 with the released version of 1.18.1 unfortunately.
I did notice though that your example seems to be invalid syntax as there are ParserErrors returned by it.
Also: when using DSC dependencies (not in your example but just as a general tip), one can use the
-SaveDscDependencyswitch, which will download the dependent modules when it sees theImport-DscResourcesyntaxrjmholt commentedon Aug 22, 2019
Chris Dent (@indented-automation), Chris Gardner (@ChrisLGardner) if you could get a proc dump (
procdump.exe -ma $PssaPid) of PSSA when this hang occurs and send it to psscriptanalyzer@microsoft.com (probably need to upload it to a file host and send a link, since it's going to be a very large file), then we'll be able to work out much more concretely why and where this hang occursbergmeister commentedon Jun 17, 2020
Chris Dent (@indented-automation), Chris Gardner (@ChrisLGardner) Let us know if this is still happening and needs re-opening but we'd need more info then
indented-automation commentedon Jun 17, 2020
bergmeister commentedon Jun 17, 2020
Chris Dent (@indented-automation) Starting with PSSA 1.18.2 (which was released after you opened this issue), we fixed a problem that caused PSSA to hang due to PackageManagement / PowerShellGet in #1316
Can you confirm you are using the latest version (1.19.0)?
indented-automation commentedon Jun 17, 2020
bergmeister commentedon Jun 17, 2020
Ok, please let us know the outcome. I am going to re-open the issue in the meantime but still leave the
Need Repro Infolabel on because that's what the bot uses to close inactive issues after a certain period of time.