AppDomain.Unload 会抛出 Finalizer 吗?

发布于 2024-09-30 04:33:32 字数 996 浏览 7 评论 0原文

这是到目前为止的故事,我有一个工作程序,它使用 AppDomain 来执行某些任务。该域的设置和拆除成本很高。因此,我为工作线程创建了一个 WeakReference 对象的缓存,如下所示:

class Worker
{
    [ThreadStatic]
    static Dictionary<string, WeakReference> _workers;

    public static Worker Fetch( ... ) { you get the idea }

    private AppDomain _domain;
    public Worker(...)
    {
        _domain = AppDomain.Create( ... );
    }

    ~Worker()
    { 
        AppDomain.Unload(_domain);
    }

    // bla bla bla
}

我遇到的问题是,当 GC 收集时,似乎总是在调用 AppDomain.Unload 时抛出异常:

System.CannotUnloadAppDomainException: Error while unloading appdomain. (Exception from HRESULT: 0x80131015)"

所以我认为这很奇怪,我知道我在该域中没有任何“运行”的东西...这是怎么回事?经过一些挖掘和反复试验,我想出了这个:

    ~Worker()
    { 
        new Action<AppDomain>(AppDomain.Unload)
            .BeginInvoke(_domain, null, null);
    }

所以我的问题是:

  1. AppDomain.Unload 总是会从终结器失败吗?为什么?
  2. 使用上述解决方法我会遇到任何“不良”情况吗?

So here is the story so far, I have this worker thingy which uses an AppDomain to perform some task. The domain is expensive to setup and teardown. So I create a cache per-thread of WeakReference objects to the worker thingy like so:

class Worker
{
    [ThreadStatic]
    static Dictionary<string, WeakReference> _workers;

    public static Worker Fetch( ... ) { you get the idea }

    private AppDomain _domain;
    public Worker(...)
    {
        _domain = AppDomain.Create( ... );
    }

    ~Worker()
    { 
        AppDomain.Unload(_domain);
    }

    // bla bla bla
}

The problem I'm having is that seems to always throw an exception in the call to AppDomain.Unload when GC collects:

System.CannotUnloadAppDomainException: Error while unloading appdomain. (Exception from HRESULT: 0x80131015)"

So I'm thinking that's wierd, I know I don't have anything 'running' in that domain... Whats the deal? A bit of digging and trial and error I came up with this:

    ~Worker()
    { 
        new Action<AppDomain>(AppDomain.Unload)
            .BeginInvoke(_domain, null, null);
    }

So my Questions are:

  1. Will AppDomain.Unload always fail from a Finalizer? Why?
  2. Am I going to experience anything 'undesirable' with the above workaround?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

AppDomain 由单独的 CLR 线程卸载。当终结器线程运行时,该线程无法运行。您收到异常是因为 CLR 注意到卸载线程没有取得进展。它永远不会运行,因为终结器线程在 Unload 调用上被阻塞。

僵局。

您的解决方法确实解决了这个僵局。明确地进行卸载而不是依赖终结器是更好的方法。

AppDomains are unloaded by a separate CLR thread. That thread cannot run while the finalizer thread is running. You're getting the exception because the CLR notices that the unload thread isn't making progress. It never gets going because the finalizer thread is blocked on the Unload call.

Deadlock.

Your workaround indeed solves that deadlock. Doing the unloading explicitly instead of relying on a finalizer is the better approach here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文