Skip to content

Commit

Permalink
Prevent backtrace from hanging if objects in the backtrace use Thread…
Browse files Browse the repository at this point in the history
… in inspect

Co-authored-by: Stan Lo <[email protected]>
  • Loading branch information
2 people authored and ko1 committed Nov 29, 2023
1 parent 5c33af4 commit 4e2bd25
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/debug/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -696,15 +696,15 @@ def register_default_command
register_command 'bt', 'backtrace', unsafe: false do |arg|
case arg
when /\A(\d+)\z/
request_tc [:show, :backtrace, arg.to_i, nil]
request_tc_with_restarted_threads [:show, :backtrace, arg.to_i, nil]
when /\A\/(.*)\/\z/
pattern = $1
request_tc [:show, :backtrace, nil, Regexp.compile(pattern)]
request_tc_with_restarted_threads [:show, :backtrace, nil, Regexp.compile(pattern)]
when /\A(\d+)\s+\/(.*)\/\z/
max, pattern = $1, $2
request_tc [:show, :backtrace, max.to_i, Regexp.compile(pattern)]
request_tc_with_restarted_threads [:show, :backtrace, max.to_i, Regexp.compile(pattern)]
else
request_tc [:show, :backtrace, nil, nil]
request_tc_with_restarted_threads [:show, :backtrace, nil, nil]
end
end

Expand Down
41 changes: 41 additions & 0 deletions test/console/backtrace_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,45 @@ def test_backtrace_prints_nested_block_label_correctly
end
end
end

class ThreadLockingTraceTest < ConsoleTestCase
def program
<<~RUBY
1| th0 = Thread.new{sleep}
2| $m = Mutex.new
3| th1 = Thread.new do
4| $m.lock
5| sleep 1
6| $m.unlock
7| end
8|
9| o = Object.new
10| def o.inspect
11| $m.lock
12| "foo".tap { $m.unlock }
13| end
14|
15| def foo(o)
16| debugger
17| end
18| sleep 0.5
19| foo(o)
RUBY
end

def test_backtrace_prints_without_hanging
debug_code(program) do
type "c"

type "bt"
assert_line_text(/Object#foo\(o=foo\)/)
type "bt"
assert_line_text(/Object#foo\(o=foo\)/)
type "bt"
assert_line_text(/Object#foo\(o=foo\)/)

type "kill!"
end
end
end
end

0 comments on commit 4e2bd25

Please sign in to comment.