NativeEngine contains thread-unsafe logic that causes crashes during fast refresh BabylonReactNative scenarios
We have logic in NativeEngine that hands around an allocator that is not valid when NativeEngine is disposed: https://github.com/BabylonJS/BabylonNative/blob/720b2dc2da8393aad4893a1661dd71bef1aa6fdc/Plugins/NativeEngine/Source/NativeEngine.cpp#L981
This logic does not observe cancellation requests and can generate crashes during fast refresh/NativeEngine teardown
Chatted with @ryantrem offline. Seems like the following is going on:
"this seems related to the conversation we just had.... NativeEngine is owned by the JS side but tries to do work on a thread other than the JS thread, and so NativeEngine can be destructed while those async operations are executing. m_allocator, m_cancellationToken, and m_runtimeScheduler are all not safe to use in this way. Either lifetimes need to be extended or some of this stuff needs to move out of NativeEngine to something longer lived."
This can happen, for example, when an async LoadTexture is in progress and the NativeEngine is disposed (or maybe when bgfx is shutdown via Graphics::DisableRendering).
Affected by https://github.com/microsoft/arcana.cpp/pull/36.