Skip to content

Commit 3fdcfef

Browse files
st0012ko1
authored andcommitted
Avoid setting @tc in thread_begin event
This event will be triggered before the new thread even has a ThreadClient. So in this case, the @tc will be overridden to just nil, which is wrong. However, we also don't want to just set @tc to the new thread's newly created ThreadClient. This is because a thread's creation can happen during a subsession, and promptly switching @tc to another thread's ThreadClient will misfire thread suspension.
1 parent 13dcca4 commit 3fdcfef

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

lib/debug/session.rb

+10-4
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,24 @@ def request_tc(req)
213213

214214
def process_event evt
215215
# variable `@internal_info` is only used for test
216-
@tc, output, ev, @internal_info, *ev_args = evt
216+
tc, output, ev, @internal_info, *ev_args = evt
217217

218218
output.each{|str| @ui.puts str} if ev != :suspend
219219

220-
case ev
221-
222-
when :thread_begin # special event, tc is nil
220+
# special event, tc is nil
221+
# and we don't want to set @tc to the newly created thread's ThreadClient
222+
if ev == :thread_begin
223223
th = ev_args.shift
224224
q = ev_args.shift
225225
on_thread_begin th
226226
q << true
227227

228+
return
229+
end
230+
231+
@tc = tc
232+
233+
case ev
228234
when :init
229235
enter_subsession
230236
wait_command_loop

test/console/thread_test.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require_relative '../support/console_test_case'
2+
3+
module DEBUGGER__
4+
class ThreadControlTest < ConsoleTestCase
5+
def program
6+
<<~RUBY
7+
1| def foo
8+
2| Thread.new { sleep 5 }
9+
3| end
10+
4|
11+
5| 5.times do
12+
6| foo
13+
7| binding.b(do: "1 == 2") # eval Ruby code in debugger
14+
8| end
15+
RUBY
16+
end
17+
18+
def test_debugger_isnt_hung_by_new_threads
19+
debug_code(program) do
20+
type "c"
21+
end
22+
end
23+
end
24+
end

0 commit comments

Comments
 (0)