xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.64k stars 1.88k forks source link

Asynchronous tasks only succeed when tapping the display on Chromebooks #15399

Open richirisu opened 2 years ago

richirisu commented 2 years ago

Android application type

Classic Xamarin.Android (MonoAndroid12.0, etc.)

Affected platform version

VSMac 8.10

Description

First of all, this issue only occurs on Chromebooks running Xamarin Android applications. It does not occur on Android phones when running the same application.

When using asynchronous tasks, it happens every so often that the tasks will not return, unless the user actively taps or clicks the screen. Depending on the amount of asynchronous tasks this may require multiple taps to finish.

Although more cumbersome, besides tapping the screen, it is also possible to force the tasks to eventually return by minimizing and maximizing the application.

Since tapping the screen multiple times leads to the tasks returning eventually, a typical deadlock situation can be eliminated.

Sample application to demonstrate the issue: richirisu/ChromebookTest

Steps to Reproduce

In order to reproduce this issue, you have to have some sort of nested tasks. Normally, the nested tasks should be executed one by one. However, the application keeps stopping the execution of the nested tasks from time to time. Strangely, by tapping on the display, the application continues to execute the next few nested tasks until it stops again.

await Task.Run(async () -> {
   await Task.Run(...);
   await Task.Run(...);
   ...
});

This works for both Task.Run() and TaskScheduler.StartNew() alike. (Most likely because the task scheduler internally makes a call to Task.Run() itself.)

await MyTaskScheduler.StartNew(async () -> {
   await Task.Run(...);
   await Task.Run(...);
   ...
});

Sample application to demonstrate the issue: richirisu/ChromebookTest

Did you find any workaround?

No response

Relevant log output

No response

richirisu commented 2 years ago

Debugging Android apps (incl. Xamarin.Android apps) on a Chromebook: https://chromeos.dev/en/android-environment/deploying-apps

Also, in order to be able to deploy the app to the Chromebook, you need to set EmbedAssembliesIntoApk = True in the Xamarin.Android project file. https://stackoverflow.com/questions/69327294/xamarin-deploy-chromebook-error-xa0131-run-as-tool-has-been-disabled https://github.com/xamarin/xamarin-android/blob/main/Documentation/guides/messages/xa0131.md

grendello commented 2 years ago

This is a weird issue which might have to do either with the Mono runtime (@steveisok) or Xamarin.Forms (@Redth), I don't see anything that could point to Xamarin.Android here. However, the suspicious part (to me) is this code:

async void Button_Clicked(object sender, EventArgs eventArgs)

async void methods are inherently dangerous if the execution environment isn't configured properly. In this instance it would have to be either the application itself or Xamarin.Forms, thus the redirection.

richirisu commented 2 years ago

In addition, I also noticed that you can trigger the succession of the nested tasks by not only tapping the screen but also by performing a circle movement with the mouse cursor over the button so that the button's highlight state constantly changes. So maybe you are right and this issue is more related to Xamarin.Forms somehow.