Friday, June 8, 2012

Uncaught Exception From Delegate Passed To Task.Factory.StartNew() Causes Windows Service To Crash

We have a Windows service written in C# against the .NET 4.0 framework, and it began crashing with the following two items written into the Application event log each time:
EventType clr20r3, P1 x.exe, P2 1.8.1.0, P3 4fd0f5c1, P4 system, P5 4.0.0.0, P6 4ba1dff4, P7 2e49, P8 12d, P9 system.aggregateexception, P10 NIL.
Application: x.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()
Some googling around yielded the following helpful information:


in which it mentions a case in which an error gets thrown within that Finalize() call.

I ended up finding a library our application referenced in which the author called Task.Factory.StartNew(), passing in a delegate which had a catch block which could itself throw an exception--which would end up being considered unhandled in that Finalize() call, and crash the process during garbage collection at some point after the exception occurred.

We're in the process of having that library corrected.

Note that Parallel.ForEach() shouldn't have this problem, because it says that all its tasks' exceptions will be bundled up into an AggregateException and returned to the caller.

No comments:

Post a Comment