Search Unity

[UWP] IL2CPP: Application.Quit does not work

Discussion in 'Windows' started by big_3, Aug 27, 2018.

  1. big_3

    big_3

    Joined:
    Apr 20, 2016
    Posts:
    45
    Hi,

    we are using Unity 2017.4.10f1.

    Application.Quit does not seem to work when I compile for UWP (IL2CPP). Is this a known issue?

    Thanks
    Jus
     
  2. big_3

    big_3

    Joined:
    Apr 20, 2016
    Posts:
    45
    update:

    I found issue 1065223.

    We are also using Unity IAP. When will this bug be fixed?
     
  3. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,999
    The bug in question is fixed internally and we're in the process of backporting it to 2017.4 and 2018.2. Unfortunately I can't give you any dates.

    Do you suffer the same symptoms of the game freezing when trying to quit?

    Fortunately, the bug was located in the public source code, so if you need the fix sooner, you should be able to workaround it with this patch diff:

    Code (csharp):
    1. diff --git a/libil2cpp/vm/ScopedThreadAttacher.h b/libil2cpp/vm/ScopedThreadAttacher.h
    2. --- a/libil2cpp/vm/ScopedThreadAttacher.h
    3. +++ b/libil2cpp/vm/ScopedThreadAttacher.h
    4. @@ -11,30 +11,20 @@ namespace vm
    5.      {
    6.      public:
    7.          ScopedThreadAttacher()
    8. -#if !IL2CPP_HAS_NATIVE_THREAD_CLEANUP
    9.              : m_AttachedThread(NULL)
    10. -#endif
    11.          {
    12. -#if !IL2CPP_HAS_NATIVE_THREAD_CLEANUP
    13.              if (il2cpp::vm::Thread::Current() == NULL)
    14.                  m_AttachedThread = il2cpp::vm::Thread::Attach(il2cpp::vm::Domain::GetRoot());
    15. -#else
    16. -            il2cpp::vm::Thread::Attach(il2cpp::vm::Domain::GetRoot());
    17. -#endif
    18.          }
    19.  
    20.          ~ScopedThreadAttacher()
    21.          {
    22. -#if !IL2CPP_HAS_NATIVE_THREAD_CLEANUP
    23.              if (m_AttachedThread != NULL)
    24.                  il2cpp::vm::Thread::Detach(m_AttachedThread);
    25. -#endif
    26.          }
    27.  
    28.      private:
    29. -#if !IL2CPP_HAS_NATIVE_THREAD_CLEANUP
    30. -        Il2CppThread * m_AttachedThread;
    31. -#endif
    32. +        Il2CppThread* m_AttachedThread;
    33.      };
    34.  }
    35.  }
     
  4. IElectric

    IElectric

    Joined:
    May 31, 2016
    Posts:
    137
    Hello I have the same issue on my game, I'm using Unity 2018.2.5f1 + Visual Studio Community 2017 15.8.1 + xbox-live-unity-plugin 1804 QFE 1 Release.

    If I build my game as .NET scripting backend all works properly, the game login in the xbox live service and when I call Application.Quit() the game window close correctly. But, if I build my game as IL2CPP scripting backend when I call Application.Quit() the game window freeze and I can't stop the game.

    I have observed that if in the IL2CPP version I doesn't login to the xbox live service the game window close correctly when I call Application.Quit().

    I don't know exactly where is the problem, can you please explain in more detail how to patch the bug ? I have tried to modify "ScopedThreadAttacher.h" but in my case doesn't works, thank you a lot, Best regards...
     
  5. IElectric

    IElectric

    Joined:
    May 31, 2016
    Posts:
    137
    Hello, I'm sorry, I have misunderstood the patch, if I modify the source file "ScopedThreadAttacher.h" as suggested the game is closed when I call Application.Quit(), thank you very much, Best regards…

    Code (CSharp):
    1. namespace il2cpp
    2. {
    3. namespace vm
    4. {
    5.     class ScopedThreadAttacher
    6.     {
    7.     public:
    8.         ScopedThreadAttacher()
    9. //#if !IL2CPP_HAS_NATIVE_THREAD_CLEANUP
    10.             : m_AttachedThread(NULL)
    11. //#endif
    12.         {
    13. //#if !IL2CPP_HAS_NATIVE_THREAD_CLEANUP
    14.             if (il2cpp::vm::Thread::Current() == NULL)
    15.                 m_AttachedThread = il2cpp::vm::Thread::Attach(il2cpp::vm::Domain::GetRoot());
    16. //#else
    17. //            il2cpp::vm::Thread::Attach(il2cpp::vm::Domain::GetRoot());
    18. //#endif
    19.         }
    20.  
    21.         ~ScopedThreadAttacher()
    22.         {
    23. //#if !IL2CPP_HAS_NATIVE_THREAD_CLEANUP
    24.             if (m_AttachedThread != NULL)
    25.                 il2cpp::vm::Thread::Detach(m_AttachedThread);
    26. //#endif
    27.         }
    28.  
    29.     private:
    30. //#if !IL2CPP_HAS_NATIVE_THREAD_CLEANUP
    31.         Il2CppThread * m_AttachedThread;
    32. //#endif
    33.     };
    34. }
    35. }
     
    Last edited: Aug 28, 2018
  6. big_3

    big_3

    Joined:
    Apr 20, 2016
    Posts:
    45
    Thanks for your answer.

    Will wait for the official patch. It's not urgent.
     
  7. IElectric

    IElectric

    Joined:
    May 31, 2016
    Posts:
    137
    Hello, wait please, seems that there are some exceptions not handled when the game are closing, my dashboard on Windows dev center signal memory failures each time the game is closed.

    fails.png

    I have debugged my game on Visual Studio and when I close it I have many exceptions not handled, Cheers…

    exception.png
     
    Last edited: Sep 8, 2018
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,999
    What's the callstack for that?
     
  9. IElectric

    IElectric

    Joined:
    May 31, 2016
    Posts:
    137
    Hello, there are many exceptions when the game is closed, I hope that the next image is useful.

    I'm using "xbox live plugin" on my game for Microsoft Store publish and the exception occours when Application.Quit() is called and the player is signed in, if the player isn't signed in the game close properly when Application.Quit() is called (also without ScopedThreadAttacher modification), Cheers...

    Unity version: Unity 2018.2.6f1
    Unity Xbox Live plugin version: 1804 QFE 1 Release
    Visual Studio 2017 version: 15.8.2

    Immagine.png
     
    Last edited: Sep 9, 2018
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,999
    One thing to check: are you unsubscribing from Xbox Live events before you shutdown? It's possible they're calling back into you as you shut down the engine, and since C# land is shut down, you crash.

    If that's not it, could you do "DEBUG" -> "Save dump as..." and send me the resulting file?
     
  11. IElectric

    IElectric

    Joined:
    May 31, 2016
    Posts:
    137
    Hello, I have tried different solution but nothing, the game raise exception when Application.Quit() is called, I have also tried to unsubscribing from Xbox Live events before quit the game.

    The dump file is very large, 1 GB...

    I'm stuck because I can't update my game on Microsoft Store in this way, if I build the game as .NET Scripting backend all works OK but sometimes when the player signin the game shutdown and if I build the game as .IL2CPP Scripting backend I can't properly quit the game.

    In the IL2CPP version on Windows 10 if the game is closed by X on title bar the game exit properly.

    I don't know if I'm the only one with this issue, thank you a lot for your assistance, Cheers…

    PS:
    The current version of the game on Microsoft Store was built with Unity 2018.1.6f1 and Unity Xbox Live plugin 1711 Release as .NET scripting backend.

    https://www.microsoft.com/store/apps/9P5GJQXZL1BQ
     
  12. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,999
    Use 7-zip to compress the dump file. They usually compress really well. I can't really help you without a dump file unfortunately :(
     
  13. IElectric

    IElectric

    Joined:
    May 31, 2016
    Posts:
    137
    Don't worry, I will wait the next version of Unity, in the mean time I will investigate on this issue, thank you, Cheers...
     
  14. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,999
    You could also report a bug with a repro if that was easier. But yeah, it's unlikely it will be fixed without us figuring out what's going on.
     
  15. IElectric

    IElectric

    Joined:
    May 31, 2016
    Posts:
    137
    Hello, I have found a workaround, I don't know if it is a good practice, I have added a WinRT call after Application.Quit() method and the game close without exception.

    Leave ScopedThreadAttacher.h as it is after build IL2CPP, I need to do much test before confirm that it's works properly, at the moment I haven't found any error on Windows Dev Center, Cheers…

    quit.png
     
    Last edited: Sep 7, 2018
  16. IElectric

    IElectric

    Joined:
    May 31, 2016
    Posts:
    137
    Hello, I have seen that is fixed in the latest release Unity 2018.2.7f1, Best regards…

    (1065223) - IL2CPP : Fixed deadlock on exit when calling back to managed code on a thread that doesn't exit before player shutdown.
     
  17. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,999
  18. IElectric

    IElectric

    Joined:
    May 31, 2016
    Posts:
    137
    Hello, I'm sorry to signal that with the latest version of Unity 2018.2.7f1 if I run my game in DEBUG build Visual Studio generate exception when the game is closed by Application.Quit() method, in MASTER mode the game close but Windows Dev Center show a memory failure as explained before.

    Without signin on xbox-live-plugin my game close poperly in DEBUG and MASTER build.

    I'm using

    Unity version: Unity 2018.2.7f1
    Unity Xbox Live plugin version: 1804 QFE 1 Release
    Visual Studio 2017 version: 15.8.3

    Best regards…
     
    Last edited: Sep 9, 2018
  19. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,999
    Yeah, we'll need a bug report for that. It's a separate issue not related to original deadlock.
     
  20. IElectric

    IElectric

    Joined:
    May 31, 2016
    Posts:
    137
    Hello, I have used the workaround Exit() after Quit() but I don't know if it's a good practice, Cheers...

    Application.Quit();

    #if ENABLE_WINMD_SUPPORT
    Windows.ApplicationModel.Core.CoreApplication.Exit();
    #endif

    PS: I'm not sure but I think that there is something wrong when UWP game works with xbox live plugin.
     
  21. cyrusmasta

    cyrusmasta

    Joined:
    Mar 20, 2019
    Posts:
    7
    I still have no solution for the problem that Application.Quit() is not working with il2cpp.
    I have tried to change ScopedThreadAttacher.h but its not working. If i do this, Unity can not compile it.

    So i really need a solution for this!! I'm using Unity 2019.2.1f1
    Problem still occurs on the latest Unity build 2019.3.0f3 (and i also don't like to switch!! i rest at 2.1f1).

    Thats really annoying!! Users can't close my game!! So please fix it finally or give me a solution!! I can't belive it's really hard to do this!
     
  22. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,999
    Scoped thread attacher was fixed a year ago. You don't need to modify it at all.

    All known issues in Unity have already been fixed a long time ago. What's likely happening in your game is that you have an active background thread that wouldn't shut down. When using Application.Quit(), Unity will wait for all other threads to exit before shutting down (otherwise it would crash), and if at least one thread doesn't exit, you deadlock. I suggest using a debugger to figure out why your thread isn't shutting down.
     
  23. cyrusmasta

    cyrusmasta

    Joined:
    Mar 20, 2019
    Posts:
    7
    Builds with scripting backend "Mono" dont have this problem.. App is closing perfectly. Build the same with il2cpp and the app is not closing. I have tried a lot. Nothing was helpful. :/
     
  24. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,999
    What have you tried?
     
  25. cyrusmasta

    cyrusmasta

    Joined:
    Mar 20, 2019
    Posts:
    7
    System.Diagnostics.Process.GetCurrentProcess().Kill();
    Application.Unload();
    Application.Quit();
     
  26. cyrusmasta

    cyrusmasta

    Joined:
    Mar 20, 2019
    Posts:
    7
    ..also build with 2019.2.1f1 and 2019.3.0f3 and modify the library
     
  27. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,999
    Did you try attaching the debugger to see where your game gets stuck? If not, try that.
     
  28. cyrusmasta

    cyrusmasta

    Joined:
    Mar 20, 2019
    Posts:
    7
    Problems only occurs on il2cpp (on mono all is fine):

    Unity Editor:
    Application.Quit() takes no effect
    System.Diagnostics.Process.GetCurrentProcess().Kill(); kills Unity completly.

    App build: App is not closing (not crashing as in the editor)

    please can someone help me to fix this issue? App is available on Playstore and i really need to fix it!

    Debugger shows me:
    Der Thread 0x3ba1cbe0 hat mit Code 0 (0x0) geendet.
    Der Thread 0x3ba1cab0 hat mit Code 0 (0x0) geendet.
    Der Thread 0x3ba1cd10 hat mit Code 0 (0x0) geendet.
    Der Thread 0x3ba1c850 hat mit Code 0 (0x0) geendet.
    Das Programm "Unity" wurde mit Code 0 (0x0) beendet.

    (Thread 0x3ba.. closed with code 0 (0x0)
    "Unity" closed with code 0 (0x0))
     
  29. DavidMann1987

    DavidMann1987

    Joined:
    Sep 3, 2018
    Posts:
    7
    Having the same problem here, but for publishing on Android. The game works perfectly when using mono as the scripting backend. As soon as I switch to il2cpp application.quit no longer works. Everything else works perfectly. Seems unity has some troubleshooting to do because a lot of users I have seen are having this same issue. Please offer a solution, Unity.
     
  30. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    6,999
    If you believe this is a defect in Unity, please submit a bug report... It's very unlikely it will be looked at without one.

    I don't know much about Android but my advice to attach a debugger and figure out which thread fails to exit still stands. That is probably the fastest path for you to unblock yourself. If you need help figuring out how to use the debugger, you can ask on the Android forum.
     
unityunity