Repository: godot
Issue: Crash when physics run on separate thread
Labels: bug,confirmed,topic:physics,crash

### Godot version

4.0 Beta9, 4.0 dev0369475

### System information

Ubuntu 22.10

### Issue description

Engine crashes when a lot of objects present and Physics set to run on separate thread.
Works fine without this setting

Stack trace:

```
Godot Engine v4.0.beta.custom_build.036947577 - https://godotengine.org
Vulkan API 1.3.224 - Using Vulkan Device #1: NVIDIA - NVIDIA GeForce RTX 3060 Laptop GPU
 
ERROR: Index p_index = 0 is out of bounds (shapes.size() = 0).
   at: get_shape_transform (servers/physics_3d/godot_collision_object_3d.h:128)

================================================================
handle_crash: Program crashed with signal 4
Engine version: Godot Engine v4.0.beta.custom_build (03694757735d2331b157478f2337b3a87421ac92)
Dumping the backtrace. Please include this when reporting the bug to the project developer.
[1] /lib/x86_64-linux-gnu/libc.so.6(+0x3bcf0) [0x7f073bc3bcf0] (??:0)
[2] GodotCollisionObject3D::get_shape_transform(int) const (??:0)
[3] GodotSpace3D::test_body_motion(GodotBody3D*, PhysicsServer3D::MotionParameters const&, PhysicsServer3D::MotionResult*) (??:0)
[4] GodotPhysicsServer3D::body_test_motion(RID, PhysicsServer3D::MotionParameters const&, PhysicsServer3D::MotionResult*) (??:0)
[5] PhysicsServer3DWrapMT::body_test_motion(RID, PhysicsServer3D::MotionParameters const&, PhysicsServer3D::MotionResult*) (??:0)
[6] PhysicsBody3D::move_and_collide(PhysicsServer3D::MotionParameters const&, PhysicsServer3D::MotionResult&, bool, bool) (??:0)
[7] CharacterBody3D::_move_and_slide_grounded(double, bool) (??:0)
[8] CharacterBody3D::move_and_slide() (??:0)
[9] void call_with_ptr_args_ret_helper<__UnexistingClass, bool>(__UnexistingClass*, bool (__UnexistingClass::*)(), void const**, void*, IndexSequence<>) (??:0)
[10] void call_with_ptr_args_ret<__UnexistingClass, bool>(__UnexistingClass*, bool (__UnexistingClass::*)(), void const**, void*) (??:0)
[11] MethodBindTR<bool>::ptrcall(Object*, void const**, void*) const (??:0)
[12] GDScriptFunction::call(GDScriptInstance*, Variant const**, int, Callable::CallError&, GDScriptFunction::CallState*) (??:0)
[13] GDScriptInstance::callp(StringName const&, Variant const**, int, Callable::CallError&) (??:0)
[14] bool Node::_gdvirtual__physics_process_call<false>(double) (??:0)
[15] Node::_notification(int) (??:0)
[16] Node::_notificationv(int, bool) (??:0)
[17] Node3D::_notificationv(int, bool) (??:0)
[18] CollisionObject3D::_notificationv(int, bool) (??:0)
[19] PhysicsBody3D::_notificationv(int, bool) (??:0)
[20] CharacterBody3D::_notificationv(int, bool) (??:0)
[21] Object::notification(int, bool) (??:0)
[22] SceneTree::_notify_group_pause(StringName const&, int) (??:0)
[23] SceneTree::physics_process(double) (??:0)
[24] Main::iteration() (??:0)
[25] OS_LinuxBSD::run() (??:0)
[26] ./godot.linuxbsd.template_debug.dev.x86_64(main+0x14a) [0x55565885f1a3] (??:0)
[27] /lib/x86_64-linux-gnu/libc.so.6(+0x23510) [0x7f073bc23510] (??:0)
[28] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x89) [0x7f073bc235c9] (??:0)
[29] ./godot.linuxbsd.template_debug.dev.x86_64(_start+0x25) [0x55565885ef95] (??:0)
-- END OF BACKTRACE --
================================================================
```

GDB stracktrace:
```
[Current thread is 1 (Thread 0x7f073bf447c0 (LWP 73854))]
(gdb) bt
#0  __pthread_kill_implementation (no_tid=0, signo=6, threadid=<optimized out>) at ./nptl/pthread_kill.c:44
#1  __pthread_kill_internal (signo=6, threadid=<optimized out>) at ./nptl/pthread_kill.c:78
#2  __GI___pthread_kill (threadid=<optimized out>, signo=signo@entry=6) at ./nptl/pthread_kill.c:89
#3  0x00007f073bc3bc46 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
#4  0x00007f073bc227fc in __GI_abort () at ./stdlib/abort.c:79
#5  0x000055565885f9d8 in handle_crash (sig=<optimized out>) at platform/linuxbsd/crash_handler_linuxbsd.cpp:131
#6  <signal handler called>
#7  GodotCollisionObject3D::get_shape_transform (this=this@entry=0x555662106bc0, p_index=p_index@entry=0) at servers/physics_3d/godot_collision_object_3d.h:128
#8  0x000055565a0e631d in GodotSpace3D::test_body_motion (this=<optimized out>, p_body=p_body@entry=0x55566216aa90, p_parameters=..., r_result=r_result@entry=0x7ffd96640cb0) at servers/physics_3d/godot_space_3d.cpp:848
#9  0x000055565a0c513d in GodotPhysicsServer3D::body_test_motion (this=0x55565dca0710, p_body=..., p_parameters=..., r_result=0x7ffd96640cb0) at servers/physics_3d/godot_physics_server_3d.cpp:920
#10 0x000055565a470556 in PhysicsServer3DWrapMT::body_test_motion (this=0x5556604469a0, p_body=..., p_parameters=..., r_result=0x7ffd96640cb0) at servers/physics_server_3d_wrap_mt.h:265
#11 0x0000555659740c6f in PhysicsBody3D::move_and_collide (this=this@entry=0x5556627d73f0, p_parameters=..., r_result=..., p_test_only=p_test_only@entry=false, p_cancel_sliding=<optimized out>) at scene/3d/physics_body_3d.cpp:117
#12 0x0000555659750148 in CharacterBody3D::_move_and_slide_grounded (this=this@entry=0x5556627d73f0, p_delta=p_delta@entry=0.0083333333333333332, p_was_on_floor=p_was_on_floor@entry=false) at scene/3d/physics_body_3d.cpp:1271
#13 0x0000555659750951 in CharacterBody3D::move_and_slide (this=0x5556627d73f0) at scene/3d/physics_body_3d.cpp:1220
#14 0x0000555658931178 in call_with_ptr_args_ret_helper<__UnexistingClass, bool>(__UnexistingClass*, bool (__UnexistingClass::*)(), void const**, void*, IndexSequence<>) (p_instance=<optimized out>, p_method=<optimized out>, p_args=<optimized out>, r_ret=0x7ffd966422a0) at ./core/variant/binder_common.h:293
#15 0x0000555658931189 in call_with_ptr_args_ret<__UnexistingClass, bool> (p_instance=<optimized out>, p_method=<optimized out>, p_args=<optimized out>, r_ret=<optimized out>) at ./core/variant/binder_common.h:541
#16 0x00005556589311b2 in MethodBindTR<bool>::ptrcall (this=<optimized out>, p_object=<optimized out>, p_args=<optimized out>, r_ret=<optimized out>) at ./core/object/method_bind.h:474
#17 0x0000555658a258a2 in GDScriptFunction::call (this=0x5556608f9860, p_instance=<optimized out>, p_instance@entry=0x555661e013a0, p_args=p_args@entry=0x7ffd966425d8, p_argcount=p_argcount@entry=1, r_err=..., p_state=<optimized out>) at modules/gdscript/gdscript_vm.cpp:1831
#18 0x000055565896900e in GDScriptInstance::callp (this=0x555661e013a0, p_method=..., p_args=0x7ffd966425d8, p_argcount=1, r_error=...) at modules/gdscript/gdscript.cpp:1830
#19 0x00005556592d54c7 in Node::_gdvirtual__physics_process_call<false> (this=this@entry=0x5556627d73f0, arg1=0.0083333333333333332) at scene/main/node.h:238
#20 0x00005556592c947e in Node::_notification (this=0x5556627d73f0, p_notification=<optimized out>) at scene/main/node.cpp:60
#21 0x00005556589201c3 in Node::_notificationv (this=<optimized out>, p_notification=<optimized out>, p_reversed=<optimized out>) at ./scene/main/node.h:45
#22 0x00005556589202a6 in Node3D::_notificationv (this=0x5556627d73f0, p_notification=16, p_reversed=<optimized out>) at ./scene/3d/node_3d.h:52
#23 0x0000555659610528 in CollisionObject3D::_notificationv (this=0x5556627d73f0, p_notification=16, p_reversed=<optimized out>) at ./scene/3d/collision_object_3d.h:38
#24 0x000055565975c3ea in PhysicsBody3D::_notificationv (this=0x5556627d73f0, p_notification=16, p_reversed=<optimized out>) at scene/3d/physics_body_3d.h:43
#25 0x000055565976a5f8 in CharacterBody3D::_notificationv (this=0x5556627d73f0, p_notification=16, p_reversed=<optimized out>) at scene/3d/physics_body_3d.h:344
#26 0x000055565a9b239c in Object::notification (this=0x5556627d73f0, p_notification=p_notification@entry=16, p_reversed=p_reversed@entry=false) at core/object/object.cpp:790
#27 0x00005556592f897d in SceneTree::_notify_group_pause (this=this@entry=0x555660482b80, p_group=..., p_notification=p_notification@entry=16) at scene/main/scene_tree.cpp:874
#28 0x00005556592f8b01 in SceneTree::physics_process (this=0x555660482b80, p_time=0.0083333333333333332) at scene/main/scene_tree.cpp:428
#29 0x000055565889c7d8 in Main::iteration () at main/main.cpp:3163
#30 0x0000555658860d16 in OS_LinuxBSD::run (this=this@entry=0x7ffd966428d0) at platform/linuxbsd/os_linuxbsd.cpp:878
#31 0x000055565885f1a3 in main (argc=<optimized out>, argv=0x7ffd96642e18) at platform/linuxbsd/godot_linuxbsd.cpp:73
```

### Steps to reproduce

Enable Physics on separate thread.
Start adding  adding objects and removing part of them.
On about 3000 objects (and about 10 objects deleted per second) you will get crash.


### Minimal reproduction project

N/A