Skip to content

Commit

Permalink
restart all threads on eval
Browse files Browse the repository at this point in the history
When a thread keeps a lock, and REPL runs a code which needs the
lock, other threads should make a progress to release the lock.

fix #877
  • Loading branch information
ko1 committed Mar 28, 2023
1 parent e706193 commit 9d2a5c0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
17 changes: 11 additions & 6 deletions lib/debug/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def process_event evt
opt = ev_args[3]
add_tracer ObjectTracer.new(@ui, obj_id, obj_inspect, **opt)
else
# ignore
stop_all_threads
end

wait_command_loop
Expand Down Expand Up @@ -900,13 +900,13 @@ def register_default_command
# * `p <expr>`
# * Evaluate like `p <expr>` on the current frame.
register_command 'p' do |arg|
request_tc [:eval, :p, arg.to_s]
request_eval :p, arg.to_s
end

# * `pp <expr>`
# * Evaluate like `pp <expr>` on the current frame.
register_command 'pp' do |arg|
request_tc [:eval, :pp, arg.to_s]
request_eval :pp, arg.to_s
end

# * `eval <expr>`
Expand All @@ -917,7 +917,7 @@ def register_default_command
@ui.puts "\nTo evaluate the variable `#{cmd}`, use `pp #{cmd}` instead."
:retry
else
request_tc [:eval, :call, arg]
request_eval :call, arg
end
end

Expand All @@ -928,7 +928,7 @@ def register_default_command
@ui.puts "not supported on the remote console."
:retry
end
request_tc [:eval, :irb]
request_eval :irb, nil
end

### Trace
Expand Down Expand Up @@ -1148,7 +1148,7 @@ def process_command line
@repl_prev_line = nil
check_unsafe

request_tc [:eval, :pp, line]
request_eval :pp, line
end

rescue Interrupt
Expand All @@ -1164,6 +1164,11 @@ def process_command line
return :retry
end

def request_eval type, src
restart_all_threads
request_tc [:eval, type, src]
end

def step_command type, arg
if type == :until
leave_subsession [:step, type, arg]
Expand Down
10 changes: 9 additions & 1 deletion lib/debug/thread_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,15 @@ def wait_next_action_
rescue SuspendReplay, SystemExit, Interrupt
raise
rescue Exception => e
pp ["DEBUGGER Exception: #{__FILE__}:#{__LINE__}", e, e.backtrace]
STDERR.puts e.cause.inspect
STDERR.puts e.inspect
Thread.list.each{|th|
STDERR.puts "@@@ #{th}"
th.backtrace.each{|b|
STDERR.puts " > #{b}"
}
}
p ["DEBUGGER Exception: #{__FILE__}:#{__LINE__}", e, e.backtrace]
raise
ensure
@returning = false
Expand Down
26 changes: 26 additions & 0 deletions test/console/eval_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,30 @@ def test_eval_evaluates_computation_and_assignment
end
end
end

class EvalThreadTest < ConsoleTestCase
def program
<<~RUBY
1| th0 = Thread.new{sleep}
2| m = Mutex.new; q = Queue.new
3| th1 = Thread.new do
4| m.lock; q << true
5| sleep 1
6| m.unlock
7| end
8| q.pop # wait for locking
9| p :ok
RUBY
end

def test_eval_with_threads
debug_code program do
type 'b 9'
type 'c'
type 'm.lock.nil?'
assert_line_text 'false'
type 'c'
end
end
end
end

0 comments on commit 9d2a5c0

Please sign in to comment.