godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
89.56k stars 20.58k forks source link

SoftBody3D prevents scene from loading if Physics set to Run on Separate Thread #86790

Open christopwner opened 9 months ago

christopwner commented 9 months ago

Tested versions

v4.2.1.stable.official [b09f793f5]

System information

Godot v4.2.1.stable - Ubuntu 22.04.3 LTS 22.04 - X11 - Vulkan (Forward+) - dedicated NVIDIA GeForce RTX 3080 Ti (nvidia; 535.129.03) - AMD Ryzen Threadripper 2950X 16-Core Processor (32 Threads)

Issue description

As title suggests, having softbody3d with separate physics thread causes godot to crash. The MRP crashes quickly, however in the project I originally discovered this, the run would stall forcing me to kill the proc. Console output from MRP captured below:

christopwner@desktop:/opt/godot$  
Godot Engine v4.2.1.stable.official.b09f793f5 - https://godotengine.org
Vulkan API 1.3.242 - Forward+ - Using Vulkan Device #0: NVIDIA - NVIDIA GeForce RTX 3080 Ti

ERROR: Caller thread can't call this function in this node (/root). Use call_deferred() or call_thread_group() instead.
   at: propagate_notification (scene/main/node.cpp:2236)

================================================================
handle_crash: Program crashed with signal 11
Engine version: Godot Engine v4.2.1.stable.official (b09f793f564a6c95dc76acc654b390e68441bd01)
Dumping the backtrace. Please include this when reporting the bug to the project developer.
[1] /lib/x86_64-linux-gnu/libc.so.6(+0x42520) [0x7f70db909520] (??:0)
[2] /opt/godot/Godot_v4.2.1-stable_linux.x86_64() [0x26113b7] (??:0)
[3] /opt/godot/Godot_v4.2.1-stable_linux.x86_64() [0x2fd0741] (??:0)
[4] /opt/godot/Godot_v4.2.1-stable_linux.x86_64() [0x34af01b] (??:0)
[5] /opt/godot/Godot_v4.2.1-stable_linux.x86_64() [0x373cb85] (??:0)
[6] /opt/godot/Godot_v4.2.1-stable_linux.x86_64() [0x42b87a3] (??:0)
[7] /lib/x86_64-linux-gnu/libc.so.6(+0x94ac3) [0x7f70db95bac3] (??:0)
[8] /lib/x86_64-linux-gnu/libc.so.6(+0x126660) [0x7f70db9ed660] (??:0)
-- END OF BACKTRACE --
================================================================

Steps to reproduce

Minimal reproduction project (MRP)

softbody3d-separate-thread.zip

Calinou commented 8 months ago

If you can compile from source (see Testing pull requests), it might be worth checking whether https://github.com/godotengine/godot/pull/72491 resolves this crash.

christopwner commented 8 months ago

@Calinou no, unfortunately doesnt fix for me

christopwner@desktop:~/godot/engine/bin$  
Godot Engine v4.1.beta.custom_build.f66b38155 - https://godotengine.org
Vulkan API 1.3.242 - Forward+ - Using Vulkan Device #0: NVIDIA - NVIDIA GeForce RTX 3080 Ti

ERROR: Caller thread can't call this function in this node (/root). Use call_deferred() or call_thread_group() instead.
   at: propagate_notification (scene/main/node.cpp:2201)

================================================================
handle_crash: Program crashed with signal 11
Engine version: Godot Engine v4.1.beta.custom_build (f66b38155d790edaf18a1de198d6e9a540717700)
Dumping the backtrace. Please include this when reporting the bug to the project developer.
[1] /lib/x86_64-linux-gnu/libc.so.6(+0x42520) [0x7f828fc4c520] (??:0)
[2] /home/christopwner/godot/engine/bin/godot.linuxbsd.editor.x86_64(+0x37a956b) [0x556189a2156b] (??:0)
[3] /home/christopwner/godot/engine/bin/godot.linuxbsd.editor.x86_64(+0x44adbbe) [0x55618a725bbe] (??:0)
[4] /home/christopwner/godot/engine/bin/godot.linuxbsd.editor.x86_64(+0x49ea3bc) [0x55618ac623bc] (??:0)
[5] /home/christopwner/godot/engine/bin/godot.linuxbsd.editor.x86_64(+0x4df10b5) [0x55618b0690b5] (??:0)
[6] /home/christopwner/godot/engine/bin/godot.linuxbsd.editor.x86_64(+0x5728944) [0x55618b9a0944] (??:0)
[7] /lib/x86_64-linux-gnu/libc.so.6(+0x94ac3) [0x7f828fc9eac3] (??:0)
[8] /lib/x86_64-linux-gnu/libc.so.6(+0x126660) [0x7f828fd30660] (??:0)
-- END OF BACKTRACE --
================================================================
ERROR: Pages in use exist at exit in PagedAllocator: 6Thread
   at: ~PagedAllocator (./core/templates/paged_allocator.h:170)
ERROR: Pages in use exist at exit in PagedAllocator: N16WorkerThreadPool4TaskE
   at: ~PagedAllocator (./core/templates/paged_allocator.h:170)
LeaoLuciano commented 8 months ago

Apparently, the problem is here:

https://github.com/godotengine/godot/blob/179dfdc8d78b5bd5377dd115af026df58308bdaf/scene/3d/soft_body_3d.cpp#L425-L427

A race condition happens and SoftBodyRenderingServerHandler::close() (L. 427) is called before SoftBodyRenderingServerHandler::set_vertex() (called inside L. 426) so there is a segfault because SoftBodyRenderingServerHandler::write_buffer is nullptr when set_vertex() attempts to write on it.

https://github.com/godotengine/godot/blob/179dfdc8d78b5bd5377dd115af026df58308bdaf/scene/3d/soft_body_3d.cpp#L76-L78

https://github.com/godotengine/godot/blob/179dfdc8d78b5bd5377dd115af026df58308bdaf/scene/3d/soft_body_3d.cpp#L84-L86

Backtrace:

================================================================
handle_crash: Program crashed with signal 11
Engine version: Godot Engine v4.3.dev.custom_build (179dfdc8d78b5bd5377dd115af026df58308bdaf)
Dumping the backtrace. Please include this when reporting the bug to the project developer.
[1] /usr/lib/libc.so.6(+0x3e710) [0x7ff2e4f14710] (??:0)
[2] /usr/lib/libc.so.6(+0x158208) [0x7ff2e502e208] (??:0)
[3] SoftBodyRenderingServerHandler::set_vertex(int, Vector3 const&) (scene/3d/soft_body_3d.cpp:86)
[4] GodotSoftBody3D::update_rendering_server(PhysicsServer3DRenderingServerHandler*) (servers/physics_3d/godot_soft_body_3d.cpp:160)
[5] GodotPhysicsServer3D::soft_body_update_rendering_server(RID, PhysicsServer3DRenderingServerHandler*) (servers/physics_3d/godot_physics_server_3d.cpp:956)
[6] CommandQueueMT::Command2<PhysicsServer3D, void (PhysicsServer3D::*)(RID, PhysicsServer3DRenderingServerHandler*), RID, PhysicsServer3DRenderingServerHandler*>::call() (./core/templates/command_queue_mt.h:322)
[7] CommandQueueMT::_flush() (./core/templates/command_queue_mt.h:374)
[8] CommandQueueMT::wait_and_flush() (./core/templates/command_queue_mt.h:415)
[9] PhysicsServer3DWrapMT::thread_loop() (servers/physics_server_3d_wrap_mt.cpp:57)
[10] PhysicsServer3DWrapMT::_thread_callback(void*) (servers/physics_server_3d_wrap_mt.cpp:48)
[11] Thread::callback(unsigned long, Thread::Settings const&, void (*)(void*), void*) (core/os/thread.cpp:63)
[12] void std::__invoke_impl<void, void (*)(unsigned long, Thread::Settings const&, void (*)(void*), void*), unsigned long, Thread::Settings, void (*)(void*), void*>(std::__invoke_other, void (*&&)(unsigned long, Thread::Settings const&, void (*)(void*), void*), unsigned long&&, Thread::Settings&&, void (*&&)(void*), void*&&) (/usr/include/c++/13.2.1/bits/invoke.h:61)
[13] std::__invoke_result<void (*)(unsigned long, Thread::Settings const&, void (*)(void*), void*), unsigned long, Thread::Settings, void (*)(void*), void*>::type std::__invoke<void (*)(unsigned long, Thread::Settings const&, void (*)(void*), void*), unsigned long, Thread::Settings, void (*)(void*), void*>(void (*&&)(unsigned long, Thread::Settings const&, void (*)(void*), void*), unsigned long&&, Thread::Settings&&, void (*&&)(void*), void*&&) (/usr/include/c++/13.2.1/bits/invoke.h:97)
[14] void std::thread::_Invoker<std::tuple<void (*)(unsigned long, Thread::Settings const&, void (*)(void*), void*), unsigned long, Thread::Settings, void (*)(void*), void*> >::_M_invoke<0ul, 1ul, 2ul, 3ul, 4ul>(std::_Index_tuple<0ul, 1ul, 2ul, 3ul, 4ul>) (/usr/include/c++/13.2.1/bits/std_thread.h:292)
[15] std::thread::_Invoker<std::tuple<void (*)(unsigned long, Thread::Settings const&, void (*)(void*), void*), unsigned long, Thread::Settings, void (*)(void*), void*> >::operator()() (/usr/include/c++/13.2.1/bits/std_thread.h:299)
[16] std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)(unsigned long, Thread::Settings const&, void (*)(void*), void*), unsigned long, Thread::Settings, void (*)(void*), void*> > >::_M_run() (/usr/include/c++/13.2.1/bits/std_thread.h:244)
[17] /usr/lib/libstdc++.so.6(+0xe1943) [0x7ff2e52ab943] (??:0)
[18] /usr/lib/libc.so.6(+0x8c9eb) [0x7ff2e4f629eb] (??:0)
[19] /usr/lib/libc.so.6(+0x1107cc) [0x7ff2e4fe67cc] (??:0)
-- END OF BACKTRACE --
================================================================