CesiumGS / cesium-unity

Bringing the 3D geospatial ecosystem to Unity
https://cesium.com/platform/cesium-for-unity/
Apache License 2.0
357 stars 84 forks source link

Unity crashes when reloading domains #525

Open Mercury-Leo opened 2 days ago

Mercury-Leo commented 2 days ago

Sometimes, Unity will crash completely while reloading domains.

To Reproduce

  1. Install Cesium.
  2. Cause domain reloads.
  3. Crashes.

Setup

Additional context

This seems to be a problem with CesiumIonSession that isn't handled while reloading domains. It causes a native crash on the C++ side in Cesium which causes Unity to crash.

This is the crash report:


    Native Crash Reporting
=================================================================
Got a UNKNOWN while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

=================================================================
    Managed Stacktrace:
=================================================================
      at <unknown> <0xffffffff>
      at CesiumForUnity.CesiumIonSession:DotNet_CesiumForUnity_CesiumIonSession_Tick <0x0017e>
      at CesiumForUnity.CesiumIonSession:Tick <0x001a2>
      at CesiumForUnity.CesiumEditorUtility:UpdateIonSession <0x0010a>
      at UnityEditor.EditorApplication:Internal_CallUpdateFunctions <0x00327>
      at System.Object:runtime_invoke_void <0x00184>
=================================================================
Received signal SIGSEGV
Obtained 48 stack frames
0x00007ff82590e53f (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff8ee5749c6 (ntdll) RtlCaptureContext2
0x00007ff8253ae8e3 (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff8253a378e (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff8253a3e7d (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff8253a033d (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff825384fb5 (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff82537ebd6 (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff82537eb40 (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff825398065 (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff8253a77df (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff8259c4c2a (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff82591b700 (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff82590e45a (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff8ee5749c6 (ntdll) RtlCaptureContext2
0x00007ff8253afc75 (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff8253a32cb (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff8253a30ed (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff82539fabf (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff82537ec62 (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff82537eb90 (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff82539fb8c (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff825383ddf (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff82537e4ea (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff82539f8ff (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff82539f83f (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff825433188 (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff825433258 (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x00007ff825389d6a (CesiumForUnityNative-Editor) DotNet_CesiumForUnity_CesiumIonSession_Disconnect
0x000002911e92539f (Mono JIT Code) (wrapper managed-to-native) CesiumForUnity.CesiumIonSession:DotNet_CesiumForUnity_CesiumIonSession_Tick (intptr,CesiumForUnity.CesiumIonSession/ImplementationHandle)
0x000002911e925183 (Mono JIT Code) CesiumForUnity.CesiumIonSession:Tick () (at ./Library/PackageCache/com.cesium.unity@1.13.1/Editor/generated/Reinterop.RoslynSourceGenerator/CesiumIonSession-generated.cs:582)
0x000002911e924ceb (Mono JIT Code) CesiumForUnity.CesiumEditorUtility:UpdateIonSession () (at ./Library/PackageCache/com.cesium.unity@1.13.1/Editor/CesiumEditorUtility.cs:26)
0x000002911e82ae08 (Mono JIT Code) UnityEditor.EditorApplication:Internal_CallUpdateFunctions ()
0x000002911ed714b5 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void (object,intptr,intptr,intptr)
0x00007ff826314b7e (mono-2.0-bdwgc) mono_jit_runtime_invoke (at C:/build/output/Unity-Technologies/mono/mono/mini/mini-runtime.c:3445)
0x00007ff82624d204 (mono-2.0-bdwgc) do_runtime_invoke (at C:/build/output/Unity-Technologies/mono/mono/metadata/object.c:3068)
0x00007ff82624d37c (mono-2.0-bdwgc) mono_runtime_invoke (at C:/build/output/Unity-Technologies/mono/mono/metadata/object.c:3115)
0x00007ff796491bd4 (Unity) scripting_method_invoke
0x00007ff79646fac4 (Unity) ScriptingInvocation::Invoke
0x00007ff79646a6d5 (Unity) ScriptingInvocation::Invoke<void>
0x00007ff7965b71cb (Unity) Scripting::UnityEditor::EditorApplicationProxy::Internal_CallUpdateFunctions
0x00007ff796fba75a (Unity) SceneTracker::Update
0x00007ff7970df6e9 (Unity) Application::TickTimer
0x00007ff79755699a (Unity) MainMessageLoop
0x00007ff79755c230 (Unity) WinMain
0x00007ff79893bb6e (Unity) __scrt_common_main_seh
0x00007ff8ecb2257d (KERNEL32) BaseThreadInitThunk
0x00007ff8ee52af08 (ntdll) RtlUserThreadStart ```
kring commented 1 day ago

Thanks for writing this issue @Mercury-Leo. I've occassionally seen this as well. I believe it's caused by the appdomain reloading while async network requests are in progress within the native code. When the requests complete, the native code finds that the managed world it knew no longer exists (and has taken important native objects with it!).

I did a big piece of the work to fix this in #503. But to truly fix it, we also need to modify the code that starts the network requests to keep the native objects alive (via reference counting) while they're in progress. So thank you for the reminder to do that.