Runtime: ICorDebugManagedCallback::DebuggerError when unloading AssemblyLoadContext

Created on 29 Jan 2020  路  27Comments  路  Source: dotnet/runtime

Running the following code in VS d16.5 typically fails with an error dialog due to the CLR's debugging interfaces returning DebuggerError with the error set to E_INVALIDARG.

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Loader;

namespace PlugInTest
{
    class Program
    {
        //TODO: insert absolute path to managed dll
        static string pluginPath = @"C:\MyPlugin.dll";
        static AssemblyLoadContext AssemblyLoadContext;
        static Assembly Assembly;

        [MethodImpl(MethodImplOptions.NoInlining)]
        private static void Load()
        {
            AssemblyLoadContext = new AssemblyLoadContext("PluginLoader", true);
            Assembly = AssemblyLoadContext.LoadFromAssemblyPath(pluginPath);
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        private static WeakReference Unload()
        {
            Assembly = null;
            AssemblyLoadContext.Unload();
            WeakReference weakRef = new WeakReference(AssemblyLoadContext);
            AssemblyLoadContext = null;
            return weakRef;
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        private static void Test()
        {
            WeakReference weakRef;
            {
                Load();
                weakRef = Unload();
            }

            while (weakRef.IsAlive)
            {
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            Console.WriteLine("Finished Test");
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        static void Main(string[] args)
        {
            Test();
            Test();
            Test(); //crashs here at GC.Collect
        }
    }
}

originally reported here: https://developercommunity.visualstudio.com/content/problem/698374/vs2019-and-fatal-error-has-occurred-and-debugging.html

area-Diagnostics-coreclr

Most helpful comment

This keeps happening for me on 3.1 on Mac with both vsmac and vscode, with 3.1.404 SDK.

All 27 comments

Duplicate of https://github.com/dotnet/runtime/issues/535.

Not sure if it's a runtime issue or a Visual Studio issue, but it's a show stopper for us. Impossible to debug our plugin management except by putting console comments since it crashes.

I don't believe this is a Visual Studio issue. The issue occurs when debugging in Visual Studio 2017/2019, VS Code, _or_ IntelliJ Rider.

I have not yet tested with Samsung's netcoredbg here: https://github.com/Samsung/netcoredbg

There is a small chance that the root issue is somewhere in the VS Debugger portion of the managed debugger that is shared between VS/VS Code, but I need somebody on the runtime to investigate and confirm/deny.

@hoyosjs is investigating

possibly related to dotnet/coreclr#26830

I've encountered the exact same problem in the application with hostfxr library. More information can be found starting from this comment.

Hey @baehny, I've reopened this issue to track a 3.1 port. Given that this has a developer experience report with some feedback I think I can justify it. Can you explain a bit here of how this affects you for when I try to explain the customer impact? Thanks 馃槃

In general when developing/using the usecases that where described in the .net core announcement
under Assembly Unloadability:
https://devblogs.microsoft.com/dotnet/announcing-net-core-3-0/
Developing such scenarios is slowed down as it is not possible to debug any scenario that
requires assembly unloading.

I'm working on an application that allows plugin loading and because of
long loading times the application keeps running while only the plugin is
reloaded if a newer version should be tested.
Additionally some plugins were recompiled/reloaded to make use of generated IL.
Without this fix the application must be restarted or tested without a debugger attached.
This makes plugin development very annoying and less attractive.

Thanks @baehny! Can you also confirm this is fixed if you use a 5.0 nightly runtime? Or do you not have a way to verify this.

I'm not sure. How can I get a nightly build of the runtime ?

I only found the preview SDK Version: 5.0.100-preview.2.20120.11 at
https://github.com/dotnet/core-sdk/blob/master/README.md

Sadly this build still crashs using Visual Studio16.5.0 Preview 2.0 and the test program described in https://developercommunity.visualstudio.com/content/problem/698374/vs2019-and-fatal-error-has-occurred-and-debugging.html

I can confirm that the issue is fixed using the current preview version: 5.0.0-preview.2.20128.16

@hoyosjs Any rough estimation when we can expect this to be backported to the 3rd version?

@nxrighthere dotnet/coreclr#28023 is the PR that's porting it to 3.1 (not 3.0). As for when, I'd expect 3.1.4, exact dates are shifting a bit looks like but it's the April release. As I get more information I'll let you know.

The fix got merged and will be available in 3.1.4

@hoyosjs We are almost in May, any news about 3.1.4 release?

@nxrighthere I just took a look at the release candidate for 3.1.4 and can confirm it contains the fix. However we are waiting for some teams to give their thumbs up for the release. Given the current situation worldwide we are trying to tread very carefully to make sure we don't anything that might regress customers. After talking to the release team, it should be finalized soon but they can't give me a date right now.

@nxrighthere I've been told May 12

@hoyosjs Awesome, thanks!

@nxrighthere it was released today.

@janvorli Yes, thank you! We already upgraded, the issue is gone.

This keeps happening for me on 3.1 on Mac with both vsmac and vscode, with 3.1.404 SDK.

Same on Linux with 3.1.404, same crash after a few minutes debugging

Same here. Running on macOS Catalina 10.15.7 and Visual Studio for Mac (Professional) 8.8.3.
_A fatal error has occurred and debugging needs to be terminated. For more details, please see the Microsoft Help and Support web site. HRESULT=0x80131c36. ErrorCode=0x0.The program ... has exited with code -1 (0xffffffff)._
Running .net Core 3.1 with SDK 3.1.404

@mari-dot-tj than you for the details. @dotnet/dotnet-diag this error is different, the error code is CORDBG_E_TARGET_INCONSISTENT.

@janvorli I see! Is this an already known issue?

It doesn't seem like a known issue, but I've tagged the diagnostics folks who might know more and would most likely do the investigations. Can you please create a new issue for your problem? It will be better than piggy backing on a closed issue.

Sure! I'll do that.

Was this page helpful?
0 / 5 - 0 ratings