Crash in Cesium for Unity during code reloading

Working with Cesium for Unity, I’ve been experiencing a recurring crash. It seems to happen when Unity’s reloading code - it always seems to happen when I switch back to the editor after modifying something in Visual Studio. I’ve attached the full stack trace below. If anyone has also experienced this crash and can help nail down some reproduction steps, that would help me get this into a GitHub issue!

=================================================================
	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 87 stack frames
0x00007ffe2f80fbd9 (CesiumForUnityNative-Editor) [xmemory:1375] std::_Container_base12::_Orphan_all_unlocked_v3 
0x00007ffe2f80fb6a (CesiumForUnityNative-Editor) [xmemory:1232] std::_Container_base12::_Orphan_all_locked_v3 
0x00007ffe2f80fb24 (CesiumForUnityNative-Editor) [xmemory:1391] std::_Container_base12::_Orphan_all 
0x00007ffe2f80fc9e (CesiumForUnityNative-Editor) [xstring:4840] std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Tidy_deallocate 
0x00007ffe2f80f44a (CesiumForUnityNative-Editor) [xstring:3141] std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string<char,std::char_traits<char>,std::allocator<char> > 
0x00007ffe2f88175d (CesiumForUnityNative-Editor) CesiumIonClient::Profile::~Profile
0x00007ffe2f883e48 (CesiumForUnityNative-Editor) CesiumIonClient::Profile::`scalar deleting destructor'
0x00007ffe2f886140 (CesiumForUnityNative-Editor) [optional:132] std::_Optional_destruct_base<CesiumIonClient::Profile,0>::reset 
0x00007ffe2f881d88 (CesiumForUnityNative-Editor) [optional:285] std::optional<CesiumIonClient::Profile>::operator= 
0x00007ffe2f85f76b (CesiumForUnityNative-Editor) [CesiumIonSessionImpl.cpp:384] <lambda_63f7f162df8f18193c46c11eabce83d6>::operator() 
0x00007ffe2fbbaf5a (CesiumForUnityNative-Editor) [CatchFunction.h:61] `<lambda_03da5fcbac684505a4eda96ad2eee3d5>::operator()'::`1'::catch$3 
0x00007ffeb7751080 (VCRUNTIME140_1D) 
0x00007ffeb7753aae (VCRUNTIME140_1D) _NLG_Return2
0x00007ffecb0d3c66 (ntdll) RtlCaptureContext2
0x00007ffe2f8654a6 (CesiumForUnityNative-Editor) [CatchFunction.h:57] <lambda_03da5fcbac684505a4eda96ad2eee3d5>::operator() 
0x00007ffe2f86713e (CesiumForUnityNative-Editor) [traits.h:108] <lambda_207b55f1b8552b847e27389ff3bd54d9>::operator() 
0x00007ffe2f872a4c (CesiumForUnityNative-Editor) [traits.h:103] async::detail::invoke_fake_void<<lambda_207b55f1b8552b847e27389ff3bd54d9>,void> 
0x00007ffe2f87285b (CesiumForUnityNative-Editor) [traits.h:108] async::detail::invoke_fake_void<<lambda_03da5fcbac684505a4eda96ad2eee3d5>,std::exception_ptr> 
0x00007ffe2f8663e9 (CesiumForUnityNative-Editor) [task_base.h:550] async::detail::continuation_exec_func<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::task<std::exception_ptr>,async::detail::fake_void,<lambda_03da5fcbac684505a4eda96ad2eee3d5>,std::integral_constant<bool,1>,0>::operator() 
0x00007ffe2f866549 (CesiumForUnityNative-Editor) [task_base.h:398] async::detail::task_func<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::detail::continuation_exec_func<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::task<std::exception_ptr>,async::detail::fake_void,<lambda_03da5fcbac684505a4eda96ad2eee3d5>,std::integral_constant<bool,1>,0>,async::detail::fake_void>::run 
0x00007ffe2f82be3f (CesiumForUnityNative-Editor) [scheduler.h:134] async::task_run_handle::run 
0x00007ffe2f82bfeb (CesiumForUnityNative-Editor) [ImmediateScheduler.h:23] CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>::schedule 
0x00007ffe2f81fd2c (CesiumForUnityNative-Editor) [scheduler.h:165] async::detail::schedule_task<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler> > 
0x00007ffe2f81fc2f (CesiumForUnityNative-Editor) [task_base.h:101] async::detail::task_base::run_continuation<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler> > 
0x00007ffe2f850f81 (CesiumForUnityNative-Editor) [task_base.h:134] async::detail::task_base::add_continuation<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler> > 
0x00007ffe2f876e85 (CesiumForUnityNative-Editor) [task.h:83] async::detail::basic_task<std::exception_ptr>::then_internal<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,<lambda_03da5fcbac684505a4eda96ad2eee3d5>,async::task<std::exception_ptr> > 
0x00007ffe2f875132 (CesiumForUnityNative-Editor) [task.h:263] async::task<std::exception_ptr>::then<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,<lambda_03da5fcbac684505a4eda96ad2eee3d5> > 
0x00007ffe2fbba188 (CesiumForUnityNative-Editor) [CatchFunction.h:64] `CesiumAsync::CesiumImpl::CatchFunction<<lambda_63f7f162df8f18193c46c11eabce83d6>,void,CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::task<void> &&>::operator()'::`1'::catch$3 
0x00007ffeb7751080 (VCRUNTIME140_1D) 
0x00007ffeb7753aae (VCRUNTIME140_1D) _NLG_Return2
0x00007ffecb0d3c66 (ntdll) RtlCaptureContext2
0x00007ffe2f8609c5 (CesiumForUnityNative-Editor) [CatchFunction.h:51] CesiumAsync::CesiumImpl::CatchFunction<<lambda_63f7f162df8f18193c46c11eabce83d6>,void,CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::task<void> &&>::operator() 
0x00007ffe2f864df0 (CesiumForUnityNative-Editor) [traits.h:108] <lambda_a0ea2b3692842674a47817ce9b9d41be>::operator() 
0x00007ffe2f873015 (CesiumForUnityNative-Editor) [traits.h:97] async::detail::invoke_fake_void<<lambda_a0ea2b3692842674a47817ce9b9d41be>,void> 
0x00007ffe2f8725ed (CesiumForUnityNative-Editor) [traits.h:108] async::detail::invoke_fake_void<CesiumAsync::CesiumImpl::CatchFunction<<lambda_63f7f162df8f18193c46c11eabce83d6>,void,CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::task<void> &&>,async::task<void> > 
0x00007ffe2f862eba (CesiumForUnityNative-Editor) [task_base.h:581] async::detail::continuation_exec_func<async::detail::inline_scheduler_impl,async::task<void>,async::detail::fake_void,CesiumAsync::CesiumImpl::CatchFunction<<lambda_63f7f162df8f18193c46c11eabce83d6>,void,CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::task<void> &&>,std::integral_constant<bool,0>,1>::operator() 
0x00007ffe2f862ff9 (CesiumForUnityNative-Editor) [task_base.h:398] async::detail::task_func<async::detail::inline_scheduler_impl,async::detail::continuation_exec_func<async::detail::inline_scheduler_impl,async::task<void>,async::detail::fake_void,CesiumAsync::CesiumImpl::CatchFunction<<lambda_63f7f162df8f18193c46c11eabce83d6>,void,CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::task<void> &&>,std::integral_constant<bool,0>,1>,async::detail::fake_void>::run 
0x00007ffe2f82be3f (CesiumForUnityNative-Editor) [scheduler.h:134] async::task_run_handle::run 
0x00007ffe2f82c164 (CesiumForUnityNative-Editor) [scheduler.h:171] async::detail::inline_scheduler_impl::schedule 
0x00007ffe2f81fdc7 (CesiumForUnityNative-Editor) [scheduler.h:165] async::detail::schedule_task<async::detail::inline_scheduler_impl> 
0x00007ffe2f81fc9f (CesiumForUnityNative-Editor) [task_base.h:101] async::detail::task_base::run_continuation<async::detail::inline_scheduler_impl> 
0x00007ffe2f86309e (CesiumForUnityNative-Editor) [task_base.h:417] async::detail::task_func<async::detail::inline_scheduler_impl,async::detail::continuation_exec_func<async::detail::inline_scheduler_impl,async::task<void>,async::detail::fake_void,CesiumAsync::CesiumImpl::CatchFunction<<lambda_63f7f162df8f18193c46c11eabce83d6>,void,CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::task<void> &&>,std::integral_constant<bool,0>,1>,async::detail::fake_void>::schedule 
0x00007ffe2f82600b (CesiumForUnityNative-Editor) [task_base.h:115] <lambda_748db4bbc179da6db048344469bf4b84>::operator() 
0x00007ffe2f81e534 (CesiumForUnityNative-Editor) [continuation_vector.h:221] async::detail::continuation_vector::flush_and_lock<<lambda_748db4bbc179da6db048344469bf4b84> > 
0x00007ffe2f82bea4 (CesiumForUnityNative-Editor) [task_base.h:117] async::detail::task_base::run_continuations 
0x00007ffe2f829e90 (CesiumForUnityNative-Editor) [task_base.h:279] async::detail::task_result<async::detail::fake_void>::cancel_base 
0x00007ffe2f862cc7 (CesiumForUnityNative-Editor) [task_base.h:410] async::detail::task_func<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::detail::continuation_exec_func<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::task<CesiumIonClient::Response<CesiumIonClient::Profile> >,async::detail::fake_void,<lambda_76165e152eb91127c2bab6b71e390739>,std::integral_constant<bool,1>,0>,async::detail::fake_void>::cancel 
0x00007ffe2f862ab3 (CesiumForUnityNative-Editor) [task_base.h:548] async::detail::continuation_exec_func<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::task<CesiumIonClient::Response<CesiumIonClient::Profile> >,async::detail::fake_void,<lambda_76165e152eb91127c2bab6b71e390739>,std::integral_constant<bool,1>,0>::operator() 
0x00007ffe2f862c79 (CesiumForUnityNative-Editor) [task_base.h:398] async::detail::task_func<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::detail::continuation_exec_func<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::task<CesiumIonClient::Response<CesiumIonClient::Profile> >,async::detail::fake_void,<lambda_76165e152eb91127c2bab6b71e390739>,std::integral_constant<bool,1>,0>,async::detail::fake_void>::run 
0x00007ffe2f82be3f (CesiumForUnityNative-Editor) [scheduler.h:134] async::task_run_handle::run 
0x00007ffe2f82bfeb (CesiumForUnityNative-Editor) [ImmediateScheduler.h:23] CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>::schedule 
0x00007ffe2f81fd2c (CesiumForUnityNative-Editor) [scheduler.h:165] async::detail::schedule_task<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler> > 
0x00007ffe2f81fc2f (CesiumForUnityNative-Editor) [task_base.h:101] async::detail::task_base::run_continuation<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler> > 
0x00007ffe2f862d1e (CesiumForUnityNative-Editor) [task_base.h:417] async::detail::task_func<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::detail::continuation_exec_func<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::task<CesiumIonClient::Response<CesiumIonClient::Profile> >,async::detail::fake_void,<lambda_76165e152eb91127c2bab6b71e390739>,std::integral_constant<bool,1>,0>,async::detail::fake_void>::schedule 
0x00007ffe2f82600b (CesiumForUnityNative-Editor) [task_base.h:115] <lambda_748db4bbc179da6db048344469bf4b84>::operator() 
0x00007ffe2f81e534 (CesiumForUnityNative-Editor) [continuation_vector.h:221] async::detail::continuation_vector::flush_and_lock<<lambda_748db4bbc179da6db048344469bf4b84> > 
0x00007ffe2f82bea4 (CesiumForUnityNative-Editor) [task_base.h:117] async::detail::task_base::run_continuations 
0x00007ffe2f9531c0 (CesiumForUnityNative-Editor) [task_base.h:279] async::detail::task_result<CesiumIonClient::Response<CesiumIonClient::Profile> >::cancel_base 
0x00007ffe2f8c82f9 (CesiumForUnityNative-Editor) [task_base.h:410] async::detail::task_func<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::detail::continuation_exec_func<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::task<std::shared_ptr<CesiumAsync::IAssetRequest> >,CesiumIonClient::Response<CesiumIonClient::Profile>,<lambda_5dedf0dce7adcf57697cfb093d21f5eb>,std::integral_constant<bool,1>,0>,CesiumIonClient::Response<CesiumIonClient::Profile> >::cancel 
0x00007ffe2f8c8088 (CesiumForUnityNative-Editor) [task_base.h:548] async::detail::continuation_exec_func<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::task<std::shared_ptr<CesiumAsync::IAssetRequest> >,CesiumIonClient::Response<CesiumIonClient::Profile>,<lambda_5dedf0dce7adcf57697cfb093d21f5eb>,std::integral_constant<bool,1>,0>::operator() 
0x00007ffe2f8c82ab (CesiumForUnityNative-Editor) [task_base.h:398] async::detail::task_func<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::detail::continuation_exec_func<CesiumAsync::CesiumImpl::ImmediateScheduler<CesiumAsync::CesiumImpl::QueuedScheduler>,async::task<std::shared_ptr<CesiumAsync::IAssetRequest> >,CesiumIonClient::Response<CesiumIonClient::Profile>,<lambda_5dedf0dce7adcf57697cfb093d21f5eb>,std::integral_constant<bool,1>,0>,CesiumIonClient::Response<CesiumIonClient::Profile> >::run 
0x00007ffe2f82be3f (CesiumForUnityNative-Editor) [scheduler.h:134] async::task_run_handle::run 
0x00007ffe2f9ab6a1 (CesiumForUnityNative-Editor) [scheduler.cpp:216] async::fifo_scheduler::try_run_one_task 
0x00007ffe2f9ab704 (CesiumForUnityNative-Editor) [scheduler.cpp:222] async::fifo_scheduler::run_all_tasks 
0x00007ffe2f97c12b (CesiumForUnityNative-Editor) [QueuedScheduler.cpp:11] CesiumAsync::CesiumImpl::QueuedScheduler::dispatchQueuedContinuations 
0x00007ffe2f97dbaf (CesiumForUnityNative-Editor) [AsyncSystem.cpp:16] CesiumAsync::AsyncSystem::dispatchMainThreadTasks 
0x00007ffe2f85ca3c (CesiumForUnityNative-Editor) [CesiumIonSessionImpl.cpp:305] CesiumForUnityNative::CesiumIonSessionImpl::Tick 
0x00007ffe2f834c64 (CesiumForUnityNative-Editor) [CesiumIonSession.cpp:298] DotNet_CesiumForUnity_CesiumIonSession_Tick 
0x000002a226a4360f (Mono JIT Code) (wrapper managed-to-native) CesiumForUnity.CesiumIonSession:DotNet_CesiumForUnity_CesiumIonSession_Tick (intptr,CesiumForUnity.CesiumIonSession/ImplementationHandle)
0x000002a226a433f3 (Mono JIT Code) [CesiumIonSession-generated.cs:233] CesiumForUnity.CesiumIonSession:Tick () 
0x000002a226a42f5b (Mono JIT Code) [CesiumEditorUtility.cs:26] CesiumForUnity.CesiumEditorUtility:UpdateIonSession () 
0x000002a226a41248 (Mono JIT Code) UnityEditor.EditorApplication:Internal_CallUpdateFunctions ()
0x000002a22678d175 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void (object,intptr,intptr,intptr)
0x00007ffdb58afe24 (mono-2.0-bdwgc) [mini-runtime.c:3445] mono_jit_runtime_invoke 
0x00007ffdb57ee764 (mono-2.0-bdwgc) [object.c:3066] do_runtime_invoke 
0x00007ffdb57ee8fc (mono-2.0-bdwgc) [object.c:3113] mono_runtime_invoke 
0x00007ff708ce7614 (Unity) scripting_method_invoke
0x00007ff708cc6eb4 (Unity) ScriptingInvocation::Invoke
0x00007ff708cc1ac5 (Unity) ScriptingInvocation::Invoke<void>
0x00007ff708e17b6b (Unity) Scripting::UnityEditor::EditorApplicationProxy::Internal_CallUpdateFunctions
0x00007ff709740662 (Unity) SceneTracker::Update
0x00007ff7098f0e1a (Unity) Application::TickTimer
0x00007ff709d3ff5a (Unity) MainMessageLoop
0x00007ff709d4480b (Unity) WinMain
0x00007ff70b0d20ce (Unity) __scrt_common_main_seh
0x00007ffeca0c257d (KERNEL32) BaseThreadInitThunk
0x00007ffecb08aa58 (ntdll) RtlUserThreadStart

Do you happen to know if you were modifying one of the ConfigureReinterop.cs files when this happened?

Reinterop creates a sort of hash of all of the interop functions so that it can detect when the C# code and the C++ code are out of sync. When they’re out of sync, attempting to call into C++ will just throw a managed exception, because proceeding would almost certainly crash hard. Stack corruption is common, for example. But it’s possible the hash isn’t capturing every aspect of the generated interop code, and so it’s failing to detect certain kinds of changes.

I’m not sure if that’s what you’re seeing here, though. It’s just a guess.

If I recall correctly, this was while editing other C# files - CesiumFlyToController being the one that comes to mind. I’ve noticed this crash seems to be proceeded by messages of A Native Collection has not been disposed, resulting in a memory leak. Enable Full StackTraces to get more details., which might be related.

I was able to find a more reliable way of producing this bug (use the editor script to set project build settings in this draft PR in cesium-unity-samples), so I was able to get it to happen in-debugger. The problem happens on line 266 of CesiumIonSessionImpl.cpp, the line this->_connection = std::move(*pConnection); The move assignment is causing a read access violation trying to move the _pSchedulers shared_ptr in AsyncSystem.

I believe the reason this bug happens in cesium-unity-samples is that the script will run when play mode begins, it will end play mode to make its changes when Ok is pressed, and then it will re-enter play mode once those changes are made. Somewhere in the steps of compiling, playing, compiling, and playing again, CesiumIonSessionImpl is trying to Resume a session that hasn’t been correctly initialized yet, causing a crash.