QueuedTaskScheduler:如何处理AppDomain卸载?

时间:2014-04-26 12:26:58

标签: .net task-parallel-library appdomain parallel-extensions

使用ParallelExtensionsExtras中的QueuedTaskScheduler我面临以下问题:卸载运行调度程序线程的AppDomain时(在我的情况下,由于将新代码版本部署到ASP.NET网站)线程进入无限旋转循环。相关代码是:

// If a thread abort occurs, we'll try to reset it and continue running.
while (true)
{
    try
    {
        // For each task queued to the scheduler, try to execute it.
        foreach (var task in _blockingTaskQueue.GetConsumingEnumerable(_disposeCancellation.Token))
        {
            //[...] run task
        }
    }
    catch (ThreadAbortException)
    {
        // If we received a thread abort, and that thread abort was due to shutting down
        // or unloading, let it pass through.  Otherwise, reset the abort so we can
        // continue processing work items.
        if (!Environment.HasShutdownStarted && !AppDomain.CurrentDomain.IsFinalizingForUnload())
        {
            Thread.ResetAbort();
        }
    }
}

调度程序尝试检测正在卸载其AppDomain的情况,但不幸的是,在运行时写入的条件结果为false。这会导致重置中止并继续循环。

据我所知,未立即提出暂停中止。有时只有jitted代码轮询一个待处理的中止并提升TAE。根据我观察到的调用堆栈,这似乎在GetConsumingEnumerable里面。

所以线程永远不会退出循环并继续旋转。 (即使这种解释是错误的,线程可能最终会在GetConsumingEnumerable中结束并在那里消耗大量CPU。)

此代码的适当修复是什么?似乎无法检测到正在卸载当前的AppDomain(AppDomain.CurrentDomain.IsFinalizingForUnload可能是假的,因为我们还没有最终确定)。我考虑修改代码只是永远不会重置中止(这解决了问题)。但本来应该做些什么呢?

(我对解决方法感兴趣不太感兴趣,因为我已经有了解决方法。)

1 个答案:

答案 0 :(得分:2)

你尝试过这样的事情(未经测试)吗?

var domain = Thread.GetDomain();
var unloading = false;
domain.DomainUnload += (s, e) =>
{
    unloading = true;
    _blockingTaskQueue.CompleteAdding();
};

while (true)
{
    try
    {
        // For each task queued to the scheduler, try to execute it.
        foreach (var task in _blockingTaskQueue.GetConsumingEnumerable(_disposeCancellation.Token))
        {
            //[...] run task
        }
    }
    catch (ThreadAbortException)
    {
        if (!unloading)
        {
            Thread.ResetAbort();
        }
    }
}

已更新,我不确定ASP.NET,但以下内容会在控制台应用中生成正确的事件序列。即使我拨打"End of DoCallBack",我也看不到Thread.ResetAbort()。我想这是有道理的,因为DoCallBack中的代码应该只在不再存在的域上执行:

using System;
using System.Runtime.Remoting.Contexts;
using System.Threading;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var newDomain = System.AppDomain.CreateDomain("New domain");
            var thread = new Thread(() =>
            {
                try
                {
                    newDomain.DoCallBack(() =>
                    {
                        var unloading = false;
                        try
                        {
                            var domain = Thread.GetDomain();
                            domain.DomainUnload += (s, e) =>
                            {
                                unloading = true;
                                // call CompleteAdding here
                                Console.WriteLine("domain.DomainUnload");
                            };

                            Thread.Sleep(2000);
                            Console.WriteLine("End of sleep");
                        }
                        catch (ThreadAbortException)
                        {
                            Console.WriteLine("ThreadAbortException");
                            Thread.ResetAbort();
                        }

                        Console.WriteLine("End of DoCallBack");
                    });
                }
                catch (AppDomainUnloadedException)
                {
                    Console.WriteLine("AppDomainUnloadedException");
                }
                Console.WriteLine("End of thread");
            });

            thread.Start();
            Thread.Sleep(1000);
            AppDomain.Unload(newDomain);

            Console.ReadLine();
        }
    }
}

输出:

domain.DomainUnload
ThreadAbortException
AppDomainUnloadedException
End of thread