UnobserverTaskException with async controllerhttp://forums.asp.net/t/1794442.aspx/1?UnobserverTaskException+with+async+controllerTue, 01 May 2012 15:35:27 -040017944424939815http://forums.asp.net/p/1794442/4939815.aspx/1?UnobserverTaskException+with+async+controllerUnobserverTaskException with async controller <p>We are implementing async controllers in ASP.NET MVC4 using tasks. If the task fails the worker process crashes with something like this:</p> <pre class="prettyprint"><strong>Exception: System.AggregateException</strong> 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.</pre> <p>Looks like a bug or are we missing something?</p> 2012-04-19T00:13:31-04:004939823http://forums.asp.net/p/1794442/4939823.aspx/1?Re+UnobserverTaskException+with+async+controllerRe: UnobserverTaskException with async controller <p>Are you actually using the AsyncController base class? What does your controller look like?</p> 2012-04-19T00:26:20-04:004939936http://forums.asp.net/p/1794442/4939936.aspx/1?Re+UnobserverTaskException+with+async+controllerRe: UnobserverTaskException with async controller <p></p> <blockquote><span class="icon-blockquote"></span> <h4>alexivanoff</h4> <p></p> <p>If the task fails the worker process crashes with something like this:</p> <pre class="prettyprint"><strong>Exception: System.AggregateException</strong></pre> <p></p> </blockquote> <p></p> <p>And this is normal.What would you do the task execution to do in error case?!</p> <p>I would surrender the task execution with try/catch and log the errror.</p> <p></p> 2012-04-19T03:05:24-04:004941633http://forums.asp.net/p/1794442/4941633.aspx/1?Re+UnobserverTaskException+with+async+controllerRe: UnobserverTaskException with async controller <pre class="prettyprint">public class HomeController : Controller { public Task&lt;ViewResult&gt; Index() { return &lt;task that faults&gt;; } }</pre> <p></p> 2012-04-19T18:30:00-04:004941637http://forums.asp.net/p/1794442/4941637.aspx/1?Re+UnobserverTaskException+with+async+controllerRe: UnobserverTaskException with async controller <p>Are you saying that this is by design and we should catch all errors and return error view in this case?</p> 2012-04-19T18:31:14-04:004941643http://forums.asp.net/p/1794442/4941643.aspx/1?Re+UnobserverTaskException+with+async+controllerRe: UnobserverTaskException with async controller <p>You should really consider using the asynccontroller instead of just controller. It was built for this kind of scenario.<br> <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.asynccontroller.aspx">http://msdn.microsoft.com/en-us/library/system.web.mvc.asynccontroller.aspx</a></p> <p><a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.asynccontroller.aspx"></a>Check out this blog post for more info:<br> <a href="http://www.aaronstannard.com/post/2011/01/06/asynchonrous-controllers-ASPNET-mvc.aspx">http://www.aaronstannard.com/post/2011/01/06/asynchonrous-controllers-ASPNET-mvc.aspx</a></p> <p>&nbsp;</p> 2012-04-19T18:34:48-04:004943369http://forums.asp.net/p/1794442/4943369.aspx/1?Re+UnobserverTaskException+with+async+controllerRe: UnobserverTaskException with async controller <p>In ASP.MET MVC 4 beta AsyncController <strong>&quot;Provided for backward compatibility with ASP.NET MVC 3.&quot;</strong></p> 2012-04-20T17:25:40-04:004944717http://forums.asp.net/p/1794442/4944717.aspx/1?Re+UnobserverTaskException+with+async+controllerRe: UnobserverTaskException with async controller <p>I think you are hitting <a href="http://forums.asp.net/p/1778103/4882836.aspx/1?Re&#43;Using&#43;an&#43;Async&#43;Action&#43;to&#43;Run&#43;Synchronous&#43;Code"> this bug </a>of ASP.NET MVC 4 beta.</p> 2012-04-22T10:49:19-04:004947312http://forums.asp.net/p/1794442/4947312.aspx/1?Re+UnobserverTaskException+with+async+controllerRe: UnobserverTaskException with async controller <p>So, is this by design or should I report a bug?</p> 2012-04-23T21:38:13-04:004947500http://forums.asp.net/p/1794442/4947500.aspx/1?Re+UnobserverTaskException+with+async+controllerRe: UnobserverTaskException with async controller <p></p> <blockquote><span class="icon-blockquote"></span> <h4>alexivanoff</h4> <p></p> <p>Are you saying that this is by design and we should catch all errors and return error view in this case?</p> <p></p> </blockquote> <p></p> <p>it is very poor practice for async tasks to throw errors. they should return an exception status. if you are running a true async task, the thread may be performing an operation for another request when the error is throw. this is just ugly to manage. &nbsp;.net changed thread pools to eat exceptions (except thread abort) due to simular problems.&nbsp;</p> <p></p> 2012-04-24T03:26:26-04:004947541http://forums.asp.net/p/1794442/4947541.aspx/1?Re+UnobserverTaskException+with+async+controllerRe: UnobserverTaskException with async controller <p></p> <blockquote><span class="icon-blockquote"></span> <h4>alexivanoff</h4> <p></p> <p>So, is this by design or should I report a bug?</p> <p></p> </blockquote> <p></p> <p>PLease see this for detail about unobserve exception.</p> <p>http://bradwilson.typepad.com/blog/2012/04/tpl-and-servers-pt3.html</p> <p></p> 2012-04-24T04:11:33-04:004951683http://forums.asp.net/p/1794442/4951683.aspx/1?Re+UnobserverTaskException+with+async+controllerRe: UnobserverTaskException with async controller <p>Would you please elaborate on &quot;return exception status&quot; and when .NET thread pool started to eat exceptions?</p> <p>The task that we are returning is doying what every other task does when operation fails: propagates the error. And when the caller accesses task result and/or waits on task completion the AggregateException is thrown.</p> 2012-04-25T23:36:13-04:004955626http://forums.asp.net/p/1794442/4955626.aspx/1?Re+UnobserverTaskException+with+async+controllerRe: UnobserverTaskException with async controller <p>Let me try to ask my question differently. What is the proper way to write async controller action that returns Task than may fail?</p> 2012-04-27T23:55:18-04:004955696http://forums.asp.net/p/1794442/4955696.aspx/1?Re+UnobserverTaskException+with+async+controllerRe: UnobserverTaskException with async controller <p></p> <blockquote><span class="icon-blockquote"></span> <h4>alexivanoff</h4> Let me try to ask my question differently. What is the proper way to write async controller action that returns Task than may fail?</blockquote> <p></p> <p>I think you have not seen the articles of Brad above. In short, in asynchronous world of ASP.NET exception should only be thrown when you called EndXX callback. For Task here is a quote from,</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Jeffrey Ritcher</h4> <p></p> <p>If the compute-bound task throws an unhandled exception, the exception will be swallowed,<br> stored in a collection, and the thread pool thread is allowed to return to the thread pool.<br> When the Wait method or the Result property is invoked, these members will throw a<br> System.AggregateException object.</p> <p>If you never call Wait or Result or query a Tasks Exception property, then your<br> code never observes that this exception has occurred. This is not ideal, as your program has experienced<br> an unexpected problem that you are not aware of. So, when a Task object is garbage<br> collected, its Finalize method checks to see if the Task experienced an unobserved exception;<br> if it has, Tasks Finalize method throws the AggregateException. Since you cannot catch<br> an exception thrown by the CLRs finalizer thread, your process is terminated immediately. You<br> must fix your code by invoking one of the aforementioned members, ensuring that your code<br> observes the exception and recovers from it.<br> </p> </blockquote> <p></p> 2012-04-28T03:29:27-04:004956570http://forums.asp.net/p/1794442/4956570.aspx/1?Re+UnobserverTaskException+with+async+controllerRe: UnobserverTaskException with async controller <p>There is no exception thrown, there is just a faulted task:</p> <pre class="prettyprint">public class HomeController : Controller { public Task&lt;ViewResult&gt; Index() { return Task.Factory.StartNew&lt;ViewResult&gt;(() =&gt; throw new InvalidOperationException(); ); } }</pre> <p>So, we return Task to ASP.NET MVC 4 runtime. At this point,&nbsp;isn't it ASP.NET MVC 4 runtime responsibility to &quot;call Wait or Result or query a Tasks Exception property&quot;?</p> <p>&nbsp;</p> 2012-04-29T04:14:19-04:004960175http://forums.asp.net/p/1794442/4960175.aspx/1?Re+UnobserverTaskException+with+async+controllerRe: UnobserverTaskException with async controller <p></p> <blockquote><span class="icon-blockquote"></span> <h4>alexivanoff</h4> There is no exception thrown, there is just a faulted task:</blockquote> <p></p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>alexivanoff</h4> <span class="pln"></span><span class="kwd">throw</span><span class="pln"> </span> <span class="kwd">new</span><span class="pln"> </span><span class="typ">InvalidOperationException</span><span class="pun">()</span></blockquote> <p></p> <p>This is an exception.</p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>alexivanoff</h4> So, we return Task to ASP.NET MVC 4 runtime. At this point,&nbsp;isn't it ASP.NET MVC 4 runtime responsibility to &quot;call Wait or Result or query a Tasks Exception property&quot;?</blockquote> <p></p> <p>I think it will check IsCompleted. If task is not completed then ContinueWith(which will only execute if task is completed, faulted, etc)</p> 2012-05-01T15:35:27-04:00