System.Threading.Tasks.Task引起的IIS应用程序池崩溃

问题现象

IIS应用程序池崩溃(Crash)的特征如下:

1. 从客户端看,浏览器一直处于连接状态,Web服务器无响应。

2. 从服务器端看(Windows Server 2008 + IIS 7.0),在事件日志中会出现Event ID为5010的错误:

A process serving application pool ‘q.cnblogs.com‘ failed to respond to a ping. The process id was ‘20080‘.

这个错误的意思是:IIS检测到程序池‘q.cnblogs.com‘无响应。为什么没有响应呢?因为程序池‘q.cnblogs.com‘崩溃了。然后呢?IIS会强制回收应用程序池。

(注:如果在你的Web服务器的事件日志中出现这个错误,一定是某个原因引起了应用程序池崩溃。)

问题原因

我们这次遇到的应用程序池崩溃,是由于在使用System.Threading.Tasks.Task进行异步操作时产生了未处理的异常。

示例代码如下:

Task.Factory.StartNew(() =>{    //下面的代码未用try..catch捕获异常    //...});

注:这是一个不需要Callback的异步操作,后续没有task.wait(或者静态方法Task.WaitAll或Task.WaitAny)操作。

当时我们发布程序后,由于Task中代码产生了异常,整个站点无法正常访问,程序池一直处于“崩溃->回收->崩溃->回收”的循环。

解决方法

捕获Task中所有代码的异常,示例代码如下:

Task.Factory.StartNew(() =>{    try    {        //... }    catch { }});

问题分析

在stackoverflow上提到了这个问题的原因

If you create a Task, and you don‘t ever call task.Wait() or try to retrieve the result of a Task<T>, when the task is collected by the garbage collector, it will tear down your application during finalization. For details, see MSDN‘s page on Exception Handling in the TPL.

The best option here is to "handle" the exception.

根据上面的英文,我的理解是:当你创建一个Task,没有调用过task.Wait()或者没有获取它的执行结果,(如果Task中出现了未处理的异常),当这个Task被GC回收时,在GC finalization阶段,会让当前应用程序崩溃。

进一步看MSDN中的Exception Handling (Task Parallel Library)

"Unhandled exceptions that are thrown by user code that is running inside a task are propagated back to the joining thread. ···Exceptions are propagated when you use one of the static or instance Task.Wait or Task(Of TResult).Wait methods···"

翻译:在一个task中运行的代码抛出的未处理异常会被回传给(创建该task的)主线程。···当你调用Task.Wait时,异常才会被回传(给主线程)。

分析:当我们遇到的情况是没调用Task.Wait,也就是异常没有被回传给主线程。下面的这句就提到了这个:

"If you do not wait on a task that propagates an exception, or access its Exception property, the exception is escalated according to the .NET exception policy when the task is garbage-collected."

译:如果你在一个task中没有等待异常被传播,或者访问它的异步特性,在task被GC回收时,该异常会遵循.NET异常策略被逐步升级。

分析:逐步升级的后果就是当前应用程序进程崩溃,对于ASP.NET程序来说,就是应用程序池崩溃。

进一步的解决方法

MSDN上的推荐做法是用Task.ContinueWith观察Task中抛出的异常并进行处理,示例代码如下:

var task1 = Task.Factory.StartNew(() =>{    throw new MyCustomException("Task1 faulted.");}).ContinueWith((t) =>    {        Console.WriteLine("I have observed a {0}",            t.Exception.InnerException.GetType().Name);    },    TaskContinuationOptions.OnlyOnFaulted);

小结

应用程序池崩溃的原因总结 —— System.Threading.Tasks.Task中的代码抛出了未处理的异常,由于没有Task.Wait()操作,异常没有被回传给主线程,在GC回收时,发现这个身份不明的异常。然后,这个异常被一级一级上报,直到当前程序进程的最高领导,最高领导为了顾全大局,果然决定与这个异常同归于尽,也就是让整个应用程序池崩溃。。。

时间: 2024-07-30 13:46:17

System.Threading.Tasks.Task引起的IIS应用程序池崩溃的相关文章

System.Threading.Tasks.Task引起的IIS应用池崩溃

接口服务运行一段时间后,IIS应用池就会突然挂掉,事件查看日志,会有事件日志Event ID为5011的错误 为应用程序池"PokeIn"提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误.该进程 ID 为"2268".数据字段包含错误号. 最后直接程序池直接被禁用 应用程序池"PokeIn"将被自动禁用,原因是为此应用程序池提供服务的进程中出现一系列错误. 查看管理事件 Applicat

System.Threading.Tasks.Task 任务引起的IIS应用程序池崩溃

转载:http://www.cnblogs.com/aaa6818162/p/4421305.html 问题现象 IIS应用程序池崩溃(Crash)的特征如下: 1. 从客户端看,浏览器一直处于连接状态,Web服务器无响应. 2. 从服务器端看(Windows Server 2008 + IIS 7.0),在事件日志中会出现Event ID为5010的错误: A process serving application pool 'q.cnblogs.com' failed to respond

Threading.Tasks.Task多线程 静态全局变量(字典) --只为了记录

--------------------------------------------------------------后台代码------------------------------------------   public JsonResult ImportPDF(Int64 id)        {            try            {                Guid currentGuid = Guid.NewGuid();               

.Net多线程编程—System.Threading.Tasks.Parallel

System.Threading.Tasks.Parallel类提供了Parallel.Invoke,Parallel.For,Parallel.ForEach这三个静态方法. 1 Parallel.Invoke 尽可能并行执行所提供的每个操作,除非用户取消了操作. 方法: 1)public static void Invoke(params Action[] actions); 2)public static void Invoke(ParallelOptions parallelOption

vs2013c#测试using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1_CXY { class Program { stati

首先安装Unit Test Generator.方法为:工具->扩展和更新->联机->搜索“图标为装有蓝色液体的小试管.Unit Test Generator”, 编写代码,生成一个新的类,编写构造函数 与 add()函数.代码如下. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Co

Threading.Tasks 简单的使用

using Lemon.Common; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Timers; using System.Web; namespace Lemon.Stats.Web.Service { public class SynchCacheDataService { /// <summary> /// 定时

C# iis应用程序池控制:回收、启动、停止

https://blog.csdn.net/tanglingbo/article/details/98735819 C# iis应用程序池控制:回收.启动.停止C# winfrom 写的程序 界面图: 实现功能:查询 iis应用程序池,回收.启动.停止 用到的dll:Microsoft.Web.Administration dll路径:C:\Program Files\Microsoft SDKs\Azure\.NET SDK\v2.9\bin\plugins\Diagnostics\Micro

System.Threading

线程:定义为可执行应用程序中的基本执行单元. 应用程序域:一个应用程序内可能有多个线程. 上下文:一个线程可以移动到一个特定的上下文的实体 导入命名空间: //得到正在执行这个方法的线程 Thread currThread = Thread.CurrentThread; //获取正在承载当前线程的应用程序 AppDomain ad = Thread.GetDomain(); //获取当前操作线程所处的上下文 System.Runtime.Remoting.Contexts.Context ct

System.Windows.Forms.Timer、System.Timers.Timer、System.Threading.Timer的差别和分别什么时候用

一.System.Windows.Forms.Timer 1.基于Windows消息循环,用事件方式触发,在界面线程执行:是使用得比较多的Timer,Timer Start之后定时(按设定的Interval)调用挂接在Tick事件上的EvnetHandler.在这种Timer的EventHandler中可 以直接获取和修改UI元素而不会出现问题--因为这种Timer实际上就是在UI线程自身上进行调用的. 2.它是一个基于Form的计时器3.创建之后,你可以使用Interval设置Tick之间的跨