[ruby/timeout] Gracefully handle a call to ensure_timeout_thread_created in a signal handler

* Fixes the issue described in
  https://github.com/ruby/timeout/issues/17#issuecomment-1461498517
  for TruffleRuby and JRuby.
* CRuby is currently unable to use Timeout in a signal handler due to
  https://bugs.ruby-lang.org/issues/19473.

7a48e1c079
This commit is contained in:
Benoit Daloze 2025-06-24 22:37:18 +02:00 committed by git
parent 4044188266
commit 3b602c952d

View file

@ -123,6 +123,9 @@ module Timeout
def self.ensure_timeout_thread_created def self.ensure_timeout_thread_created
unless @timeout_thread and @timeout_thread.alive? unless @timeout_thread and @timeout_thread.alive?
# If the Mutex is already owned we are in a signal handler.
# In that case, just return and let the main thread create the @timeout_thread.
return if TIMEOUT_THREAD_MUTEX.owned?
TIMEOUT_THREAD_MUTEX.synchronize do TIMEOUT_THREAD_MUTEX.synchronize do
unless @timeout_thread and @timeout_thread.alive? unless @timeout_thread and @timeout_thread.alive?
@timeout_thread = create_timeout_thread @timeout_thread = create_timeout_thread