Skip to content

NullReferenceException in console when running code with PS classes #2230

@nvarscar

Description

@nvarscar

System Details

System Details Output

### VSCode version: 1.38.1 b37e54c98e1a74ba89e03073e5a3761284e3ffb0 x64

### VSCode extensions:
ckolkman.vscode-postgres@1.1.11     
eamodio.gitlens@10.1.0
KnisterPeter.vscode-github@0.30.2   
ms-vscode.powershell@2019.9.0       
vscode-icons-team.vscode-icons@9.4.0


### PSES version: 1.13.0.0

### PowerShell version:

Name                           Value
----                           -----
PSVersion                      6.2.3
PSEdition                      Core
GitCommitId                    6.2.3
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

Issue Description

I'm running into a NullReferenceException when running the code in VSCode Powershell extension console. I experienced this error on both Mac and Windows, though the further debug information only comes from Windows. It's worth mentioning, that the error start to appear after a noticeable amount of time, which makes it hard to debug, however I will try my best.

I'm running Pester tests, which involve accessing classes defined inside the module. After running tests for a while and after a random time period I'm starting to get unexpected output in the console which is completely unrelated to the tests I'm running. Two tests that were run without changing the tested code would return different results: one would succeed, the next one will fail with a NullReference. Once it's failed, it would always fail until I restart the PS console, which resolve the issue temporarily, until next time.

      NullReferenceException: Object reference not set to an instance of an object.
      at InitFile, C:\git\dbops\internal\classes\DBOps.class.ps1: line 959
      at DBOpsFile, C:\git\dbops\internal\classes\DBOps.class.ps1: line 913
      at <ScriptBlock>, C:\git\dbops\tests\DBOpsFile.class.Tests.ps1: line 54

The code line causing the issue seems to be this one:

[DBOpsHelper]::GetBinaryFile($someFile)

which is working perfectly fine if I'm running it manually. It actually works without an issue in a clean session, until it doesn't.
The pattern in which I was able to repro this bug looks like this:

  • Run all the tests in the module
  • Wait several minutes
  • Run one single class-related test -> Success
  • Wait several minutes
  • Run the same test -> Failure

Last but not least, this error never occurs in a regular Powershell session regardless of PS version.

Expected Behaviour

Code runs without raising a NullRef

Actual Behaviour

NullReferenceException: Object reference not set to an instance of an object.

>> $error[1]| select *

PSMessageDetails      :
Exception             : System.NullReferenceException: Object reference not set to an instance of an object.
                           at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean
                        propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] args)
                           at System.Management.Automation.ScriptBlock.InvokeAsMemberFunctionT[T](Object instance, Object[] args)
                           at System.Management.Automation.Internal.ScriptBlockMemberMethodWrapper.InvokeHelperT[T](Object instance, Object sessionStateInternal, Object[] args)
                           at CallSite.Target(Closure , CallSite , Type , Object )
                           at InitFile(Closure , FunctionContext )
TargetObject          :
CategoryInfo          : OperationStopped: (:) [], NullReferenceException
FullyQualifiedErrorId : System.NullReferenceException
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at InitFile, C:\git\dbops\internal\classes\DBOps.class.ps1: line 959
                        at DBOpsFile, C:\git\dbops\internal\classes\DBOps.class.ps1: line 913
                        at DBOpsPackage, C:\git\dbops\internal\classes\DBOps.class.ps1: line 538
                        at <ScriptBlock>, C:\git\dbops\tests\DBOpsFile.class.Tests.ps1: line 154
                        at Invoke-Blocks, C:\Users\SU\Documents\PowerShell\Modules\Pester\4.4.3\Functions\SetupTeardown.ps1: line 140
                        at Invoke-TestCaseSetupBlocks, C:\Users\SU\Documents\PowerShell\Modules\Pester\4.4.3\Functions\SetupTeardown.ps1: line 115
                        at Invoke-Test, C:\Users\SU\Documents\PowerShell\Modules\Pester\4.4.3\Functions\It.ps1: line 279
                        at ItImpl, C:\Users\SU\Documents\PowerShell\Modules\Pester\4.4.3\Functions\It.ps1: line 231
                        at It, C:\Users\SU\Documents\PowerShell\Modules\Pester\4.4.3\Functions\It.ps1: line 121
                        at <ScriptBlock>, C:\git\dbops\tests\DBOpsFile.class.Tests.ps1: line 209
                        at DescribeImpl, C:\Users\SU\Documents\PowerShell\Modules\Pester\4.4.3\Functions\Describe.ps1: line 171
                        at Context, C:\Users\SU\Documents\PowerShell\Modules\Pester\4.4.3\Functions\Context.ps1: line 72
                        at <ScriptBlock>, C:\git\dbops\tests\DBOpsFile.class.Tests.ps1: line 152
                        at DescribeImpl, C:\Users\SU\Documents\PowerShell\Modules\Pester\4.4.3\Functions\Describe.ps1: line 171
                        at Describe, C:\Users\SU\Documents\PowerShell\Modules\Pester\4.4.3\Functions\Describe.ps1: line 85
                        at <ScriptBlock>, C:\git\dbops\tests\DBOpsFile.class.Tests.ps1: line 31
PipelineIterationInfo : {}

>> $error[1].Exception| select *

Message        : Object reference not set to an instance of an object.
Data           : {}
InnerException :
TargetSite     : Void InvokeWithPipe(Boolean, ErrorHandlingBehavior, System.Object, System.Object, System.Object, System.Management.Automation.Internal.Pipe, System.Management.Automation.InvocationInfo, Boolean, System.Collections.Generic.List`1[System.Management.Automation.PSVariable],  
                 System.Collections.Generic.Dictionary`2[System.String,System.Management.Automation.ScriptBlock], System.Object[])
StackTrace     :    at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean propagateAllExceptionsToTop,     
                 List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] args)
                    at System.Management.Automation.ScriptBlock.InvokeAsMemberFunctionT[T](Object instance, Object[] args)
                    at System.Management.Automation.Internal.ScriptBlockMemberMethodWrapper.InvokeHelperT[T](Object instance, Object sessionStateInternal, Object[] args)
                    at CallSite.Target(Closure , CallSite , Type , Object )
                    at InitFile(Closure , FunctionContext )
HelpLink       :
Source         : System.Management.Automation
HResult        : -2147467261

Attached Logs

1570427510-af47caf0-8890-45d9-b195-6d2e3883fba41570419200265.zip

Let me know if there's anything else I can submit. I didn't see any related errors in the VSCode plugin so I would appreciate any advice.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions