playgameservices / play-games-plugin-for-unity

Google Play Games plugin for Unity
Other
3.46k stars 965 forks source link

Is GooglePlayGames.PlayGamesPlatform.LoadScores thread safe? #2862

Closed ecv80 closed 4 years ago

ecv80 commented 4 years ago
03-29 13:24:20.453 15529 15915 E CRASH   : signal 6 (SIGABRT), code -6 (?), fault addr --------
03-29 13:24:20.453 15529 15915 E CRASH   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
03-29 13:24:20.453 15529 15915 E CRASH   : Build type 'Release', Scripting Backend 'mono', CPU 'armeabi-v7a'
03-29 13:24:20.453 15529 15915 E CRASH   : Build fingerprint: 'xiaomi/ugg/ugg:7.1.2/N2G47H/V9.6.2.0.NDKMIFD:user/release-keys'
03-29 13:24:20.453 15529 15915 E CRASH   : Revision: '0'
03-29 13:24:20.454 15529 15915 E CRASH   : pid: 15529, tid: 15915, name: Thread-172  >>> com.CENSORED <<<
03-29 13:24:20.454 15529 15915 E CRASH   :     r0 00000000  r1 00003e2b  r2 00000006  r3 00000008
03-29 13:24:20.454 15529 15915 E CRASH   :     r4 cacfa978  r5 00000006  r6 cacfa920  r7 0000010c
03-29 13:24:20.454 15529 15915 E CRASH   :     r8 cacf9eb0  r9 f1741538  sl 0000000a  fp b6210a00
03-29 13:24:20.454 15529 15915 E CRASH   :     ip 0000000d  sp cacf9dd0  lr f20aa2d7  pc f20acb58  cpsr ae287f00
03-29 13:24:20.454 15529 15915 E CRASH   : 
03-29 13:24:20.454 15529 15915 E CRASH   : backtrace:
03-29 13:24:20.457 15529 15915 E CRASH   :  #00  pc 00049b58  /system/lib/libc.so (tgkill+12)
03-29 13:24:20.457 15529 15915 E CRASH   :  #01  pc 000472d3  /system/lib/libc.so (pthread_kill+34)
03-29 13:24:20.457 15529 15915 E CRASH   :  #02  pc 0001d575  /system/lib/libc.so (raise+10)
03-29 13:24:20.457 15529 15915 E CRASH   :  #03  pc 000190c1  /system/lib/libc.so (__libc_android_abort+34)
03-29 13:24:20.457 15529 15915 E CRASH   :  #04  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.457 15529 15915 E CRASH   :  #05  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.457 15529 15915 E CRASH   :  #06  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.457 15529 15915 E CRASH   :  #07  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.457 15529 15915 E CRASH   :  #08  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.457 15529 15915 E CRASH   :  #09  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.457 15529 15915 E CRASH   :  #10  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #11  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #12  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #13  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #14  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #15  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #16  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #17  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #18  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #19  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #20  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #21  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #22  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #23  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #24  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #25  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #26  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #27  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #28  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #29  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #30  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #31  pc 00017124  /system/lib/libc.so (abort+4)
03-29 13:24:20.458 15529 15915 E CRASH   :  #32  il 00000037  at (wrapper managed-to-native) UnityEngine.AndroidJNI.NewGlobalRef (intptr) <0x00037>
03-29 13:24:20.458 15529 15915 E CRASH   :  #33  il 0000003f  at UnityEngine.GlobalJavaObjectRef..ctor (intptr) [0x0001a] in <5aceb54cdbcc492fbae9ab05aab81814>:0
03-29 13:24:20.458 15529 15915 E CRASH   :  #34  il 0000005f  at UnityEngine.AndroidJavaClass._AndroidJavaClass (string) [0x00022] in <5aceb54cdbcc492fbae9ab05aab81814>:0
03-29 13:24:20.458 15529 15915 E CRASH   :  #35  il 00000023  at UnityEngine.AndroidJavaClass..ctor (string) [0x00007] in <5aceb54cdbcc492fbae9ab05aab81814>:0
03-29 13:24:20.458 15529 15915 E CRASH   :  #36  il 0000002f  at GooglePlayGames.Android.AndroidHelperFragment.GetActivity () [0x00000] in <9831954e212c4144a916d9fae4f9545f>:0
03-29 13:24:20.458 15529 15915 E CRASH   :  #37  il 00000033  at GooglePlayGames.Android.AndroidClient.getLeaderboardsClient () [0x00000] in <9831954e212c4144a916d9fae4f9545f>:0
03-29 13:24:20.458 15529 15915 E CRASH   :  #38  il 00000083  at GooglePlayGames.Android.AndroidClient.LoadScores (string,GooglePlayGames.BasicApi.LeaderboardStart,int,GooglePlayGames.BasicApi.LeaderboardCollection,GooglePlayGames.BasicApi.LeaderboardTimeSpan,System.Action`1<GooglePlayGames.BasicApi.LeaderboardScoreData>) [0x0002c] in <9831954e212c4144a916d9fae4f9545f>:0
03-29 13:24:20.458 15529 15915 E CRASH   :  #39  il 000000c3  at GooglePlayGames.PlayGamesPlatform.LoadScores (string,GooglePlayGames.BasicApi.LeaderboardStart,int,GooglePlayGames.BasicApi.LeaderboardCollection,GooglePlayGames.BasicApi.LeaderboardTimeSpan,System.Action`1<GooglePlayGames.BasicApi.LeaderboardScoreData>) [0x00022] in <9831954e212c4144a916d9fae4f9545f>:0
03-29 13:24:20.459 15529 15915 E CRASH   :  #40  il 000000ef  at ScoresPanel.<populateWorldScores>b__46_0 () [0x00020] in <9831954e212c4144a916d9fae4f9545f>:0
03-29 13:24:20.459 15529 15915 E CRASH   :  #41  il 000000ab  at System.Threading.ThreadHelper.ThreadStart_Context (object) [0x00014] in <a8ed250850854b439cedc18931a314fe>:0
03-29 13:24:20.459 15529 15915 E CRASH   :  #42  il 0000019f  at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) [0x00071] in <a8ed250850854b439cedc18931a314fe>:0
03-29 13:24:20.459 15529 15915 E CRASH   :  #43  il 0000002b  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) [0x00000] in <a8ed250850854b439cedc18931a314fe>:0
03-29 13:24:20.459 15529 15915 E CRASH   :  #44  il 0000004f  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object) [0x0002b] in <a8ed250850854b439cedc18931a314fe>:0
03-29 13:24:20.459 15529 15915 E CRASH   :  #45  il 0000002f  at System.Threading.ThreadHelper.ThreadStart () [0x00008] in <a8ed250850854b439cedc18931a314fe>:0
03-29 13:24:20.459 15529 15915 E CRASH   :  #46  il 000000bb  at (wrapper runtime-invoke) object.runtime_invoke_void__this__ (object,intptr,intptr,intptr) <0x000bb>
public void populateWorldScores () {

        worldHighScores.text="";

        new Thread(()=>{

        int currentScorePos=0;
        int entries=0;              

        PlayGamesPlatform.Instance.LoadScores(
            GPGSIds.leaderboard_high_scores,
            LeaderboardStart.PlayerCentered,
            50,
            LeaderboardCollection.Public,
            LeaderboardTimeSpan.AllTime,
            (data) =>
            {

                entries=data.Scores.Length;
                ScoreEntry[] scoreEntries= new ScoreEntry[entries];

                string[] userIDs=new string[entries];

                for (int i=0; i<scoreEntries.Length; i++) {
                    userIDs[i]=scoreEntries[i].userID=data.Scores[i].userID;
                    scoreEntries[i].score=(int)data.Scores[i].value;
                    scoreEntries[i].date=data.Scores[i].date;
                    scoreEntries[i].rank=data.Scores[i].rank;
                    scoreEntries[i].metadata=((PlayGamesScore)data.Scores[i]).metaData;
                }

                Social.LoadUsers(userIDs, profs => {

                    for (int i=0; i<profs.Length; i++)
                        for (int j=0; j<scoreEntries.Length; j++)
                            if (profs[i].id==scoreEntries[j].userID)
                                scoreEntries[j].userName=profs[i].userName;

                    MainThreadPool.Done done=new MainThreadPool.Done();
                    MainThreadPool.add(ref done, ()=>{
                        worldHighScores.text="";

                        for (int i=0; i<scoreEntries.Length; i++) {
                            if (Encryption.DecryptScoreTag(scoreEntries[i].metadata)!=scoreEntries[i].score)
                                continue;

                            if (scoreEntries[i].userID==data.PlayerScore.userID) {
                                currentScorePos=i;
                                worldHighScores.text+="<b><color=#AAFF00FF>";
                            }

                            worldHighScores.text+=scoreEntries[i].rank+". "
                                +scoreEntries[i].userName+" "
                                +scoreEntries[i].score+" "
                                +scoreEntries[i].date.ToString("MMM/dd/yy")
                                +"\n";

                            if (scoreEntries[i].userID==data.PlayerScore.userID)
                                worldHighScores.text+="</color></b>";

                        }

                        //Focus on user top score
                        worldHighScoresSR.horizontalNormalizedPosition=.5f;
                        worldHighScoresSR.verticalNormalizedPosition=1f-currentScorePos/Mathf.Max(1f, entries-1);

                    });

                });

            });

        }).Start();
    }
olehkuznetsov commented 4 years ago

There is no need to call it not on main thread as it is asynchronous. You can use thread to handle data returned by it.

ecv80 commented 4 years ago

Thanks.

I guess the answer is no. Calling PlayGamesPlatform.Instance.LoadScores() and Social.LoadUsers() within, causes a crash regardless of whether I comment out my own MainThreadPool code or not (which works beautifully otherwise). Calling them from the game thread, on the other hand, caused no crash.

I see now how it doesn't make sense to call them from other thread than the game thread, as their callbacks will be run in the game thread regardless of the thread they were called from. Thanks for clearing that up. The reason they were being called from a new thread in the first place, is probably because of this statement which I find confusing and took as a warning: "I Unity : [Play Games Plugin DLL] 03/29/20 13:14:50 +02:00 DEBUG: Invoking user callback on game thread". Unfortunately Google returns a myriad of unrelated hits as to what this may actually mean.