0

I expect that if I use a fire and forget Task in ASP.NET like below:

Task.Run(() => { throw new NullReferenceException(); });

Thread.Sleep(100);
GC.Collect();
GC.WaitForPendingFinalizers();

and have the following in Web.config:

<ThrowUnobservedTaskExceptions enabled="true"/>

then I should crash the IIS worker process. However, it does not and I do not understand why. If I start a new thread, it indeed crashes the process:

    var thread = new Thread(() => { throw new NullReferenceException(); });
    thread.Start();
    Thread.Sleep(100);

Why is the Task not causing the process to crash?

Tim
  • 514
  • 8
  • 17
  • Have you checked the windows event log? Maybe it's reported as crashed but IIS recovers it without you noticing – Nick.Mc Aug 22 '16 at 04:35
  • Look at this => http://stackoverflow.com/questions/3284137/taskscheduler-unobservedtaskexception-event-handler-never-being-triggered – CodeNotFound Aug 22 '16 at 04:42
  • Nick - no, there are no crashes from the Task case in the event log. – Tim Aug 22 '16 at 04:53
  • CodeNotFound - I was looking at that earlier, however, I've skipped the TaskScheduler.UnobservedTaskException += line. I have also forced garbage collection, hence I would expect it to crash the process. I'm actually running in .Net 4.0, so in theory the the ThrowUnobservedTaskExceptions is redundant. – Tim Aug 22 '16 at 04:54

0 Answers0