IIS中w3wp进程意外终止:Hangfire服务异常排查求助
已将包含服务端与仪表盘的Hangfire服务部署至IIS,创建了名为HangfireServerDb的数据库。启动IIS后,Hangfire服务端成功生成所需的数据库架构与表,初期所有任务运行正常,但无故障运行数小时后,w3wp.exe进程意外终止,异常信息如下:
Application: w3wp.exe
CoreCLR Version: 6.0.2623.60508
.NET Version: 6.0.26
Description: The process was terminated due to an unhandled exception.
Exception Info: System.AggregateException: One or more hosted services failed to stop. (A task was canceled.)
System.Threading.Tasks.TaskCanceledException: A task was canceled.
at Hangfire.Processing.TaskExtensions.WaitOneAsync(WaitHandle waitHandle, TimeSpan timeout, CancellationToken token) in C:\projects\hangfire-525\src\Hangfire.Core\Processing\TaskExtensions.cs:line 96
at Hangfire.Processing.BackgroundDispatcher.WaitAsync(TimeSpan timeout, CancellationToken cancellationToken) in C:\projects\hangfire-525\src\Hangfire.Core\Processing\BackgroundDispatcher.cs:line 82
at Hangfire.Server.BackgroundProcessingServer.WaitForShutdownAsync(CancellationToken cancellationToken) in C:\projects\hangfire-525\src\Hangfire.Core\Server\BackgroundProcessingServer.cs:line 148
at Microsoft.Extensions.Hosting.Internal.Host.StopAsync(CancellationToken cancellationToken)
End of inner exception stack trace ---
at Microsoft.Extensions.Hosting.Internal.Host.StopAsync(CancellationToken cancellationToken)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.WaitForShutdownAsync(IHost host, CancellationToken token)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
at HangfireDashboard.WebApi.Program.Main(String[] args) in D:\CI_Data\TeamCity\buildAgent2\work\ea4fb37391903d0f\Services\HangfireDashboard\Program.cs:line 22
at HangfireDashboard.WebApi.Program.(String[] args)
当前IIS应用池配置:
- Start Mode: AlwaysRunning
- Recycling > Regular Time Interval: 0
- Managed Pipeline Mode: Integrated
原因分析
- 核心触发点:Hangfire后台处理服务在停止阶段,异步等待操作超时被取消,未处理的
AggregateException直接终止了w3wp.exe进程 - 潜在诱因:
- IIS应用池的隐性回收:即使关闭了定期时间间隔回收,内存限制、空闲超时、请求队列溢出等其他条件仍可能触发进程回收,进而触发Hangfire服务停止流程
- Hangfire版本兼容性:使用的Hangfire版本(对应源码路径
hangfire-525)与.NET 6的Host停止逻辑存在适配问题,导致异步停止时的取消异常未被正确处理 - 系统资源不足:服务器内存、CPU耗尽时,Hangfire的后台调度线程被系统中断,引发停止流程中的任务取消
解决办法
1. 捕获并处理Host终止异常
在Program.cs的Main方法中包裹服务启动逻辑,捕获并过滤停止时的取消异常,避免进程因未处理异常终止:
try { var host = CreateHostBuilder(args).Build(); await host.RunAsync(); } catch (AggregateException ex) { // 仅忽略服务停止时的TaskCanceledException,其他异常正常抛出 var nonCancelExceptions = ex.InnerExceptions.Where(e => !(e is TaskCanceledException)).ToList(); if (nonCancelExceptions.Any()) { throw new AggregateException(nonCancelExceptions); } // 记录日志:服务因取消请求正常停止 }
2. 排查并调整IIS应用池回收配置
- 检查应用池Recycling设置中的内存限制(Private/Virtual Memory Limit),若设置了阈值,调高或取消限制(根据服务器资源情况)
- 确认Idle Time-out (minutes) 是否为0,避免空闲超时触发进程回收
- 开启IIS的Failed Request Tracing,查看事件日志中进程终止前的回收触发原因
3. 升级Hangfire至兼容.NET 6的最新稳定版
旧版本Hangfire与.NET 6的Host异步停止逻辑存在适配问题,升级到1.8.x及以上的稳定版本,可修复此类取消异常的处理逻辑
4. 延长Hangfire服务停止超时时间
在Hangfire服务配置中,增加后台服务停止的超时阈值,避免因等待过短导致任务取消:
services.AddHangfireServer(options => { options.ShutdownTimeout = TimeSpan.FromMinutes(5); // 根据任务执行时长调整 });
5. 配置IIS进程自动重启
确保应用池的Rapid-Fail Protection处于启用状态(默认开启),设置合理的失败次数与时间间隔,确保进程崩溃后自动重启,减少服务中断时长
内容的提问来源于stack exchange,提问作者user23313952

