Skip to content

5 xUnit cases failed with "System.InvalidOperationException : Binding model is already locked for the AppDomain and cannot be reset." #676

Closed

Description

When I run ./xunit.sh, it shows below error:

xUnit.net DNX Runner (64-bit ubuntu.14.04-x64)
Discovering: powershell-tests
Discovered: powershell-tests
Starting: powershell-tests
PSTests.RunspaceTests.TestRunspaceWithPowerShellAndHost [FAIL]
System.InvalidOperationException : Binding model is already locked for the AppDomain and cannot be reset.
Stack Trace:
at System.Runtime.Loader.AssemblyLoadContext.InitializeDefaultContext(AssemblyLoadContext context)
at System.Management.Automation.PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext(String basePaths)
at PSTests.RunspaceTests..ctor()
PSTests.RunspaceTests.TestRunspaceWithPipeline [FAIL]
System.InvalidOperationException : Binding model is already locked for the AppDomain and cannot be reset.
Stack Trace:
at System.Runtime.Loader.AssemblyLoadContext.InitializeDefaultContext(AssemblyLoadContext context)
at System.Management.Automation.PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext(String basePaths)
at PSTests.RunspaceTests..ctor()
PSTests.RunspaceTests.TestRunspaceWithPowerShell [FAIL]
System.InvalidOperationException : Binding model is already locked for the AppDomain and cannot be reset.
Stack Trace:
at System.Runtime.Loader.AssemblyLoadContext.InitializeDefaultContext(AssemblyLoadContext context)
at System.Management.Automation.PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext(String basePaths)
at PSTests.RunspaceTests..ctor()
PSTests.RunspaceTests.TestRunspaceWithFunction [FAIL]
System.InvalidOperationException : Binding model is already locked for the AppDomain and cannot be reset.
Stack Trace:
at System.Runtime.Loader.AssemblyLoadContext.InitializeDefaultContext(AssemblyLoadContext context)
at System.Management.Automation.PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext(String basePaths)
at PSTests.RunspaceTests..ctor()
PSTests.RunspaceTests.TestRunspaceWithPowerShellAndInitialSessionState [FAIL]
System.InvalidOperationException : Binding model is already locked for the AppDomain and cannot be reset.
Stack Trace:
at System.Runtime.Loader.AssemblyLoadContext.InitializeDefaultContext(AssemblyLoadContext context)
at System.Management.Automation.PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext(String basePaths)
at PSTests.RunspaceTests..ctor()
Finished: powershell-tests
=== TEST EXECUTION SUMMARY ===
powershell-tests Total: 41, Errors: 0, Failed: 5, Skipped: 0, Time: 1.908s
/workspace/PowerShell

Activity

  1. andyleejordan commented on Mar 16, 2016

    @andyleejordan
    Member

    That came in with the new package updates. We really need to get our xUnit tests running automatically, as I rushed #662 through to unblock another developer.

    The root cause is that .NET enabled a safety check that is completely valid, and we were previously skirting it in these tests. The test setup function that initializes the AssemblyLoadContext should only be called once, but is being for each test it seems.

  2. andyleejordan commented on Mar 16, 2016

    @andyleejordan
    Member

    Sorry, I haven't even had time to get this filed to get fixed properly. Thanks for reporting.

  3. JumpingYang001 commented on Mar 16, 2016

    @JumpingYang001
    Author

    Andy Jordan (@andschwa) I use below code to fix calling once issue, but I meet another issue when fix it.

    public static object setupObject;
    
            // Initialize the Core PowerShell AssemblyLoadContext
            public RunspaceTests()
            {
                if(setupObject==null)
                {
                    setupObject=new object();
                    PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext(AppContext.BaseDirectory);
                }
            }

    New error:
    xUnit.net DNX Runner (64-bit ubuntu.14.04-x64)
    Discovering: powershell-tests
    Discovered: powershell-tests
    Starting: powershell-tests
    PSTests.RunspaceTests.TestRunspaceWithPowerShellAndHost [FAIL]
    System.InvalidOperationException : Binding model is already locked for the AppDomain and cannot be reset. Stack Trace:
    at System.Runtime.Loader.AssemblyLoadContext.InitializeDefaultContext(AssemblyLoadContext context)
    at System.Management.Automation.PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext(String basePaths)
    at PSTests.RunspaceTests..ctor()
    PSTests.RunspaceTests.TestRunspaceWithPipeline [FAIL]
    System.InvalidOperationException : The default AssemblyLoadContext in use is invalid. The default AssemblyLoadContext for PowerShell Core should be of type 'PowerShellAssemblyLoadContext'.
    Stack Trace:
    at System.Management.Automation.ClrFacade.GetAssemblyLoadContext()
    at System.Management.Automation.ClrFacade.Load(AssemblyName assembly)
    at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadMshSnapinAssembly(PSSnapInInfo mshsnapinInfo)
    at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadPSSnapIn(PSSnapInInfo mshsnapinInfo)
    at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadPSSnapIns(Collection1 mshsnapinInfos, PSConsoleLoadException& warning) at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadConsole(PSConsoleLoadException& warning) at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.CreateDefaultConfiguration() at System.Management.Automation.Runspaces.RunspaceConfiguration.Create() at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost host) at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace() at PSTests.RunspaceTests.TestRunspaceWithPipeline() PSTests.RunspaceTests.TestRunspaceWithPowerShell [FAIL] System.InvalidOperationException : The default AssemblyLoadContext in use is invalid. The default AssemblyLoadContext for PowerShell Core should be of type 'PowerShellAssemblyLoadContext'. Stack Trace: at System.Management.Automation.ClrFacade.GetAssemblyLoadContext() at System.Management.Automation.ClrFacade.Load(AssemblyName assembly) at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadMshSnapinAssembly(PSSnapInInfo mshsnapinInfo) at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadPSSnapIn(PSSnapInInfo mshsnapinInfo) at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadPSSnapIns(Collection1 mshsnapinInfos, PSConsoleLoadException& warning)
    at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.LoadConsole(PSConsoleLoadException& warning)
    at System.Management.Automation.Runspaces.RunspaceConfigForSingleShell.CreateDefaultConfiguration()
    at System.Management.Automation.Runspaces.RunspaceConfiguration.Create()
    at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(PSHost host)
    at System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace()
    at PSTests.RunspaceTests.TestRunspaceWithPowerShell()
    PSTests.RunspaceTests.TestRunspaceWithFunction [FAIL]
    System.InvalidOperationException : The default AssemblyLoadContext in use is invalid. The default AssemblyLoadContext for PowerShell Core should be of type 'PowerShellAssemblyLoadContext'.
    Stack Trace:
    at System.Management.Automation.ClrFacade.GetAssemblyLoadContext()
    at System.Management.Automation.ClrFacade.Load(AssemblyName assembly)
    at System.Management.Automation.Runspaces.PSSnapInHelpers.LoadPSSnapInAssembly(PSSnapInInfo psSnapInInfo, Dictionary2& cmdlets, Dictionary2& providers)
    at System.Management.Automation.Runspaces.InitialSessionState.ImportPSSnapIn(PSSnapInInfo psSnapInInfo, PSSnapInException& warning)
    at System.Management.Automation.Runspaces.InitialSessionState.ImportCorePSSnapIn()
    at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2()
    at Microsoft.PowerShell.Linux.Host.Listener..ctor(String initialScript, Boolean loadProfiles)
    at PSTests.RunspaceTests.TestRunspaceWithFunction()
    PSTests.RunspaceTests.TestRunspaceWithPowerShellAndInitialSessionState [FAIL]
    System.InvalidOperationException : The default AssemblyLoadContext in use is invalid. The default AssemblyLoadContext for PowerShell Core should be of type 'PowerShellAssemblyLoadContext'.
    Stack Trace:
    at System.Management.Automation.ClrFacade.GetAssemblyLoadContext()
    at System.Management.Automation.ClrFacade.Load(AssemblyName assembly)
    at System.Management.Automation.Runspaces.PSSnapInHelpers.LoadPSSnapInAssembly(PSSnapInInfo psSnapInInfo, Dictionary2& cmdlets, Dictionary2& providers)
    at System.Management.Automation.Runspaces.InitialSessionState.ImportPSSnapIn(PSSnapInInfo psSnapInInfo, PSSnapInException& warning)
    at System.Management.Automation.Runspaces.InitialSessionState.ImportCorePSSnapIn()
    at System.Management.Automation.Runspaces.InitialSessionState.CreateDefault2()
    at PSTests.RunspaceTests.TestRunspaceWithPowerShellAndInitialSessionState()
    Finished: powershell-tests

  4. andyleejordan commented on Mar 16, 2016

    @andyleejordan
    Member

    Can you re-test on the latest master branch? I know I saw the same errors in the last few days, but I can't reproduce it any more.

  5. andyleejordan commented on Mar 16, 2016

    @andyleejordan
    Member

    Strange. It only reproduces intermittently. 1/3 runs just now.

  6. JumpingYang001 commented on Mar 17, 2016

    @JumpingYang001
    Author

    Andy Jordan (@andschwa) We still repro this error, you can login ps64-ubun14-01 (IP: 10.231.73.77 ) user: root password:OpsMgr2007R2 to check the result under /root folder, thanks.

  7. andyleejordan commented on Mar 17, 2016

    @andyleejordan
    Member

    That's no problem, it reproed for me eventually, just not the first or second time. Which is strange. I'll look into this further.

  8. added a commit that references this issue on Mar 18, 2016
    b820817
  9. added a commit that references this issue on Aug 6, 2026
    f1c69c5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    WG-Quality-Testissues in a test or in test infrastructure

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions