diff --git a/lib/timeout.rb b/lib/timeout.rb index 7f40baf..1d092f7 100644 --- a/lib/timeout.rb +++ b/lib/timeout.rb @@ -120,7 +120,7 @@ def self.create_timeout_thread requests.reject!(&:done?) end end - ThreadGroup::Default.add(watcher) + ThreadGroup::Default.add(watcher) unless watcher.group.enclosed? watcher.name = "Timeout stdlib thread" watcher.thread_variable_set(:"\0__detached_thread__", true) watcher diff --git a/test/test_timeout.rb b/test/test_timeout.rb index 2d3dd16..882a3ca 100644 --- a/test/test_timeout.rb +++ b/test/test_timeout.rb @@ -172,4 +172,24 @@ def test_threadgroup end; end + def test_handling_enclosed_threadgroup + # The problem "add: can't move from the enclosed thread group" #24, + # happens when the timeout_thread is created in an enclosed ThreadGroup. + # Make sure there isn't a timeout_thread in use to test this situation. + Timeout.instance_eval { @timeout_thread = nil } + + t1 = Thread.new { + Thread.stop + assert_block do + Timeout.timeout(0.1) {} + true + end + } + sleep 0.1 while t1.status != 'sleep' + group = ThreadGroup.new + group.add(t1) + group.enclose + t1.run + t1.join + end end