Thread#join can break with fiber scheduler unblock fails or blocks.
In addition to https://bugs.ruby-lang.org/issues/17666 we found several more cases that need to be addressed.
Fix potential hang when joining threads.
If the thread termination invokes user code after
THREAD_KILLED, and the user unblock function causes that
become something else (e.g.
THREAD_RUNNING), threads waiting in
thread_join_sleep will hang forever. We move the unblock function call
to before the thread status is updated, and allow threads to join as soon
th->value becomes defined.
Wake up join list within thread EC context. (#4471)
rb_fiber_scheduler_unblock raises an exception, it can result in a
rb_threadptr_join_list_wakeup is not within a valid EC. This
rb_threadptr_join_list_wakeup into the thread's top level EC
which initially caused an infinite loop because on exception will retry. We
explicitly remove items from the thread's join list to avoid this situation.
These are already fixed on master branch. Here is a PR for backport: https://github.com/ruby/ruby/pull/4686