Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent backtrace from hanging if objects in the backtrace use Thread in inspect #1038

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading