Bug 12112 - async + garbage collector causes SIGSEGV and other strange corruption (with nice repro case)
Summary: async + garbage collector causes SIGSEGV and other strange corruption (with n...
Alias: None
Product: Android
Classification: Xamarin
Component: Pre-release ()
Version: 4.7.x
Hardware: All All
: --- normal
Target Milestone: 4.8 (async)
Assignee: Rodrigo Kumpera
Depends on:
Reported: 2013-05-06 05:33 UTC by T.J. Purtell
Modified: 2013-06-28 14:00 UTC (History)
3 users (show)

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

Test Case Which Crashes on Android up to and including API 14 (4.0) (2.00 KB, text/plain)
2013-05-06 05:33 UTC, T.J. Purtell
Test Case Rewritten to avoid async/await and only use TPL ( (1.57 KB, text/plain)
2013-05-06 05:59 UTC, T.J. Purtell

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 T.J. Purtell 2013-05-06 05:33:56 UTC
Created attachment 3918 [details]
Test Case Which Crashes on Android up to and including API 14 (4.0)

When a GC occurs on Android versions less than 4.1, there is a chance that the garbage collector will corrupt native memory if there are pending async callbacks.  It seems as if something about the pending Task or some of the hoisted variable references are not being handled correctly, leading to miscollection, and then corruption.

I originally noticed this error doing some complicated things involving chattering between C# and a WebView.  Generally, the corruption manifested as A SIGSEGV with no C# stack trace.  However, it occasionally give a much stranger result, such as a NullReferenceException in code that clearly couldn't have produced one, or a NRE inside the generated Java<=>C# binding code.

I was able to make a relatively simple test case to produce the error.  The key part of the code is as follows:

for (int i = 0; i < 10; ++i)
  await Task.Run(() => GC.Collect());
When I rewrote the test case to eliminate all use of the async and await keywords, I was able to run without seeing a crash, presumably confirming that this is an issue at the cross-section of GC+async.

The attached test case was evaluated on these platform
* On Galaxy Nexus, 4.2 - works as expected
* On emux86 4.1  - works as expected
* On mono-api-14-arm - dies
* On Atrix, 2.3  - dies
* On emux86, 2.3 - dies
* On mono-api-10-arm - dies
Comment 1 T.J. Purtell 2013-05-06 05:59:29 UTC
Created attachment 3919 [details]
Test Case Rewritten to avoid async/await and only use TPL (

As I mentioned before, I ran a version of this case where the async/await functionality was not used.  

On my Android 2.3 (Atrix) device, it hung after running for some time and then upon reopening the app, it SIGSEGVed.  This behavior was observed on multiple long runs. 

On the API-10 emulator, it still seems to be working after 30 min.
On the 2.3-x86 emulator, it still seems to be working after 15 min (note that it took longer to repro the crash on this emulator with the first test case).
Comment 2 Jonathan Pryor 2013-05-10 17:43:20 UTC
I'm seeing Attachment #3919 [details] hang on a Nexus 10 w/
Comment 3 Rodrigo Kumpera 2013-05-13 18:04:26 UTC
I can't repro with 4.7.4 on a Galaxy Note. Now trying
Comment 4 Rodrigo Kumpera 2013-05-13 19:02:41 UTC
I can repro it with 4.7.5.
Comment 5 Rodrigo Kumpera 2013-05-15 16:12:07 UTC
Fixed on master. Will be part of 4.7.6.
Comment 6 Saurabh 2013-06-28 14:00:02 UTC
Today we have checked this issue on following builds :

XS 4.0.9 (build 12)
X.Android 4.7.11002

Devices info :

Samsung S2 (2.3.2)
Samsung S2 (4.1.2)
HTC One (4.0.3)
Nexus (4.1.2)
API-10-X86 emulator

We have tried this with attachment given in comment 1 or comment 2( attachment 3919 [details]). Application deploy successfully and relaunch in loop. 

Hence closing this issue. Changing its status to verified