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:
    88
    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:
    88
    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:
    10,680
    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:
    215
    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:
    215
    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…
     
    Last edited: Nov 30, 2020
  6. big_3

    big_3

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

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

    IElectric

    Joined:
    May 31, 2016
    Posts:
    215
    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.

    I have debugged my game on Visual Studio and when I close it I have many exceptions not handled, Cheers…
     
    Last edited: Nov 30, 2020
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

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

    IElectric

    Joined:
    May 31, 2016
    Posts:
    215
    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
     
    Last edited: Nov 30, 2020
  10. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    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:
    215
    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:
    10,680
    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:
    215
    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:
    10,680
    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:
    215
    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…
     
    Last edited: Nov 30, 2020
  16. IElectric

    IElectric

    Joined:
    May 31, 2016
    Posts:
    215
    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:
    10,680
  18. IElectric

    IElectric

    Joined:
    May 31, 2016
    Posts:
    215
    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:
    10,680
    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:
    215
    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:
    11
    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:
    10,680
    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:
    11
    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:
    10,680
    What have you tried?
     
  25. cyrusmasta

    cyrusmasta

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

    cyrusmasta

    Joined:
    Mar 20, 2019
    Posts:
    11
    ..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:
    10,680
    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:
    11
    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:
    10,680
    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.
     
  31. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    I have this problem in 2019.2. Application.Quit() works in the XBox build but not on Desktop. No errors, no crashing, but no quitting either - the game just proceeds like no Application.Quit() was called.
     
  32. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Is that on Windows Standalone or when targeting UWP?
     
  33. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    UWP. Standalone works.
     
  34. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
  35. IElectric

    IElectric

    Joined:
    May 31, 2016
    Posts:
    215
    Hello, still I have the same issue on Unity 2019.3.12f1, the UWP application quit but generate a failure on Windows Dev Center account, I don't know if it come from xbox live plugin, Cheers...
     
    DavidPolux likes this.
  36. DavidPolux

    DavidPolux

    Joined:
    Mar 2, 2018
    Posts:
    55
    Same here... with same configuration (Xbox Live Plugin)... :(
    And for me the application doesn't quit...
     
  37. DavidPolux

    DavidPolux

    Joined:
    Mar 2, 2018
    Posts:
    55
    Test on 2019-3-13f1 and same. I've used the trick by IElectric and the application ends when launch on vs2019 but when i read the debug log i see:
    "Exception levée à 0x00007FFE399AA799 dans MyGame.exe : exception Microsoft C++ : Il2CppExceptionWrapper à l'emplacement de mémoire 0x0000004CE18FEAC0."
     
  38. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Could we get bug reports for these issues?
     
  39. cuttinged

    cuttinged

    Joined:
    May 12, 2013
    Posts:
    65
    This worked for me.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Exit : MonoBehaviour
    6. {
    7.     // Quit the game.  Put on a button to enable on the button.
    8.     void Start()
    9.     {
    10.         Application.Quit();
    11.     }
    12.  
    13. }
    Make that script and put it on a button.
    Enable it on button press.
    Runs Application.Quit() when it is enabled.
    For some reason it works, not sure why.
    It only works in a build as Application.Quit only does.
     
  40. NILC

    NILC

    Joined:
    Aug 17, 2013
    Posts:
    9
    For those who are using the Xbox Live Unity Plugin, I have verified that there is a crash in the Microsoft.Xbox.Services.140.UWP.C.dll file upon application exit. It appears to be a thread created on the Native side of things is not being "gracefully stopped and/or exited" upon the application quitting. The most likely culprit is that the thread has some form of cross boundary memory pointer (Native to Managed) that is still referenced in the native side thread that is still running even after the GameAssembly.dll (managed) has unloaded. I have looked through the managed side of things (GameAssembly.dll/Unity Side) and there does not seem to be anything that could be invoked/called to fix the issue...even calling XboxLive.Instance.Dispose() right before calling Application.Quit() doesn't seem to fix the issue. The dispose function of the XboxLive singleton class ~should~ destroy all threads created within the ThreadPool.c file, but then again they also use the libHttpClient that creates threads as well...so it really requires a debug version of the Microsoft.Xbox.Services.140.UWP.C.dll file.

    For more information you can review the issue tracker submission I made here:
    140.C.dll crashes on app exit

    I was going to rebuild the libraries and see specifically where the crash was happening, but as I outlined in the above bug/issue report it is not very easy to get the repo to build. The repo appears to still be based around VS2015 and much older .NET and Windows SDK versions than most people are probably using and/or have installed.
    If anyone gets the repo building, please reply in the above GitHub issue (#312) and I will get debug versions of the libraries built and debug down to the offending code. As I mention in issue #312, I currently don't have further time to allocate towards hunting this bug down and since it doesn't seem to be "an issue" getting your title through certification ("Creators Club" only) I guess the only issue would be the Partner Center's database and associated VM resources being consumed if a Creators Club (UWP) Unity game/application using the Xbox Live Unity Plugin becomes really popular (i.e. could generate thousands to 100's of thousands of memory access violation reports per day).

    I guess this could be one of those "the squeaky wheel gets the oil" scenarios.

    Either case, if you are getting a memory access violation upon exit and it is coming from the Microsoft.Xbox.Services.140.UWP.C.dll then just as an FYI your title will make it through certification (Creators Club) with that issue. The offending DLL will be forced to unload and the thread will eventually be killed (but not before it throws the memory access violation).
     
    SoftwareGeezers and IElectric like this.
  41. eron82

    eron82

    Joined:
    Mar 10, 2017
    Posts:
    83
    The quit bug is still there.
     
  42. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Still waiting for a bug report.
     
    SoftwareGeezers likes this.
  43. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,255
    Am also seeing this from Application.Quit() with Xbox Live services in a UWP build on xbox one. Given this is happening on xbox from a Visual Studio build, what is the best method / steps to create a bug report for Unity?

    This is from a Unity 2020.1.6f1 build with IL2CPP.
     
  44. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Same way you report any Unity bug: https://unity3d.com/unity/qa/bug-reporting

    If you can reproduce it only with Xbox live build in the game, I'd try pausing the game when it hangs and look at what different threads are doing with Debug -> Windows -> Parallel Stacks. It's more than likely that IL2CPP is waiting for some threads to exit but they're stuck somewhere.
     
  45. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,255
    Ok. I did once see it fail here:
    Code (CSharp):
    1. GC_API int GC_CALL GC_register_my_thread(const struct GC_stack_base *sb)
    2. {
    3.   GC_thread me;
    4.   DWORD thread_id = GetCurrentThreadId();
    5.   DCL_LOCK_STATE;
    6.  
    7.   if (GC_need_to_lock == FALSE)
    8.     ABORT("Threads explicit registering is not previously enabled");
    9.  
    10.   /* We lock here, since we want to wait for an ongoing GC.     */
    11.   LOCK();  ***** ERROR HERE ******
    12.   me = GC_lookup_thread_inner(thread_id);
    13.   if (me == 0) {
    14. #   ifdef GC_PTHREADS
    15.       me = GC_register_my_thread_inner(sb, thread_id);
    16.       me -> flags |= DETACHED;
    17.           /* Treat as detached, since we do not need to worry about     */
    18.           /* pointer results.                                           */
    19. #   else
    20.       GC_register_my_thread_inner(sb, thread_id);
    21. #   endif
    22.     UNLOCK();
    23.     return GC_SUCCESS;
    24.   } else
    25. #   ifdef GC_PTHREADS
    26.       /* else */ if ((me -> flags & FINISHED) != 0) {
    27.         GC_record_stack_base(me, sb);
    28.         me -> flags &= ~FINISHED; /* but not DETACHED */
    29. #       ifdef THREAD_LOCAL_ALLOC
    30.           GC_init_thread_local((GC_tlfs)(&me->tlfs));
    31. #       endif
    32.         UNLOCK();
    33.         return GC_SUCCESS;
    34.       } else
    35. #   endif
    36.   /* else */ {
    37.     UNLOCK();
    38.     return GC_DUPLICATE;
    39.   }
    40. }
     
  46. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,255
    upload_2020-9-30_13-56-19.png
    I'm assuming I need to figure out what these threads are, right?
     
  47. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Yes. You first need to right click on "[External Code]" and click "Show External Code". Then, you need to load symbols for stuff that isn't resolved. It should give you a better view of what's going on.
     
  48. StarGamingNetwork

    StarGamingNetwork

    Joined:
    Feb 2, 2015
    Posts:
    16
    I can't remember the last version of Unity that Application.Quit(); actually worked on the Xbox UWP.

    I use:

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


    That actually works.