bugsnag / bugsnag-unity

Automatic error reporting for Unity games
https://docs.bugsnag.com/platforms/unity
MIT License
90 stars 31 forks source link

[Android] Calling Bugsnag.Notify multiple times per frame will crash the app #129

Closed tayl0r closed 5 years ago

tayl0r commented 5 years ago

Description

Call Bugsnag.Notify multiple times per frame will crash the app

Issue

JNI ERROR (app bug): local reference table overflow (max=512) at BugsnagUnity.NativeInterface.DictionaryFromJavaMap (intptr)

It looks like this DictionaryFromJavaMap() method in NativeInterface.cs isn't deleting all the local references it is creating. So if you run it enough in the same frame, it will crash due to this local reference table overflow error.

I'm no Android/Java programmer, but I think all of these calls to CallStringMethod and CallObjectMethod need to have their result deleted via DeleteLocalRef.

I think the crash only happens on older/slower Android devices such as the Nexus 5X running Android OS 7.0. I don't think the Android OS matters though, I have seen the crash also on an Xiaomi Mi 8 running Android OS 9.0 device. But haven't seen the crash on any Android OS 8.0 devices.

Here is a subset of the devices that we've seen the crash on: P9, P9 Lite, Tab A, Intex Aqua Craze, Galaxy Grand 2, S4, Xperia Z2, Le Pro 3, Redmi 4 Pro, Redmi Note 4, Vibe K5. Almost all of those were running Android OS 7.0.

Environment

Example code snippet

To reproduce, you can just open this Unity project, scene "main", then build to your Android device. It will run 20 Bugsnag.Notify() calls which will crash the app. The project is completely empty except for the Bugsnag plugin plus my test code. https://www.dropbox.com/s/43ssyb4kjrmksb4/TestBugsnag.zip?dl=0

kattrali commented 5 years ago

Thanks for the report and reproduction case, @tayl0r. I initially had some trouble reproducing this but see the issue (at least on older devices).

The object references are being cleaned up correctly, however it seems that we'll need to set more strict limits for the amount of metadata/breadcrumbs that can be attached to reports for Unity projects as deeply recursive structures are consuming too many references at once.

kattrali commented 5 years ago

Update: There is one ref which isn't cleaned up correctly, fixing it reduces the likelihood of hitting this case significantly. Making a patch. ✍️