-
Notifications
You must be signed in to change notification settings - Fork 433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Main thread errors and freezing on Android - Unity 2018.1.7f1, Firebase SDK 5.1.1, IL2CPP, .NET 4.x, Windows 10 x64 #181
Comments
Hi Fanus, When you are using Task.ContinueWith(continuationFunc) in Unity with .NET 4.x, the continuation function will NOT be called from the main thread. This behavior is different from Unity with .NET 3.x, where continuation function is always called from the main thread. Therefore, if you are accessing any GameObject or its properties in the continuation function, ex. PrepareGame(), Unity will definitely complain about "xxxx can only be called from the main thread", and this may result in unexpected behavior. One quick solution to this problem, with minimum code change, is to use Task.ContinueWith(continuationFunc, scheduler) with TaskScheduler.FromCurrentSynchronizationContext(), assuming your first ContinueWith() is called from the main thread. If not, you will need to store a reference of the scheduler from the main thread. Another option for you is to use await instead of ContinueWith(). This also makes your code prettier. What I am curious about is the configuration when you first started getting freeze on Android. May I know the Unity version, Firebase SDK version and .NET version you were using? It would be awesome if you have any additional information! Thank you and hope this information helps. Shawn |
Hi, I just wanted to add my personal experience with Unity + Firebase. When I switched to .NET 4.x, I had those errors about accessing GameObjects from the tasks, so I queued events instead and it got rid of those problems. But I still had problems with the realtime database. Some queries would never get a response or would have their permission denied even if the user was successfully logged in. I switched back to .NET 3.x thinking it would help, but it didn't. Then I read the other issue where someone suggested using the REST API, and it worked for me. Felix |
@chkuang-g thanks for the suggestions. I'm going to try both and see how it far I get.
I assumed the freezes were due to the SDK not using System.Threading.Task and therefore unable to deal with asynchronous calls. Sorry I cannot give you more info (adb logcat, etc.) because downgrading my Unity again just isn't feasible at the moment. @felixsoum thank you for sharing. Fortunately I haven't had any permission issues, but I did experience the responses never coming back. I fixed that by checking if the Results are null. See this line from the snippet above: I haven't considered the REST API yet as I was rooting for whatever option can get me to the finish line as soon as possible. However, I am planning on a iOS version of the game and now thinking that the REST API might be the best option in the long run... I'm not familiar with the API so if you can share some references I would very much appreciate it. Fanus |
So far when using the TaskScheduler, none of the firebase calls work and logcat reports the following crash error as soon as
Any suggestions? The editor works perfectly fine, this is on Android device only. |
Thanks for your tips about the response. Here are some useful links: So far working with these has solved my database issues. Good luck! Felix |
I managed to get everything working by changing my Scripting Backend from IL2CPP back to Mono and keeping everything else on .NET 4. This is the final configuration: Services that are tested and working: Firebase Authentication, Firebase Realtime Database, and Google Play Games. Now the question is what is going on with IL2CPP and how can I make it work. Its benefits seem promising. |
@felixsoum @Fanus IL2CPP supports issue seems concerning to me. I'll see if I can reproduce this locally. Thank you for the information! |
Not sure if it is related but I am having a similar freeze issue here: #182 |
@chkuang-g I don't think it is a thread issue anymore since after I added the taskScheduler, all the Is it possible that the device I'm using maybe doesn't support IL2CPP? |
I've been trying to reproduce this using Unity TestApp but unable to make Android build crash. I was trying to use exactly the same environment.
Also, I was using Pixel phone with Android 8.0.0 for testing. I don't think the device matter but which phone where you using? Also, could you try to simplify your code to And you can try the TestApp as well. If it crash, could you provide more log data? Thank you! |
I'm seeing this issue too with the latest version of firebase SDK (5.2.0). Funnily enough, it was not happening one week ago as I had an older version in my app, however a few days ago gradle started being unable to download the dependencies it needed for the old version, so I was forced to update. I didn't change anything in my project, it's a Unity 2017.1 project. I suggest firebase Unity has regressed badly so allowing gradle to get the last working version (I had 4.4.2 working perfectly) into app builds would be a great step while you guys fix this extremely disruptive bug. |
Hi @chrisfairc, Did you see the crash or the freeze in Android build as @Fanus described? Thank you! |
I am seeing an issue on an Android build, tested it on 2 devices, a samsung phone and a tablet. I'm trying different things but still no joy. I'm sure your test app works fine, but I will try it once I exhaust my test options. I am also seeing issues with failing to start the app. I think I will make my own issue once I have it figured out a bit better. |
@chrisfairc Thank you. Please provide more information about the issue you experienced either in this thread or in a different one. This would help us to pinpoint the problem. @Fanus Are you still experiencing crashs? If so, could you provide the steps to reproduce it? Thank you |
Since I fixed mine by changing the Scripting Backend from IL2CPP back to Mono, I'm not experiencing any issues. I'm afraid I'm launching my project soon so changing a working setup would be too much of a risk for me. The best I can do is confirm that the setup from this comment is still working 100% and the android build will soon be released. Perhaps when tweaking performance after release I will revisit this issue so we can put it to rest. @chkuang-g too also revert back to you regarding this comment. I'm testing it on a archaic Samsung Galaxy Alpha. However the new setup has been working fine on a few other Android devices. Also note that the freezes only happened randomly. I can only speculate that it freezes when the process jumps to a new CPU thread. Perhaps because the phone is under heavy load or the asynchronous function puts heavy strain on the device and isn't just a empty function. |
@chkuang-g I found that it was not actually a problem with firebase unity, sorry I was too hasty earlier. There is an issue with unity cloud accessing the play service resolver dependencies for firebase, so I disabled the auto resolution, and committed the dependencies, now it's working fine again. I am still seeing freezes when building locally but that doesnt appear to be a firebase issue now, there may be an issue with the way my local android sdk is configured. At least I can get a good build from unity cloud now so I'm happy enough. Thanks and sorry for hijaking the thread, my issue seemed relevant at first. |
@Fanus Good luck with your launching! Please do let us know if you have more information when tweaking performance. And thank you for patiently providing all the information. I've created an internal task to investigate the potential issue related to IL2CPP. It would take time since we do not have a simple way to reproduce it. @chrisfairc No problem. Glad you resolved it. :) |
Hello, We are seeing an issue that seems similar to what others are describing. Our setup is as follows:
This setup was working without issue until we added an Android plugin developed in-house that launches a Service. Once we did that, our application freezes when we call:
I believe this is the first explicit point of contact with Firebase (at least Auth) in our application. Note that at this point we have not started the newly added Service, only called a couple static methods on the plugin. *I should mention we attempted upgrading to Firebase 5.2, but ran into issues where Firebase stopped functioning (repeatedly throwing the error Any help resolving this issue would be greatly appreciated! -Peter |
@pkennedy-tripp This seems to be a different issue since it is .NET 3.5 and Firebase 4.5.2. Could you start a different issue on Github? Also, what are the other static methods on the plugin you called before this? Thank you. |
@Fanus did you resolve your issue? |
~ removed, since problem was not related to this issue. I am sorry. |
@E-Reg Also, do you still see the same issue with the latest Unity plugin? |
I am seeing an issue on pixel, Android P.
|
It occurs in armeabi-v7a
|
@xiabodan & @KenshiroIchiyama both crash logs attached do not contain any stack traces that lead to Firebase. What makes you believe that Firebase is the root cause of either crash? |
I also have this issue yesterday with netstandard API compat profile and need to switch to 4.x. Doesn't use IL2Cpp though I don't really know it caused from firebase or anything else. But just google and found this issue. Mine also has the same similar |
Ah no, it's not related to compat profile. Both can be crash and throw this kind of exception
|
Seeing this as well with IL2CPP |
Unity Upgrade
I've recently upgrade from Unity 2017.3.0f3 because I started getting freezes on Android when I call anything from the Firebase SDK. The version of Unity I have now is 2018.1.7f1. I am also using the latest firebase sdk 5.1.1.
.NET Upgrade and IL2CPP
Still not getting the game running on android (editor works fine), I switched to IL2CPP and .NET 4.x. As far as I can tell, these technologies are compatible with the Firebase SDK as long as use the correct Parse SDK dlls. See bottom of this page.
Editor Errors
Now, I'm getting the following error when I try run the game in the editor (Android is also still broken):
get_isActiveAndEnabled can only be called from the main thread.
The stack trace points to a simple line of code that, if I delete it, it just throws a similar error somewhere else (code that always worked prior to Firebase):
get_gameObject can only be called from the main thread.
The Issue
Finally, I tested disabling my firebase sdk. This resulted in the game running smoothly in the editor and on an Android device.
The errors also make it quite clear that Unity can't handle code called from multiple threads. I've also seen this from other issues people are experiencing. I also believe this is why the Android game on Unity 2017 didn't exactly crash, it just froze (probably switching to a different thread once Firebase SDK is used). And it only happened occasionally, not every time (perhaps there aren't always other threads available). I'm also not sure whether this issue started with Firebase Authentication or Realtime Database (I'm assuming both because they both use System.Threading).
Anyway...
The text was updated successfully, but these errors were encountered: