From cb25b933e7c30e890129f730a05ce558c6fa3ddd Mon Sep 17 00:00:00 2001 From: Adam Simpkins Date: Thu, 10 Jul 2025 13:47:30 -0700 Subject: [PATCH] Document some deadlocks in the physics server code Add some comments documenting locations where PhysicsServer3D::soft_body_set_mesh() can deadlock. godotengine/godot-proposals#12670 has a proposal for some alternate thread-safe soft body APIs. In the meantime it seems worth at least documenting some of the current pitfalls in the code. --- modules/godot_physics_3d/godot_soft_body_3d.cpp | 4 ++++ modules/jolt_physics/objects/jolt_soft_body_3d.cpp | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/modules/godot_physics_3d/godot_soft_body_3d.cpp b/modules/godot_physics_3d/godot_soft_body_3d.cpp index 9c82781c80c..3f1da45b1c9 100644 --- a/modules/godot_physics_3d/godot_soft_body_3d.cpp +++ b/modules/godot_physics_3d/godot_soft_body_3d.cpp @@ -136,6 +136,10 @@ void GodotSoftBody3D::set_mesh(RID p_mesh) { return; } + // TODO: calling RenderingServer::mesh_surface_get_arrays() from the physics thread + // is not safe and can deadlock when physics/3d/run_on_separate_thread is enabled. + // This method blocks on the main thread to return data, but the main thread may be + // blocked waiting on us in PhysicsServer3D::sync(). Array arrays = RenderingServer::get_singleton()->mesh_surface_get_arrays(soft_mesh, 0); ERR_FAIL_COND(arrays.is_empty()); diff --git a/modules/jolt_physics/objects/jolt_soft_body_3d.cpp b/modules/jolt_physics/objects/jolt_soft_body_3d.cpp index 95bd39b5a39..9813124eb86 100644 --- a/modules/jolt_physics/objects/jolt_soft_body_3d.cpp +++ b/modules/jolt_physics/objects/jolt_soft_body_3d.cpp @@ -128,6 +128,10 @@ bool JoltSoftBody3D::_ref_shared_data() { if (iter_shared_data == mesh_to_shared.end()) { RenderingServer *rendering = RenderingServer::get_singleton(); + // TODO: calling RenderingServer::mesh_surface_get_arrays() from the physics thread + // is not safe and can deadlock when physics/3d/run_on_separate_thread is enabled. + // This method blocks on the main thread to return data, but the main thread may be + // blocked waiting on us in PhysicsServer3D::sync(). const Array mesh_data = rendering->mesh_surface_get_arrays(mesh, 0); ERR_FAIL_COND_V(mesh_data.is_empty(), false);