xamarin/Xamarin.Forms

[Bug] Application crashes on Android when power saving is enabled

Aberta

#8.382 aberto em 5 de nov. de 2019

 (13 comentários) (1 reação) (0 responsável)C# (1.926 forks)batch import
e/3 :clock3:help wantedp/Androidt/bug :bug:up-for-grabs

Métricas do repositório

Stars
 (5.644 estrelas)
Métricas de merge de PR
 (Nenhuma PRs mesclada em 30d)

Description

Description

For the last two weeks, I was trying to find a root cause for an issue that freezes my application on a splash screen on some devices. It was possible to reproduce only on Galaxy S8 and S9 and only on some devices.

After two weeks and countless hours, I finally managed to trace the root cause. This issue happens when battery power saving mode is enabled (on Galaxy it is specifically medium power consumption mode) and animation is invoked in a loop like this:

async Task RotateElement(VisualElement element)
{
    while (true)
    {
        await element.RotateTo(360, 1200);
        element.Rotation = 0;
    }
}

After some more investigation, I found out that it was broken since Xamarin.Forms 3.1.0.637273. Using Xamarin.Forms 3.1.0.583944 it works properly. Following this lead, I found out that in the broken version there was a fix for issue #4176 and I think it causes this behavior. Probably because of await operator which doesn't work anymore as expected (because of the fix which runs code on a Looper threads) and invoking two times RotateTo causes freeze.

Adding permission android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS does not help.

Steps to Reproduce

  1. Switch Android device to battery saving mode (medium power consumption option).
  2. Open the application and run animation.

Expected Behavior

The application should not crash/freeze. I think that "medium power consumption" should not turn off animations either.

Actual Behavior

Application freezes, stops responding and eventually crashes.

Basic Information

  • Version with issue: Xamarin.Forms 3.1.0.637273 and all newer
  • Last known good version: Xamarin.Forms 3.1.0.583944
  • IDE: Visual Studio for Mac
  • Android: Confirmed on 7, 9 and 10.
  • Affected Devices: confirmed on Samsung Galaxy S8/S9, One Plus, Google Pixel 3, but probably it happens on most Android devices.

Reproduction Link

https://github.com/wojciech-kulik/XamarinPowerSavingCrash

There is nothing special in application logs during a freeze.

Workaround

I found out that it is possible to provide some workaround by adding await Task.Delay(1) at the end of loop, but I don't know how reliable it is.

Guia do colaborador