Skip to content

PSShouldProcess hangs #1287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
indented-automation opened this issue Jul 12, 2019 · 13 comments
Closed

PSShouldProcess hangs #1287

indented-automation opened this issue Jul 12, 2019 · 13 comments

Comments

@indented-automation
Copy link

indented-automation commented Jul 12, 2019

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 -Verbose

Expected 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.0

And:

> $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.1
@ChrisLGardner
Copy link

I'm seeing this same issue on my local machine but not when I run a similar script via Azure DevOps.

@bergmeister
Copy link
Collaborator

bergmeister commented Jul 12, 2019

  • Are you using a specific psm1 file?
  • When you say 'hang' do you mean it never completes or only that the command cannot be cancelled? When calling Invoke-ScriptAnalyzer and using Ctrl+C to cancel the command one has to wait for the current file being analysis to be finished (i.e. if one used e.g. the -Recurse option 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 of PSShouldProcess , 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 case

@indented-automation
Copy link
Author

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
Copy link
Author

Okay, I've trimmed down the code a lot. This snippet reproduces the problem for me:

Invoke-ScriptAnalyzer -Verbose -ScriptDefinition @'
function SomeCommand { }

[DscResource()]
class SomeClass {
    [String] $Name = 'Value'

    [Void] Set() {
        $source = Get-PackageSource -Name $this.Name
    }

    [Boolean] Test() {
        # $source = Get-PackageSource -Name $this.Name

        $something = SomeCommand -Name $this.Name
    }
}
'@

There are some combinations with this one.

  1. All commands: Hangs
  2. Get\Get-PackageSource and SomeCommand: Hangs
  3. Set\Get-PackageSource and SomeCommand: Hangs
  4. Get\Get-PackageSource and Set\Get-PackageSource: No problem
  5. Any command alone: No problem
  6. Get\Get-PackageSource and Set\Get-PackageSource: No problem

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
Copy link
Collaborator

bergmeister commented 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 -SaveDscDependency switch, which will download the dependent modules when it sees the Import-DscResource syntax

@rjmholt
Copy link
Contributor

rjmholt commented Aug 22, 2019

@indented-automation, @ChrisLGardner if you could get a proc dump (procdump.exe -ma $PssaPid) of PSSA when this hang occurs and send it to [email protected] (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 occurs

@ghost
Copy link

ghost commented Jun 17, 2020

Closing due to inactivity

@ghost ghost closed this as completed Jun 17, 2020
@bergmeister
Copy link
Collaborator

@indented-automation, @ChrisLGardner Let us know if this is still happening and needs re-opening but we'd need more info then

@indented-automation
Copy link
Author

indented-automation commented Jun 17, 2020 via email

@bergmeister
Copy link
Collaborator

@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
Copy link
Author

indented-automation commented Jun 17, 2020 via email

@bergmeister
Copy link
Collaborator

Ok, please let us know the outcome. I am going to re-open the issue in the meantime but still leave the Need Repro Info label on because that's what the bot uses to close inactive issues after a certain period of time.

@bergmeister bergmeister reopened this Jun 17, 2020
@ghost
Copy link

ghost commented Jul 1, 2020

Closing due to inactivity

@ghost ghost closed this as completed Jul 1, 2020
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants