Bug 2513 - UnobservedTaskException should not be called from TaskCompletionSource.SetException
Summary: UnobservedTaskException should not be called from TaskCompletionSource.SetExc...
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 7.2.0
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Jérémie Laval
URL:
Depends on:
Blocks:
 
Reported: 2011-12-15 01:27 UTC by Jahmai
Modified: 2016-02-05 16:16 UTC (History)
5 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Jahmai 2011-12-15 01:27:23 UTC
The UnobservedTaskException event on System.Threading.Tasks.TaskScheduler is designed as a last-chance opportunity to observe an exception thrown during execution of a Task before the Finalizer thread throws it as an unhanded exception and essentially kills the app.

It appears that by using TaskCompletionSource and calling the SetException method, that the UnobservedTaskException event is fired immediately when in fact the correct behaviour is to mark the exception as unhanded internally, and the only call the UnobservedTaskException event upon Finalization.
Comment 1 Rolf Bjarne Kvinge [MSFT] 2011-12-20 03:35:12 UTC
Jérémie, this looks like something you might know about.
Comment 2 Jérémie Laval 2012-02-03 10:27:36 UTC
Hello,

Could you provide a test case or relevant snippet of your program where this behavior happen? I couldn't reproduce it on simulator.

Thanks.
Comment 3 Jahmai 2012-02-27 07:02:36 UTC
Sorry for the late reply. I will re-test this with the latest MonoTouch and submit a snippet if it is still reproducible.
Comment 4 Jérémie Laval 2012-11-15 10:25:29 UTC
Closing since it has been a long time. Don't hesitate to reopen if that bug still exists.
Comment 5 Jahmai 2014-03-28 02:02:46 UTC
Reopening this as it is happening in iOS 7.2.0.

This function exemplifies the issue:

        private async Task AwaitOnTcsTask()
        {
            TaskScheduler.UnobservedTaskException += (sender, e) => { Console.WriteLine("You should not see this!"); };

            TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();

            tcs.SetException(new Exception());

            try
            {
                await tcs.Task;
            }
            catch (Exception)
            {
            }
        }
Comment 6 Rolf Bjarne Kvinge [MSFT] 2016-02-05 16:16:48 UTC
I can't reproduce this with the code from comment #5, so it looks like it's been fixed (probably when importing the reference source from Microsoft).

My test project: https://github.com/rolfbjarne/TestApp/tree/bug2513

If you can still reproduce, please provide a complete test project.