[runtime] Fix a thread join race condition.  Fixes #18026.