Repository: godot
Issue: 2D project crashes with separate thread physics and object pooling
Labels: bug,confirmed,topic:physics,crash

### Godot version

v4.0.stable.official (92bee43adba8d2401ef40e2480e53087bcb1eaf1)

### System information

Ubuntu 22.04.2 LTS, RTX3080TI, 525.78.01. Vulkan API 1.3.224

### Issue description

Hello there. I have started to learn godot recently. I took Dodge The Creeps tutorial and added some features here and there.

I enabled separate thread physics processing from the start and already fixed some bugs related to the multithreaded physics. Real problems started to appear when i added the object pooling for bullets. And now i know that godot does not need pooling as much as garbage-collected engines, but i just want to understand why it crashes and how to prevent such bugs in future.

As far as i know, it is safe to add physical nodes to the tree in _physics_process step because it is synchronized with separate physical threads.
Also i assume that it is always safe to add/remove child nodes from any other function with the call_deferred method. And i do exactly that.
Bullets are added to the tree from _physics_process and they are removed from the tree from two different places with the call_deferred method. As result, the project runs about a minute and crashes afterwards with different errors like these

```
Running: /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64 --path /home/pnemtsev/projects/2dgame --remote-debug tcp://127.0.0.1:6007 --editor-pid 66342 --position 768,336 res://main.tscn
Godot Engine v4.0.stable.official.92bee43ad - https://godotengine.org
Vulkan API 1.3.224 - Forward+ - Using Vulkan Device #0: NVIDIA - NVIDIA GeForce RTX 3080 Ti
ERROR: FATAL: Index p_index = 0 is out of bounds (size() = 0).
   at: get (./core/templates/cowdata.h:155)
================================================================
handle_crash: Program crashed with signal 4
Engine version: Godot Engine v4.0.stable.official (92bee43adba8d2401ef40e2480e53087bcb1eaf1)
Dumping the backtrace. Please include this when reporting the bug to the project developer.
[1] /lib/x86_64-linux-gnu/libc.so.6(+0x42520) [0x7f8930a42520] (??:0)
[2] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x3a2b2da] (??:0)
[3] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x3a6a03d] (??:0)
[4] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x3164a02] (??:0)
[5] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x31add3a] (??:0)
[6] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x31c9979] (??:0)
[7] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x12a7aea] (??:0)
[8] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x118b77f] (??:0)
[9] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x2b5cfdd] (??:0)
[10] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x3148bc2] (??:0)
[11] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x4568594] (??:0)
[12] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x2b9a3ee] (??:0)
[13] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x2bd1cb4] (??:0)
[14] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0xebffb6] (??:0)
[15] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0xe027f3] (??:0)
[16] /lib/x86_64-linux-gnu/libc.so.6(+0x29d90) [0x7f8930a29d90] (??:0)
[17] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80) [0x7f8930a29e40] (??:0)
[18] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0xe2286e] (??:0)
-- END OF BACKTRACE --
================================================================
```

```
Running: /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64 --path /home/pnemtsev/projects/2dgame --remote-debug tcp://127.0.0.1:6007 --editor-pid 66342 --position 768,336 res://main.tscn
Godot Engine v4.0.stable.official.92bee43ad - https://godotengine.org
Vulkan API 1.3.224 - Forward+ - Using Vulkan Device #0: NVIDIA - NVIDIA GeForce RTX 3080 Ti
ERROR: FATAL: Index p_index = 0 is out of bounds (shapes.size() = 0).
   at: get_shape (servers/physics_2d/godot_collision_object_2d.h:117)

================================================================
handle_crash: Program crashed with signal 4
Engine version: Godot Engine v4.0.stable.official (92bee43adba8d2401ef40e2480e53087bcb1eaf1)
Dumping the backtrace. Please include this when reporting the bug to the project developer.
[1] /lib/x86_64-linux-gnu/libc.so.6(+0x42520) [0x7fd4d3c42520] (??:0)
[2] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x3a6ac6d] (??:0)
[3] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x3164a02] (??:0)
[4] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x31add3a] (??:0)
[5] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x31c9979] (??:0)
[6] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x12a7aea] (??:0)
[7] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x118b77f] (??:0)
[8] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x2b5cfdd] (??:0)
[9] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x3148bc2] (??:0)
[10] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x4568594] (??:0)
[11] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x2b9a3ee] (??:0)
[12] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0x2bd1cb4] (??:0)
[13] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0xebffb6] (??:0)
[14] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0xe027f3] (??:0)
[15] /lib/x86_64-linux-gnu/libc.so.6(+0x29d90) [0x7fd4d3c29d90] (??:0)
[16] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80) [0x7fd4d3c29e40] (??:0)
[17] /home/pnemtsev/bin/godot/Godot_v4.0-stable_linux.x86_64() [0xe2286e] (??:0)
-- END OF BACKTRACE --
================================================================
```

I just have tried that on the recently released stable 4.0 version, but with older rc versions, and even windows buids i had similar results.

### Steps to reproduce

Run the project for about a minute, and don't forget to shoot the bullets continuously.

### Minimal reproduction project

[2dgame.zip](https://github.com/godotengine/godot/files/10864451/2dgame.zip)