Skip to content

Commit c4f1385

Browse files
larskaniseregon
authored andcommitted
Explicit add the timeout thread to default ThreadGroup
Otherwise the timeout thread would be added to the ThreadGroup of the thread that makes the first call to Timeout.timeout . Fixes bug 19020: https://bugs.ruby-lang.org/issues/19020 Add a test case to make sure the common thread doesn't leak to another ThreadGroup
1 parent bd25f4b commit c4f1385

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/timeout.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def self.create_timeout_thread
120120
requests.reject!(&:done?)
121121
end
122122
end
123+
ThreadGroup::Default.add(watcher)
123124
watcher.name = "Timeout stdlib thread"
124125
watcher.thread_variable_set(:"\0__detached_thread__", true)
125126
watcher

test/test_timeout.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,17 @@ def test_fork
159159
assert_equal 'timeout', r.read
160160
r.close
161161
end
162+
163+
def test_threadgroup
164+
assert_separately(%w[-rtimeout], <<-'end;')
165+
tg = ThreadGroup.new
166+
thr = Thread.new do
167+
tg.add(Thread.current)
168+
Timeout.timeout(10){}
169+
end
170+
thr.join
171+
assert_equal [].to_s, tg.list.to_s
172+
end;
173+
end
174+
162175
end

0 commit comments

Comments
 (0)