Task throws UnobservedTaskException in rare occasions only and kills iis process

62 Views Asked by At

Task, in very rare occasions throws UnobservedTaskException and kills the iis process. I could not successfully reproduce it as it happens very rarely and randomly. After some research I found out that the Task is garbage collected somehow. But I am holding the reference to the task anyway so this should not happen.

var task = Task.Factory.StartNew(() =>
{
    processor.DoSomeLongRunningProcess();       
});

var endTask = task.ContinueWith(t =>
{
    if (t.IsCanceled)
    {
        Loggger.Log("Cancelled");
        UpdateStatusToDb(Cancelled);
    }
    else if (t.IsFaulted)
    {
        if (t.Exception != null)
        {
            Loggger.Log("Faulted", e.Exception);
            UpdateStatusToDb("Faulted", e.Exception.Message);
        }
        else
        {
            Loggger.Log("Faulted");
            UpdateStatusToDb("Faulted", "No Exception but Faulted");
        }
    }
    else
    {
        UpdateStatusToDb("Successful");
    }
}, TaskContinuationOptions.None);

Any idea why?

Edit: I am deliberately throwing some Exception inside DoSomeLongRunningProcess(). Those Exceptions are pending inside Task and thrown after some time I guess.

0

There are 0 best solutions below