Bug 50210 - GC causes app to crash after ModernHttpClient throws an exception
Summary: GC causes app to crash after ModernHttpClient throws an exception
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 7.0 (C8)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
Depends on:
Reported: 2016-12-16 13:16 UTC by Daniel
Modified: 2017-06-13 21:51 UTC (History)
5 users (show)

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

Simple test project for reproducing the issue (2.23 MB, application/x-zip-compressed)
2016-12-16 13:16 UTC, Daniel

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:

Description Daniel 2016-12-16 13:16:40 UTC
Created attachment 18909 [details]
Simple test project for reproducing the issue

We have an app that is using ModernHttpClient to make HTTP requests. We've noticed that if ModernHttpClient throws an exception (that we catch) the app will crash the next time that a garbage collect is performed. When the app crashes it logs a "JNI DETECTED ERROR IN APPLICATION: use of invalid jobject 0x7f58cec5e0" error and a huge stack trace.

This only seems to happen on Cycle 8 releases (not on Cycle 7 or the most recent Cycle 9 release, Xamarin.Android Also, it only happens when ModernHttpClient is used. If I switch to HttpClientHandler or AndroidClientHandler the app doesn't crash.

I've broken out the code from our project to a simple test project.

Steps to reproduce:

	1. Put the device in airplane mode
	2. Launch the app
	3. Press the "Send get request" button
		a. An exception will be thrown since the device doesn't have a connection.
	4. Press the "Send get request" button a second time
	5. Press the "Manual GC call" button

In the test project that I've attached I need to send two requests for the issue to occur, in our real project the issue occurs after one request.

I don't know if this is a bug in Xamarin or in ModernHttpClient, but even if it is a bug in ModernHttpClient I would like to know why it only occurs in Cycle 8 releases. If I try the same thing in Cycle 7 (Xamarin.Android or Cycle 9 (Xamarin.Android releases the app doesn't crash.
Comment 1 Daniel 2016-12-16 13:29:14 UTC
Just noticed that this may be a duplicate of: https://bugzilla.xamarin.com/show_bug.cgi?id=45003
Comment 2 David Laundav 2017-02-06 11:59:39 UTC
Any news in regards to this issue?
Comment 3 Anton Morozov 2017-02-15 10:26:04 UTC
The bug is caused by ModernHttp setting a Java exception into a Task.

See this comment in in ModernHttp repo: 

It can be easily fixed in ModernHttp code.
Comment 4 Cody Beyer (MSFT) 2017-06-13 21:51:13 UTC
This has been fixed in a fork of ModernHTTPClient, however that fork has not turned into a PR as far as I can tell.

Please see https://github.com/Youscribe/ModernHttpClient/tree/hotfix/cycle8-issue