Skip to content

Commit

Permalink
Don't move the timer_thread when it's enclosed
Browse files Browse the repository at this point in the history
Don't move the timer_thread to ThreadGroup::Default, when it's
created in an enclosed ThreadGroup.
Prevents the exception: "add" can't move from the enclosed thread group"
  • Loading branch information
gamecreature committed Feb 14, 2023
1 parent 1415dd1 commit 27f27d5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/timeout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions test/test_timeout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,25 @@ 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.
assert_separately(%w[-rtimeout], <<-'end;')
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
end

0 comments on commit 27f27d5

Please sign in to comment.