Repository: godot
Issue: Running LOD generation under tsan uncovers lock-order-inversion
Labels: bug,confirmed,topic:thirdparty,topic:import,topic:3d

### Tested versions

Reproducible at head


### System information

TSAN build

### Issue description

```
WARNING: ThreadSanitizer: lock-order-inversion (potential deadlock) (pid=2203)
  Cycle in lock order graph: M68815185789847888 (0x000000000000) => M68533710813269808 (0x000000000000) => M68815185789847888

  Mutex M68533710813269808 acquired here while holding mutex M68815185789847888 in main thread:
    #0 pthread_mutex_lock <null> (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x4e73576)
    #1 embree::MutexSys::lock() /home/runner/work/godot/godot/thirdparty/embree/common/sys/mutex.cpp:44:9 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x6b199cd)
    #2 embree::Lock<embree::MutexSys>::lock() /home/runner/work/godot/godot/thirdparty/embree/kernels/common/../../common/tasking/../sys/mutex.h:98:71 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x6b22460)
    #3 embree::Scene::commit(bool) /home/runner/work/godot/godot/thirdparty/embree/kernels/common/scene.cpp:777:19 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x6b22460)
    #4 rtcCommitScene /home/runner/work/godot/godot/thirdparty/embree/kernels/common/rtcore.cpp:236:12 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x6b12e0c)
    #5 StaticRaycasterEmbree::commit() /home/runner/work/godot/godot/modules/raycast/static_raycaster_embree.cpp:94:2 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x6d202d9)
    #6 ImporterMesh::generate_lods(float, float, Array) /home/runner/work/godot/godot/scene/resources/importer_mesh.cpp:444:15 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0xa3655ec)
    #7 ResourceImporterScene::_generate_meshes(Node*, Dictionary const&, bool, bool, ResourceImporterScene::LightBakeMode, float, Vector<unsigned char> const&, Vector<Vector<unsigned char> >&) /home/runner/work/godot/godot/editor/import/resource_importer_scene.cpp:2155:33 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x84b1233)
    #8 ResourceImporterScene::_generate_meshes(Node*, Dictionary const&, bool, bool, ResourceImporterScene::LightBakeMode, float, Vector<unsigned char> const&, Vector<Vector<unsigned char> >&) /home/runner/work/godot/godot/editor/import/resource_importer_scene.cpp:2222:3 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x84b1b6c)
    #9 ResourceImporterScene::import(String const&, String const&, HashMap<StringName, Variant, HashMapHasherDefault, HashMapComparatorDefault<StringName>, DefaultTypedAllocator<HashMapElement<StringName, Variant> > > const&, List<String, DefaultAllocator>*, List<String, DefaultAllocator>*, Variant*) /home/runner/work/godot/godot/editor/import/resource_importer_scene.cpp:2605:10 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x84b3e13)
    #10 TestGltfExtras::_gltf_export_then_import(Node*, String) /home/runner/work/godot/godot/./modules/gltf/tests/test_gltf_extras.h:89:22 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x5418b46)
    #11 TestGltfExtras::DOCTEST_ANON_FUNC_7535() /home/runner/work/godot/godot/./modules/gltf/tests/test_gltf_extras.h:124:17 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x5419957)
    #12 doctest::Context::run() /home/runner/work/godot/godot/./thirdparty/doctest/doctest.h:7007:21 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x573eb20)
    #13 test_main(int, char**) /home/runner/work/godot/godot/tests/test_main.cpp:202:22 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x541f36b)
    #14 Main::test_entrypoint(int, char**, bool&) /home/runner/work/godot/godot/main/main.cpp:726:17 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x4fecde3)
    #15 main /home/runner/work/godot/godot/platform/linuxbsd/godot_linuxbsd.cpp:56:2 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x4ee7148)

    Hint: use TSAN_OPTIONS=second_deadlock_stack=1 to get more informative warning message

  Mutex M68815185789847888 acquired here while holding mutex M68533710813269808 in main thread:
    #0 pthread_mutex_lock <null> (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x4e73576)
    #1 embree::MutexSys::lock() /home/runner/work/godot/godot/thirdparty/embree/common/sys/mutex.cpp:44:9 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x6b199cd)
    #2 embree::Lock<embree::MutexSys>::Lock(embree::MutexSys&) /home/runner/work/godot/godot/thirdparty/embree/kernels/common/../../common/tasking/../sys/mutex.h:95:62 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x6b26395)
    #3 embree::Scene::commit(bool)::$_3::operator()() const /home/runner/work/godot/godot/thirdparty/embree/kernels/common/scene.cpp:795:67 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x6b26395)
    #4 embree::TaskScheduler::ClosureTaskFunction<embree::Scene::commit(bool)::$_3>::execute() /home/runner/work/godot/godot/thirdparty/embree/kernels/common/../../common/tasking/taskschedulerinternal.h:47:24 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x6b26395)
    #5 embree::TaskScheduler::Task::run_internal(embree::TaskScheduler::Thread&) /home/runner/work/godot/godot/thirdparty/embree/common/tasking/taskschedulerinternal.cpp:54:20 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x6b6168f)
    #6 embree::TaskScheduler::TaskQueue::execute_local_internal(embree::TaskScheduler::Thread&, embree::TaskScheduler::Task*) /home/runner/work/godot/godot/thirdparty/embree/common/tasking/taskschedulerinternal.cpp:87:20 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x6b61c15)
    #7 embree::TaskScheduler::TaskQueue::execute_local(embree::TaskScheduler::Thread&, embree::TaskScheduler::Task*) /home/runner/work/godot/godot/thirdparty/embree/common/tasking/taskschedulerinternal.cpp:104:12 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x6b61c15)
    #8 void embree::TaskScheduler::spawn_root<embree::Scene::commit(bool)::$_3>(embree::Scene::commit(bool)::$_3 const&, unsigned long, bool) /home/runner/work/godot/godot/thirdparty/embree/kernels/common/../../common/tasking/taskschedulerinternal.h:280:27 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x6b22a1c)
    #9 embree::Scene::commit(bool) /home/runner/work/godot/godot/thirdparty/embree/kernels/common/scene.cpp:795:18 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x6b22a1c)
    #10 rtcCommitScene /home/runner/work/godot/godot/thirdparty/embree/kernels/common/rtcore.cpp:236:12 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x6b12e0c)
    #11 StaticRaycasterEmbree::commit() /home/runner/work/godot/godot/modules/raycast/static_raycaster_embree.cpp:94:2 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x6d202d9)
    #12 ImporterMesh::generate_lods(float, float, Array) /home/runner/work/godot/godot/scene/resources/importer_mesh.cpp:444:15 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0xa3655ec)
    #13 ResourceImporterScene::_generate_meshes(Node*, Dictionary const&, bool, bool, ResourceImporterScene::LightBakeMode, float, Vector<unsigned char> const&, Vector<Vector<unsigned char> >&) /home/runner/work/godot/godot/editor/import/resource_importer_scene.cpp:2155:33 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x84b1233)
    #14 ResourceImporterScene::_generate_meshes(Node*, Dictionary const&, bool, bool, ResourceImporterScene::LightBakeMode, float, Vector<unsigned char> const&, Vector<Vector<unsigned char> >&) /home/runner/work/godot/godot/editor/import/resource_importer_scene.cpp:2222:3 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x84b1b6c)
    #15 ResourceImporterScene::import(String const&, String const&, HashMap<StringName, Variant, HashMapHasherDefault, HashMapComparatorDefault<StringName>, DefaultTypedAllocator<HashMapElement<StringName, Variant> > > const&, List<String, DefaultAllocator>*, List<String, DefaultAllocator>*, Variant*) /home/runner/work/godot/godot/editor/import/resource_importer_scene.cpp:2605:10 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x84b3e13)
    #16 TestGltfExtras::_gltf_export_then_import(Node*, String) /home/runner/work/godot/godot/./modules/gltf/tests/test_gltf_extras.h:89:22 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x5418b46)
    #17 TestGltfExtras::DOCTEST_ANON_FUNC_7535() /home/runner/work/godot/godot/./modules/gltf/tests/test_gltf_extras.h:124:17 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x5419957)
    #18 doctest::Context::run() /home/runner/work/godot/godot/./thirdparty/doctest/doctest.h:7007:21 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x573eb20)
    #19 test_main(int, char**) /home/runner/work/godot/godot/tests/test_main.cpp:202:22 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x541f36b)
    #20 Main::test_entrypoint(int, char**, bool&) /home/runner/work/godot/godot/main/main.cpp:726:17 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x4fecde3)
    #21 main /home/runner/work/godot/godot/platform/linuxbsd/godot_linuxbsd.cpp:56:2 (godot.linuxbsd.editor.dev.x86_64.llvm.san+0x4ee7148)

SUMMARY: ThreadSanitizer: lock-order-inversion (potential deadlock) (/home/runner/work/godot/godot/bin/godot.linuxbsd.editor.dev.x86_64.llvm.san+0x4e73576) in pthread_mutex_lock
```

### Steps to reproduce

- Run LOD generation under TSAN - can be easily reproduced by running tests with tsan and by flipping 
`options["meshes/generate_lods"]` to `true` from https://github.com/godotengine/godot/pull/86183

e.g. https://github.com/godotengine/godot/actions/runs/7249176559/job/19746727561

### Minimal reproduction project (MRP)

n/a