Repository: godot
Issue: Concurent threaded Instantiating of nodes  from same PackedScene causes crash: "Trying to unreference a SafeRefCount"
Labels: bug,topic:core

### Tested versions

- 4.3 https://github.com/godotengine/godot/commit/d40fc50f086d14583c7cc979ed4e5363ac223717
-

### System information

Win 10

### Issue description

I have 2 treads using one PackedScene resource that try to instantiate scenes at same time.
Upd: PackedScene doesn't have to be same object, can be different objects instances but have to be loaded from same scene path.

That causes:
```

RASH_COND_MSG(count.get() == 0,
				"Trying to unreference a SafeRefCount which is already zero is wrong and a symptom of it being misused.\n"
				"Upon a SafeRefCount reaching zero any object whose lifetime is tied to it, as well as the ref count itself, must be destroyed.\n"
				"Moreover, to guarantee that, no multiple threads should be racing to do the final unreferencing to zero.");
```

Callstack

```


godot.windows.editor.dev.x86_64.exe!SafeRefCount::_check_unref_safety() Line 187 (..\Godot\core\templates\safe_refcount.h:187)
godot.windows.editor.dev.x86_64.exe!SafeRefCount::unrefval() Line 214 (..\Godot\core\templates\safe_refcount.h:214)
godot.windows.editor.dev.x86_64.exe!RefCounted::unreference() Line 77 (..\Godot\core\object\ref_counted.cpp:77)
godot.windows.editor.dev.x86_64.exe!Ref<Resource>::unref() Line 209 (..\Godot\core\object\ref_counted.h:209)
godot.windows.editor.dev.x86_64.exe!Ref<Resource>::~Ref<Resource>() Line 223 (..\Godot\core\object\ref_counted.h:223)
godot.windows.editor.dev.x86_64.exe!KeyValue<Ref<Resource>,Ref<Resource>>::~KeyValue<Ref<Resource>,Ref<Resource>>() (Unknown Source:0)
godot.windows.editor.dev.x86_64.exe!HashMapElement<Ref<Resource>,Ref<Resource>>::~HashMapElement<Ref<Resource>,Ref<Resource>>() (Unknown Source:0)
godot.windows.editor.dev.x86_64.exe!HashMapElement<Ref<Resource>,Ref<Resource>>::`scalar deleting destructor'(unsigned int) (Unknown Source:0)
godot.windows.editor.dev.x86_64.exe!memdelete<HashMapElement<Ref<Resource>,Ref<Resource>>>(HashMapElement<Ref<Resource>,Ref<Resource>> * p_class) Line 116 (..\Godot\core\os\memory.h:116)
godot.windows.editor.dev.x86_64.exe!DefaultTypedAllocator<HashMapElement<Ref<Resource>,Ref<Resource>>>::delete_allocation(HashMapElement<Ref<Resource>,Ref<Resource>> * p_allocation) Line 221 (..\Godot\core\os\memory.h:221)
godot.windows.editor.dev.x86_64.exe!HashMap<Ref<Resource>,Ref<Resource>,HashMapHasherDefault,HashMapComparatorDefault<Ref<Resource>>,DefaultTypedAllocator<HashMapElement<Ref<Resource>,Ref<Resource>>>>::clear() Line 266 (..\Godot\core\templates\hash_map.h:266)
godot.windows.editor.dev.x86_64.exe!HashMap<Ref<Resource>,Ref<Resource>,HashMapHasherDefault,HashMapComparatorDefault<Ref<Resource>>,DefaultTypedAllocator<HashMapElement<Ref<Resource>,Ref<Resource>>>>::~HashMap<Ref<Resource>,Ref<Resource>,HashMapHasherDefault,HashMapComparatorDefault<Ref<Resource>>,DefaultTypedAllocator<HashMapElement<Ref<Resource>,Ref<Resource>>>>() Line 616 (..\Godot\core\templates\hash_map.h:616)
godot.windows.editor.dev.x86_64.exe!SceneState::instantiate(SceneState::GenEditState p_edit_state) Line 607 (..\Godot\scene\resources\packed_scene.cpp:607)
godot.windows.editor.dev.x86_64.exe!PackedScene::instantiate(PackedScene::GenEditState p_edit_state) Line 2093 (..\Godot\scene\resources\packed_scene.cpp:2093)
godot.windows.editor.dev.x86_64.exe!SceneState::instantiate(SceneState::GenEditState p_edit_state) Line 232 (..\Godot\scene\resources\packed_scene.cpp:232)
godot.windows.editor.dev.x86_64.exe!PackedScene::instantiate(PackedScene::GenEditState p_edit_state) Line 2093 (..\Godot\scene\resources\packed_scene.cpp:2093)
godot.windows.editor.dev.x86_64.exe!SceneState::instantiate(SceneState::GenEditState p_edit_state) Line 199 (..\Godot\scene\resources\packed_scene.cpp:199)
godot.windows.editor.dev.x86_64.exe!PackedScene::instantiate(PackedScene::GenEditState p_edit_state) Line 2093 (..\Godot\scene\resources\packed_scene.cpp:2093)
godot.windows.editor.dev.x86_64.exe!call_with_ptr_args_retc_helper<PackedScene,Node *,enum PackedScene::GenEditState,0>(PackedScene * p_instance, Node *(const PackedScene::*)(PackedScene::GenEditState) p_method, const void * * p_args, void * r_ret, IndexSequence<0> __formal) Line 340 (..\Godot\core\variant\binder_common.h:340)
godot.windows.editor.dev.x86_64.exe!call_with_ptr_args_retc<PackedScene,Node *,enum PackedScene::GenEditState>(PackedScene * p_instance, Node *(const PackedScene::*)(PackedScene::GenEditState) p_method, const void * * p_args, void * r_ret) Line 588 (..\Godot\core\variant\binder_common.h:588)
godot.windows.editor.dev.x86_64.exe!MethodBindTRC<PackedScene,Node *,enum PackedScene::GenEditState>::ptrcall(Object * p_object, const void * * p_args, void * r_ret) Line 641 (..\Godot\core\object\method_bind.h:641)
godot.windows.editor.dev.x86_64.exe!gdextension_object_method_bind_ptrcall(const void * p_method_bind, void * p_instance, const void * const * p_args, void * p_ret) Line 1222 (..\Godot\core\extension\gdextension_interface.cpp:1222)

```

### Steps to reproduce

Have one PackedScene shared between threads. Instantiate it until a crash.

### Minimal reproduction project (MRP)

N/A