Bug 16676 - Tasks don't handle OperationCancelledException correctly
Summary: Tasks don't handle OperationCancelledException correctly
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.10.1
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2013-12-09 10:22 UTC by Grigory (Playtika)
Modified: 2015-07-13 15:53 UTC (History)
2 users (show)

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


Attachments
Crash (203.34 KB, image/png)
2013-12-10 02:36 UTC, Grigory (Playtika)
Details


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 Grigory (Playtika) 2013-12-09 10:22:18 UTC
try
{
	await ThrowMyException();
        // Unhandled TaskCancelledException here
}
catch(MyException)
{
	Toast.MakeText(this,"Wohoo", ToastLength.Long).Show();
	// We don't get here. WTF?!

}

------------------------------------

public async Task ThrowMyException()
{
	throw new MyException ();
}
-------------------------------------

public class MyException : System.OperationCanceledException
{
}

Im not sure if this is a Mono bug or a Xamarin.Android. But anyway this is quite buggy logic
Comment 1 Grigory (Playtika) 2013-12-09 10:27:47 UTC
And please review all your code related to tasks and Aggregate Exceptions.
I have a project for Android and Windows (dekstop Windows Forms app on .NET 4.5)
On Android i see huge amount of unhandled Aggregate exceptions. I've checked every line of code. I suppose that situation there is like this. Some exceptions are simply not caught. I don't know why but this is really a BLOCKER issue in Xamarin. All those cool contact and GPS apis have zero value when such issues exist.
Comment 2 Jonathan Pryor 2013-12-09 14:03:45 UTC
I'm going to need a more complete test case. The following code works for me:

  using System;
  using System.Threading.Tasks;
  using Android.App;
  using Android.Widget;
  using Android.OS;

  namespace Scratch.Bxc16676 {
    [Activity (Label = "Scratch.Bxc16676", MainLauncher = true)]
    public class MainActivity : Activity {
      int count = 1;

      protected override async void OnCreate (Bundle bundle)
      {
        base.OnCreate (bundle);

        try {
          await ThrowMyException ();
        } catch (MyException e) {
          Console.WriteLine ("Caught exception: {0}", e);
        }

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button> (Resource.Id.myButton);
      
        button.Click += delegate {
          button.Text = string.Format ("{0} clicks!", count++);
        };
      }

      public static async Task ThrowMyException ()
      {
        throw new MyException ();
      }
    }

    class MyException : System.OperationCanceledException {
    }
  }

The above writes the following to `adb logcat`:

> I/mono-stdout( 3566): Caught exception: Scratch.Bxc16676.MyException: The operation was canceled.
> I/mono-stdout( 3566):   at Scratch.Bxc16676.MainActivity+<ThrowMyException>c__async1.MoveNext ()
> I/mono-stdout( 3566):   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[<ThrowMyException>c__async1] (Scratch.Bxc16676.<ThrowMyException>c__async1& stateMachine)
> I/mono-stdout( 3566):   at Scratch.Bxc16676.MainActivity.ThrowMyException ()
> I/mono-stdout( 3566):   at Scratch.Bxc16676.MainActivity+<OnCreate>c__async0.MoveNext () [0x00061]

Which Xamarin.Android version are you using?
Comment 3 Grigory (Playtika) 2013-12-09 17:00:10 UTC
Xamarin.Android
Version: 4.10.1 (Enterprise Edition)

Stable branch.

I'll attach repro project tomorrow. App crashes for me with same code.
Comment 4 Grigory (Playtika) 2013-12-10 02:36:01 UTC
Created attachment 5626 [details]
Crash
Comment 5 Grigory (Playtika) 2013-12-10 03:14:36 UTC
I've ran your code and it has crashed.
Comment 6 Jonathan Pryor 2013-12-10 12:31:21 UTC
I suspect this may be a dupe of Bug #14824, which has been fixed in monodroid/master (which I was testing on -- sorry) but exists in XA 4.8.x.
Comment 7 Grigory (Playtika) 2013-12-10 13:31:04 UTC
This is great. What can we do to get this fix ASAP ? This is quite critical for the application release. AFAIK my company has Ultimate license, can we somehow use it ?
Comment 8 Grigory (Playtika) 2015-07-13 15:53:18 UTC
Doesn't reproduce anymore on 5.0+