Repository: godot
Issue: `OS.open_midi_inputs()` crashes executing it after a while
Labels: bug,topic:audio,crash

### Godot version

4.0 beta 6

### System information

Ubuntu 22.04 - Nvidia GTX 970, Gnome shell 42 X11

### Issue description

Running for a few seconds
```
OS.open_midi_inputs()
```
shows this errors
```
[New Thread 0x7fff0cee1640 (LWP 300057)]
ALSA lib seq_hw.c:466:(snd_seq_hw_open) open /dev/snd/seq failed: Cannot allocate memory
WARNING: A Thread object has been re-started without wait_to_finish() having been called on it. Please do so to ensure correct cleanup of the thread.
     at: start (core/os/thread.cpp:76)
```
and closing app will crash with this message/backtrace
```
ERROR: snd_rawmidi_read error: Bad file descriptor
   at: read_in (drivers/alsamidi/midi_driver_alsamidi.cpp:147)
ERROR: snd_rawmidi_read error: Bad file descriptor
   at: read_in (drivers/alsamidi/midi_driver_alsamidi.cpp:147)

Thread 226 "godot4" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fff3d732640 (LWP 300536)]
0x00007fffd06d95fe in snd_seq_event_input () from /lib/x86_64-linux-gnu/libasound.so.2
(gdb) backtrace
#0  0x00007fffd06d95fe in snd_seq_event_input () from /lib/x86_64-linux-gnu/libasound.so.2
#1  0x00007fffd06dbd7b in ?? () from /lib/x86_64-linux-gnu/libasound.so.2
#2  0x00000000043adf3a in MIDIDriverALSAMidi::InputConnection::read_in (this=0x102135a8, driver=..., 
    timestamp=0) at drivers/alsamidi/midi_driver_alsamidi.cpp:143
#3  0x00000000043ae0e3 in MIDIDriverALSAMidi::thread_func (p_udata=0x7fffffffd8f8)
    at drivers/alsamidi/midi_driver_alsamidi.cpp:168
#4  0x00000000075660b1 in Thread::callback (p_self=0x7fffffffd900, p_settings=..., 
    p_callback=0x43ae04e <MIDIDriverALSAMidi::thread_func(void*)>, p_userdata=0x7fffffffd8f8)
    at core/os/thread.cpp:65
#5  0x00000000075670a1 in std::__invoke_impl<void, void (*)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*, Thread::Settings, void (*)(void*), void*> (
    __f=@0x1001f718: 0x7565ff4 <Thread::callback(Thread*, Thread::Settings const&, void (*)(void*), void*)>) at /usr/include/c++/11/bits/invoke.h:61
#6  0x0000000007566f46 in std::__invoke<void (*)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*, Thread::Settings, void (*)(void*), void*> (
    __fn=@0x1001f718: 0x7565ff4 <Thread::callback(Thread*, Thread::Settings const&, void (*)(void*), void*)>) at /usr/include/c++/11/bits/invoke.h:96
#7  0x0000000007566de5 in std::thread::_Invoker<std::tuple<void (*)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*, Thread::Settings, void (*)(void*), void*> >::_M_invoke<0ul, 1ul, 2ul, 3ul, 4ul> (this=0x1001f6f8) at /usr/include/c++/11/bits/std_thread.h:253
#8  0x0000000007566d46 in std::thread::_Invoker<std::tuple<void (*)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*, Thread::Settings, void (*)(void*), void*> >::operator() (this=0x1001f6f8)
    at /usr/include/c++/11/bits/std_thread.h:260
#9  0x0000000007566d26 in std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (*)(Thread*, Thread::Settings const&, void (*)(void*), void*), Thread*, Thread::Settings, void (*)(void*), void*> > >::_M_run (this=0x1001f6f0) at /usr/include/c++/11/bits/std_thread.h:211
#10 0x00000000080ccd04 in execute_native_thread_routine ()
#11 0x00007ffff7b84b43 in start_thread (arg=<optimized out>) at ./nptl/pthread_create.c:442
#12 0x00007ffff7c16a00 in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:81
```

### Steps to reproduce

Above

### Minimal reproduction project

N/A