System.Threading.Tasks.Task 引起的 IIS 應用池崩潰


接口服務運行一段時間后,IIS應用池就會突然掛掉,事件查看日志,會有事件日志Event ID為5011的錯誤

為應用程序池“PokeIn”提供服務的進程在與 Windows Process Activation Service 通信時出現嚴重錯誤。該進程 ID 為“2268”。數據字段包含錯誤號。

image

最后直接程序池直接被禁用

應用程序池“PokeIn”將被自動禁用,原因是為此應用程序池提供服務的進程中出現一系列錯誤。

查看管理事件

image

Application Error

錯誤應用程序名稱: w3wp.exe,版本: 7.5.7601.17514,時間戳: 0x4ce7a5f8

錯誤模塊名稱: KERNELBASE.dll,版本: 
6.1.7601.17514,時間戳: 0x4ce7bafa

異常代碼: 0xe0434352
錯誤偏移量: 0x0000b727
錯誤進程 ID: 0xc1c
錯誤應用程序啟動時間: 0x01d045ca298f9b3c
錯誤應用程序路徑: C:\Windows\SysWOW64\inetsrv\w3wp.exe 錯誤模塊路徑: C:\Windows\syswow64\KERNELBASE.dll 報告 ID: eb60afd0
-b1bd-11e4-9f2d-005056a934bb

.NET Runtime

Application: w3wp.exe Framework Version: v4.0.30319

Description: The process was terminated due to an unhandled exception. Exception Info: System.AggregateException Stack: at System.Threading.Tasks.TaskExceptionHolder.Finalize()

初步排查代碼確定有可能的幾點

1.項目中使用了dapper ,使用了using,跟蹤了源碼發現已經有對連接池的處理

int flagNum = new SqlConnection(DBSetting.XXX).Execute("SPS_BookForApp", param, commandType: CommandType.StoredProcedure);

 2.由之前的委托異步改為基於任務Task的異步

Func<string, string> func = f =>
            {
                if (!string.IsNullOrEmpty(orderNo) &&orderNo.IsPositiveInt())
                {
                    //記錄訂單量
                }
                else
                {
                    return string.Empty;
                }
            };
            func.BeginInvoke("Logging Order Data", ir =>
            {
                var ar = (AsyncResult)ir;
                var fun = (Func<string, string>)ar.AsyncDelegate;
                string returnValue = fun.EndInvoke(ir);
                if (!string.IsNullOrEmpty(returnValue))
                {
                    //判斷是否記錄成功
                }
            }, null);

改為

Task.Factory.StartNew(() =>
                        {
                            //
                        })

沒有去捕獲異常,想起之前看過的dudu說過這樣的問題 ,於是修改了下

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

現運行良好。

Refer:
Exception Handling (Task Parallel Library)
https://msdn.microsoft.com/en-us/library/dd997415.aspx
http://www.cnblogs.com/dudu/archive/2012/04/05/task_unhandled_exception_application_crash.html
Exception Handling with the Task Parallel Library
http://www.ademiller.com/blogs/tech/2010/10/exception-handling-with-the-task-parallel-library/
Exception while running system threading tasks task
http://stackoverflow.com/questions/15804059/exception-while-running-system-threading-tasks-task
http://stackoverflow.com/questions/7883052/a-tasks-exceptions-were-not-observed-either-by-waiting-on-the-task-or-accessi/7883083

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM