From 05fe5e19ebb7d90037a0fedd514fa32d9eb96396 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Tue, 24 Jun 2025 22:37:18 +0200 Subject: [PATCH] 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. --- lib/timeout.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/timeout.rb b/lib/timeout.rb index 4fd1fa4..f5f232a 100644 --- a/lib/timeout.rb +++ b/lib/timeout.rb @@ -123,6 +123,9 @@ def self.create_timeout_thread def self.ensure_timeout_thread_created 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 unless @timeout_thread and @timeout_thread.alive? @timeout_thread = create_timeout_thread