Repository: godot
Issue: Crash with adding/removing AnimationLibraries and trying to play an animation
Labels: bug,confirmed,crash,topic:animation

### Tested versions

4.2.1 stable mono, 4.2.1 (standard non-mono version).

### System information

Windows 11 - Vulkan - Nvidia RTX 4070 - intel i5 13600KF

### Issue description

In my project when I try to load animation library on the fly and then play animation I get random crashes with errors as below. Sometimes animation plays couple times and then crashes and sometimes it crashes immediately.

Step by step what I do:
1. No animation is played, no library is in AnimationPlayer
2. Load all animation libraries in Ready()
3. On input event (_UnhandledInput) I invoke a method to play an animation which does:
4. Check if AnimationPlayer is playing, if yes then return and don't do anything
5. If no animation is playing then remove current AnimationLibrary if it's in animation library list
6. Add new animation library to the animation player
7. Play animation from this library

Nothing else is happening in my project currently, no other code, everything running on the main thread, I tried async and await a frame for things to settle down but it makes no difference.

I can stress enough the part that there're no animations in the animation player and no animations are playing when I press the button there's nothing else in the project, which makes this next step strange.
If before removing and adding animation library to the animation player I call animPlayer.Stop() then I get no errors and no crashes but it makes no sense since as I mentioned I do not have any animations playing, no animations are in the animation player to begin with and I also have an if check just in case animation player was playing to return early and not do anything.

In the test project I've removed all the guards and such to keep everything to the minimum, also it's difficult to reproduce with MRP you might need to keep trying for a while, it's easier to reproduce in C# build that's why this demo has both `main.gd` as well as `main.cs`, I did crash the gdscript version too but it took a lot of time, C# is easier to crash (though it still takes more time).
(my animation libraries are a lot larger in my project so chances are that since the MRP library is so small everything happens quickly thus it's more difficult to find the timing for the error while in my project it happens all the time, I can barely play more than 3 animations in succession without crashing.

GDscript version crashes with no error, I also tried `--verbose` mode but gdscript version still gave nothing just silently crashed but it might be a different issue or a combined one, I noticed that when trying to play an animation and then unfocusing window play (moving it) it can cause crash too but without any error messages I can't determine if this is the same issue or not that's why again I'm providing both gdscript and C# scripts.

Error message:
```

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication .
   at Godot.NativeInterop.NativeFuncs.godotsharp_method_bind_ptrcall(IntPtr, IntPtr, Void**, Void*)
   at Godot.NativeCalls.godot_icall_4_145(IntPtr, IntPtr, Godot.NativeInterop.godot_string_name, Double, Single, Godot.)
   at Godot.AnimationPlayer.Play(Godot.StringName, Double, Single, Boolean)
   at main.PlayAnimation()
   at main._UnhandledInput(Godot.InputEvent)
   at Godot.Node.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name ByRef, Godot.NativeInterop.NativeVariantP)
   at Godot.Node3D.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name ByRef, Godot.NativeInterop.NativeVarian)
   at main.InvokeGodotClassMethod(Godot.NativeInterop.godot_string_name ByRef, Godot.NativeInterop.NativeVariantPtrArgs)
   at Godot.Bridge.CSharpInstanceBridge.Call(IntPtr, Godot.NativeInterop.godot_string_name*, Godot.NativeInterop.godot_)
```


I cannot share my animations from my project since these are under a non-open license but I converted them into godot resources (tres) directly and I can reproduce this issue with the MRP which has an animation from inside godot so the issue is not within my animation files.

It can take quite a while to trigger this until you get the hang of it then it's pretty easy, spam that 1 button as much as possible.
**I purpously removed all the guards in the MRP to make it easier to trigget, I understand it's not a good idea to remove an animation library while an animation is playing but it's really difficult to put together the MRP for this, in my project I have all the guards up and more actually, I triple checked none of them break everytime I test this.**

### Steps to reproduce

1. Open the project in the mono editor preferably (details why mentioned above, otherwise check step 5 first but be aware it's more difficult to trigger this with gdscript for some reason it just takes a lot longer)
2. Press the Play button
3. Keep pressing number 1 above letters on your keyboard (or change the input key in Project Settings)
4. At some point you should see the crash, prepare yourself it might take a while.
(5. Drop the main.cs project from the Main node and attach main.gd to the project and go back to step 1)

### Minimal reproduction project (MRP)

[TestAnimLibs.zip](https://github.com/godotengine/godot/files/13805497/TestAnimLibs.zip)


EDIT: I also tried awaiting `caches_cleared` (called it before), `animation_finished`, `animation_libraries_updated` but it did nothing apart from that it never stopped waiting.