Android ANR caused by long synchronous operation in UnityMain thread

I just want to bring Unity’s attention to a problem of Android ANRs which leads to bad Vitals and game developers loosing their organic traffic and money!

By definition Android ANR is caused by an application blocking main thread.

The Unity thread UnityMain is not a main thread so blocking UnityMain must not lead to an ANR. In theory. But it does! Because of Unity architecture where main and UnityMain are synchronized when UnityPlayerActivity changes its state like in Activity.onPause. Thus minimizing application when doing a heavy operation in UnityMain causes an ANR.

I think this is so technically subtle topic that the majority of independent developers suffer ANRs penalties imposed by Google without even realizing that!

What do you think?

Would you not be better posting this on the dedicated Android forum?

I can move your post if you wish.

1 Like

Yes please!

1 Like

Doing a heavy operation in UnityMain is already a problem on it’s own. There is a grace period when sending app to background on Android, so it’s have been stuck for quite some time. If you do something in Unity main thread that takes few seconds, your game freezes that time.

If you find a lockup when backgrounding a performant application, that’s a bug on our side. But if it’s your code doing something that takes seconds, it’s your design issue, cause main thread is supposed to be spinning and producing frames at intervals measured in tens of milliseconds.

I admit that our code taking seconds is our design issue. But…

To clarify things - I am talking about our game’s loading which occurs at startup and which does not imply high framerates or smooth animations or smooth user’s input handling. And while everything is happing in the Unity Main thread which is not the process’s main thread it must not influence the backgrounding and allowed grace period and thus must not lead to an ANR.

We are working on our side to chop the loading progress into chunks. I just wanted to let you know that Unity can help solving this kind of ANRs from their side looking into how main and Unity Main threads are synchronized.

2 Likes

For those of you encountering Android loading ANRs and having trouble diagnosing them, definitely look for points in your code where you are using Resources.Load on an asset on the main thread.

This wound up being our problem. When we switched over to Resources.LoadAsync or using threads / jobs the ANR’s decreased.

For the unity folks: It’s really hard to track down where these are coming from, or even tell if they are a game engine problem or a problem with our code. Not sure if it has to do with the way the game starts, or the way Android gets backgrounded, but it took a long time to diagnose this issue.