In Ruby 2.4 thru 3.2, you could check the hidden ruby_vm_global_ptr != NULL.
For my use, in rb-sys there is a feature to automatically report Rust memory allocations via rb_gc_adjust_memory_usage. However, there is a bug that can cause processes to deadlock and/or segfault if a Rust destructor gets called after the VM quits (which can happen in Rust background threads). Currently, I don’t think there’s a way to safely make this feature work. (https://github.com/oxidize-rb/rb-sys/pull/205)
It would be nice to have an official way to know if the Ruby VM is available, basically just a ruby_current_vm_ptr != null. Thoughts?
Maybe you want to explore a similar fix which will short-circuit functions like rb_gc_adjust_memory_usage instead of forcing callers to figure out if it's safe?
So far I’ve encountered this issue this issue in rb_gc_adjust_memory_usage and rb_during_gc, so I still think having the ability to check for a Ruby VM is important for writing robust code. At the very least, it would provide an escape hatch in case there are undiscovered edge cases lurking.