Bug 40228 - Calling Task.Wait in on OnStateChanged on main thread hangs the app
Summary: Calling Task.Wait in on OnStateChanged on main thread hangs the app
Status: VERIFIED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.2.0
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-04-08 09:41 UTC by Kasper
Modified: 2016-04-21 17:24 UTC (History)
3 users (show)

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


Attachments
Sample solution (35.56 KB, application/x-gzip)
2016-04-08 09:41 UTC, Kasper
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:
VERIFIED FIXED

Description Kasper 2016-04-08 09:41:08 UTC
Created attachment 15651 [details]
Sample solution

After upgrading to XF 2.2.0-pre1 my app hangs on Android if I leave the app (using back-button or home), and start it again by pressing the app icon.

I think I traced the problem to the OnStateChanged method on FormsApplicationActivity.

I took the liberty to use the build in decompiler in Xamarin Studio. The method looks like this

private void OnStateChanged ()
{
	if (this._application == null) {
		return;
	}
	if (this._previousState == AndroidApplicationLifecycleState.OnCreate && this._currentState == AndroidApplicationLifecycleState.OnStart) {
		this._application.SendStart ();
		return;
	}
	if (this._previousState == AndroidApplicationLifecycleState.OnStop && this._currentState == AndroidApplicationLifecycleState.OnRestart) {
		this._application.SendResume ();
		return;
	}
	if (this._previousState == AndroidApplicationLifecycleState.OnPause && this._currentState == AndroidApplicationLifecycleState.OnStop) {
		this._application.SendSleepAsync ().Wait ();
	}
}

Note the construction SendSleepAsync().Wait(). If this happens on the main thread, as it does in my case, it will deadlock, because, because of the SynchronizationContext does not allow to use a different thread.

I have attached a simple reproduction. Actually I just created a XF project with build in template in Xamarin Studio and upgrade XF to 2.2.0-pre1.

To see the issue, just start the app (note the "Wellcome to Xamarin Form" message), press back, and start again. This will hang the app (ie. no wellcome message will be shown).

If you break it in the debugger, you will see the following stack trace:

System.Threading.Monitor.Monitor_wait() in 
System.Threading.Monitor.Wait(object obj, int millisecondsTimeout) in /Users/builder/data/lanes/3053/a94a03b5/source/mono/mcs/class/corlib/System.Threading/Monitor.cs:148
System.Threading.ManualResetEventSlim.Wait(int millisecondsTimeout,  cancellationToken) in /Users/builder/data/lanes/3053/a94a03b5/source/mono/external/referencesource/mscorlib/system/threading/ManualResetEventSlim.cs:669
System.Threading.Tasks.Task.SpinThenBlockingWait(int millisecondsTimeout,  cancellationToken) in /Users/builder/data/lanes/3053/a94a03b5/source/mono/external/referencesource/mscorlib/system/threading/Tasks/Task.cs:3356
System.Threading.Tasks.Task.InternalWait(int millisecondsTimeout,  cancellationToken) in /Users/builder/data/lanes/3053/a94a03b5/source/mono/external/referencesource/mscorlib/system/threading/Tasks/Task.cs:3295
System.Threading.Tasks.Task.Wait(int millisecondsTimeout,  cancellationToken) in /Users/builder/data/lanes/3053/a94a03b5/source/mono/external/referencesource/mscorlib/system/threading/Tasks/Task.cs:3201
System.Threading.Tasks.Task.Wait() in /Users/builder/data/lanes/3053/a94a03b5/source/mono/external/referencesource/mscorlib/system/threading/Tasks/Task.cs:3083
Xamarin.Forms.Platform.Android.FormsApplicationActivity.OnStateChanged() in C:\BuildAgent3\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\FormsApplicationActivity.cs:300
Xamarin.Forms.Platform.Android.FormsApplicationActivity.OnStop() in C:\BuildAgent3\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\FormsApplicationActivity.cs:215
Android.App.Activity.n_OnStop(System.IntPtr jnienv, System.IntPtr native__this) in /Users/builder/data/lanes/3053/a94a03b5/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.App.Activity.cs:4925
object.4ad35580-7d61-4ad1-b49f-72ab9482b358( arg0,  arg1) in 

It is a classical mistake with async code. You can read about it here: http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

Br, Kasper
Comment 1 Kasper 2016-04-08 12:09:36 UTC
As a workaround, I tried moving the shutdown code to the thread pool by overriding OnStop from FormsApplicationActivity like this:

	protected override void OnStop ()
	{
		// Workaround for https://bugzilla.xamarin.com/show_bug.cgi?id=40228 
		Task.Run(base.OnStop).Wait(); // run on background thread!
	}

Ugly, but it works (tm)
Comment 2 Kasper 2016-04-11 09:24:39 UTC
This is still an issue with XF 2.2.0.5-pre2
Comment 3 Chris King 2016-04-12 22:01:30 UTC
Thanks for the excellent bug report. We patched this bug in one place but not another. The patch has been submitted. Should be in the pre3 if we have time.
Comment 4 Kasper 2016-04-13 07:09:12 UTC
You are welcome. Looking forward to pre3.
Comment 5 Parmendra Kumar 2016-04-21 17:24:58 UTC
I have checked this issue with X.F 2.2.0-pre4 and its working fine.

Hence closing this issue.