12

I just transitioned some of my website's code from using QueueUserWorkItem to Task.Factory.StartNew

I have some bad code that threw an Exception and it ultimately shut down w3wp. Running IIS 7.5 on Windows Server 2008 R2, x64

                Task.Factory.StartNew(() =>
                {
                    MethodThatThrowsException();
                }

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()

Exception: System.AggregateException

Message: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread.

StackTrace: at System.Threading.Tasks.TaskExceptionHolder.Finalize()

InnerException: System.Data.SqlClient.SqlException

I would have assumed an exception would have generated an event log and NOT have killed w3wp. Is this a wrong assumption?

Visualize
  • 590
  • 5
  • 12
  • 2
    Kind of a silly question because your question basically answers your question. Of course your assumption is wrong or you wouldn't be asking this question. That being said I gave your question and up vote because it is something I hadn't realized myself. My guess is that when an exception is thrown normally there is a global error handler that catches the error. When you use the new thread with the task factory you are jumping outside the normal error handling... This is something that is good to know... or perhaps you found a bug! – John Sobolewski Feb 20 '11 at 02:33

1 Answers1

9

Unhandled exceptions crash IIS worker process. This is by design. http://support.microsoft.com/kb/911816

Therefore, you should properly handle exceptions.

Lex Li
  • 60,503
  • 9
  • 116
  • 147