Bug 34937 - Using System.Net.WebException triggers AndroidRuntime FATAL EXCEPTION
Summary: Using System.Net.WebException triggers AndroidRuntime FATAL EXCEPTION
Status: RESOLVED ANSWERED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 5.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2015-10-16 07:17 UTC by Håkon Løvdal
Modified: 2015-11-04 03:03 UTC (History)
1 user (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 ANSWERED

Description Håkon Løvdal 2015-10-16 07:17:07 UTC
We have the following function

        private async Task<string> HttpGet(string method)
        {
            var webServiceUrl = GetWebServiceUrl();
            if (webServiceUrl == null)
            {
                return null;
            }

            try
            {
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(JsonMediaType));
                    client.Timeout = new TimeSpan(0, 0, 10);

                    var url = webServiceUrl + method;
                    var response = await client.GetAsync(url);
                    response.EnsureSuccessStatusCode();

                    return await response.Content.ReadAsStringAsync();
                }
            }
            catch (System.Net.WebException ex)
            {
                LogHttpException(ex, "GET", method, webServiceUrl);
                return null;
            }
        }

and when trying to access an url that is not accessible the system crashes like

10-16 12:25:38.070: I/MonoDroid(21810): UNHANDLED EXCEPTION:
10-16 12:25:38.070: I/MonoDroid(21810): System.Threading.Tasks.TaskCanceledException: A task was canceled.
10-16 12:25:38.070: I/MonoDroid(21810): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61
10-16 12:25:38.070: I/MonoDroid(21810): at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (object) [0x00000] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1006
10-16 12:25:38.070: I/MonoDroid(21810): at Android.App.SyncContext/<Post>c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/2185/53fce373/source/monodroid/src/Mono.Android/src/Android.App/SyncContext.cs:18
10-16 12:25:38.070: I/MonoDroid(21810): at Java.Lang.Thread/RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/2185/53fce373/source/monodroid/src/Mono.Android/src/Java.Lang/Thread.cs:36
10-16 12:25:38.070: I/MonoDroid(21810): at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) [0x00009] in /Users/builder/data/lanes/2185/53fce373/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Java.Lang.IRunnable.cs:71
10-16 12:25:38.070: I/MonoDroid(21810): at (wrapper dynamic-method) object.79a2dcc9-88cb-4828-b30c-27761d4002e4 (intptr,intptr) <IL 0x00011, 0x0003b>
10-16 12:25:38.390: D/dalvikvm(21810): WAIT_FOR_CONCURRENT_GC blocked 0ms
10-16 12:25:38.414: D/AbsListView(21810): [unregisterDoubleTapMotionListener]
10-16 12:25:38.414: D/dalvikvm(21810): GC_EXPLICIT freed 171K, 15% free 9690K/11271K, paused 2ms+2ms, total 28ms
10-16 12:25:38.429: D/Mono(21810): GC_OLD_BRIDGE num-objects 259 num_hash_entries 1090 sccs size 776 init 0.00ms df1 2.90ms sort 0.40ms dfs2 1.01ms setup-cb 0.46ms free-data 0.82ms links 1257/1257/850/9 dfs passes 2606/2033
10-16 12:25:38.429: D/Mono(21810): GC_MAJOR: (mature allocation failure) pause 33.75ms, total 34.06ms, bridge 44.59ms major 2288K/1936K los 5931K/15882K
10-16 12:25:38.562: I/dalvikvm(21810): Could not find method java.lang.Throwable.<init>, referenced from method md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable.<init>
10-16 12:25:38.562: W/dalvikvm(21810): VFY: unable to resolve direct method 15871: Ljava/lang/Throwable;.<init> (Ljava/lang/String;Ljava/lang/Throwable;ZZ)V
10-16 12:25:38.562: D/dalvikvm(21810): VFY: replacing opcode 0x70 at 0x0000
10-16 12:25:38.570: D/AndroidRuntime(21810): Shutting down VM
10-16 12:25:38.570: W/dalvikvm(21810): threadid=1: thread exiting with uncaught exception (group=0x41c812a0)
10-16 12:25:40.507: D/dalvikvm(630): GC_CONCURRENT freed 467K, 31% free 9113K/13191K, paused 18ms+5ms, total 93ms
10-16 12:25:42.117: E/AndroidRuntime(21810): FATAL EXCEPTION: main
10-16 12:25:42.117: E/AndroidRuntime(21810): java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
10-16 12:25:42.117: E/AndroidRuntime(21810): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
10-16 12:25:42.117: E/AndroidRuntime(21810): 	at dalvik.system.NativeStart.main(Native Method)
10-16 12:25:42.117: E/AndroidRuntime(21810): Caused by: java.lang.reflect.InvocationTargetException
10-16 12:25:42.117: E/AndroidRuntime(21810): 	at java.lang.reflect.Method.invokeNative(Native Method)
10-16 12:25:42.117: E/AndroidRuntime(21810): 	at java.lang.reflect.Method.invoke(Method.java:511)
10-16 12:25:42.117: E/AndroidRuntime(21810): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
10-16 12:25:42.117: E/AndroidRuntime(21810): 	... 2 more
10-16 12:25:42.117: E/AndroidRuntime(21810): Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.Threading.Tasks.TaskCanceledException: A task was canceled.
10-16 12:25:42.117: E/AndroidRuntime(21810): at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61
10-16 12:25:42.117: E/AndroidRuntime(21810): at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (object) [0x00000] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1006
10-16 12:25:42.117: E/AndroidRuntime(21810): at Android.App.SyncContext/<Post>c__AnonStorey0.<>m__0 () [0x00000] in /Users/builder/data/lanes/2185/53fce373/source/monodroid/src/Mono.Android/src/Android.App/SyncContext.cs:18
10-16 12:25:42.117: E/AndroidRuntime(21810): at Java.Lang.Thread/RunnableImplementor.Run () [0x0000b] in /Users/builder/data/lanes/2185/53fce373/source/monodroid/src/Mono.Android/src/Java.Lang/Thread.cs:36
10-16 12:25:42.117: E/AndroidRuntime(21810): at Java.Lang.IRunnableInvoker.n_Run (intptr,intptr) [0x00009] in /Users/builder/data/lanes/2185/53fce373/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Java.Lang.IRunnable.cs:71
10-16 12:25:42.117: E/AndroidRuntime(21810): at (wrapper dynamic-method) object.79a2dcc9-88cb-4828-b30c-27761d4002e4 (intptr,intptr) <IL 0x00011, 0x0003b>
10-16 12:25:42.117: E/AndroidRuntime(21810): 	at mono.java.lang.RunnableImplementor.n_run(Native Method)
10-16 12:25:42.117: E/AndroidRuntime(21810): 	at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)
10-16 12:25:42.117: E/AndroidRuntime(21810): 	at android.os.Handler.handleCallback(Handler.java:615)
10-16 12:25:42.117: E/AndroidRuntime(21810): 	at android.os.Handler.dispatchMessage(Handler.java:92)
10-16 12:25:42.117: E/AndroidRuntime(21810): 	at android.os.Looper.loop(Looper.java:137)
10-16 12:25:42.117: E/AndroidRuntime(21810): 	at android.app.ActivityThread.main(ActivityThread.java:4949)
10-16 12:25:42.117: E/AndroidRuntime(21810): 	... 5 more


However if I replace the catch line with plain
            catch (Exception ex)
instead, then an exception is trown like expected which we catch and continue running:

10-16 12:21:44.890: XXXX(21476): TID[21476] Exception calling GET method xxxxxxxx with url http://10.0.0.10/xxxxxx/: System.Threading.Tasks.TaskCanceledException: A task was canceled.
10-16 12:21:44.890: XXXX(21476):   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x0002d] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:193 
10-16 12:21:44.890: XXXX(21476):   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:170 
10-16 12:21:44.890: XXXX(21476):   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:142 
10-16 12:21:44.890: XXXX(21476):   at System.Runtime.CompilerServices.TaskAwaiter`1[System.Net.Http.HttpResponseMessage].GetResult () [0x00000] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:372 
10-16 12:21:44.890: XXXX(21476):   at System.Net.Http.HttpClientHandler+<SendAsync>c__async0.MoveNext () [0x004af] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.cs:364 
10-16 12:21:44.890: XXXX(21476): --- End of stack trace from previous location where exception was thrown ---
10-16 12:21:44.890: XXXX(21476):   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61 
10-16 12:21:44.890: XXXX(21476):   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00027] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:190 
10-16 12:21:44.890: XXXX(21476):   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:170 
10-16 12:21:44.890: XXXX(21476):   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:142 
10-16 12:21:44.890: XXXX(21476):   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[System.Net.Http.HttpResponseMessage].GetResult () [0x00000] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:580 
10-16 12:21:44.890: XXXX(21476):   at System.Net.Http.HttpClient+<SendAsyncWorker>c__async0.MoveNext () [0x000a9] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:274 
10-16 12:21:44.890: XXXX(21476): --- End of stack trace from previous location where exception was thrown ---
10-16 12:21:44.890: XXXX(21476):   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000b] in /Users/builder/data/lanes/2185/53fce373/source/mono/mcs/class/corlib/System.Runtime.ExceptionServices/ExceptionDispatchInfo.cs:61 
10-16 12:21:44.890: XXXX(21476):   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) [0x00027] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:190 
10-16 12:21:44.890: XXXX(21476):   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) [0x0002e] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:170 
10-16 12:21:44.890: XXXX(21476):   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd (System.Threading.Tasks.Task task) [0x0000b] in /Users/builder/data/lanes/2185/53fce373/source/mono/external/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:142 
10-16 12:21:44.890: XXXX(21476):   at System.Runtime.CompilerServices.


Searching internet for md52ce486a14f4bcd95899665e9d932190b only brings up Xamarin related posts. Some of them points to using WebView (like https://bugzilla.xamarin.com/show_bug.cgi?id=29197) and when just referencing WebException trigger this bug that makes sence.

There might be other things than WebException that trigger this as well, but at least with this knowledge it should be easy to reproduce. https://bugzilla.xamarin.com/show_bug.cgi?id=31338 might give some clues to what the causes are.

In case it matters, Xamarin is updated to latest, compiling with Minimum API Level 15, running on Galaxy tab 4.1.2.
Comment 1 Jonathan Pryor 2015-10-16 12:22:01 UTC
> We have the following function
> 
>        private async Task<string> HttpGet(string method)
>        {
>            ...
>            try
>            {
>                ...
>            }
>            catch (System.Net.WebException ex)
>            {
>                ...
>            }
>        }

So you have a method which catches WebException.

Then you call it, and:

> 10-16 12:25:38.070: I/MonoDroid(21810): UNHANDLED EXCEPTION:
> 10-16 12:25:38.070: I/MonoDroid(21810): System.Threading.Tasks.TaskCanceledException: A task was canceled.

A type that ISN'T a WebException is thrown. Consequently, your `catch` block doesn't handle it.

Aside: why is a TaskCanceledException thrown? *Probably* because you asked it to by setting the timeout:

>                     client.Timeout = new TimeSpan(0, 0, 10);

Next, you change your `catch` block to catch all Exceptions instead of just WebExceptions:

>            catch (Exception ex)

That now causes the thrown TaskCanceledException to be caught, so your app continues execution.

This looks like expected behavior to me.
Comment 2 Håkon Løvdal 2015-11-04 03:03:24 UTC
Yes, you are correct. After some research, HttpClient throws a couple of exceptions in addition to WebException, so the following now is used:

            //// Apparently the following mix of exceptions are possible errors thrown by HttpClient.
            catch (AggregateException ex)
            {
                LogHttpException(ex, "GET", method, webServiceUrl);
                return null;
            }
            catch (TaskCanceledException ex)
            {
                LogHttpException(ex, "GET", method, webServiceUrl);
                return null;
            }
            catch (System.Net.WebException ex)
            {
                LogHttpException(ex, "GET", method, webServiceUrl);
                return null;
            }