Skip to content

Commit

Permalink
Let's notify ide dispatcher after server started to avoid race descri…
Browse files Browse the repository at this point in the history
…bed in #38
  • Loading branch information
os97673 committed Jan 31, 2014
1 parent d13955e commit 4f39744
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
40 changes: 33 additions & 7 deletions lib/ruby-debug-ide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'stringio'
require "socket"
require 'thread'
if (RUBY_VERSION < '2.0' || defined?(JRUBY_VERSION))
if RUBY_VERSION < '2.0' || defined?(JRUBY_VERSION)
require 'ruby-debug-base'
else
require 'debase'
Expand Down Expand Up @@ -59,14 +59,14 @@ def interrupt_last
end
end

def start_server(host = nil, port = 1234)
def start_server(host = nil, port = 1234, notify_dispatcher = false)
return if started?
start
start_control(host, port)
start_control(host, port, notify_dispatcher)
end

def prepare_debugger(options)
start_server(options.host, options.port)
start_server(options.host, options.port, options.notify_dispatcher)

raise "Control thread did not start (#{@control_thread}}" unless @control_thread && @control_thread.alive?

Expand Down Expand Up @@ -97,18 +97,20 @@ def run_prog_script
end
end

def start_control(host, port)
def start_control(host, port, notify_dispatcher)
raise "Debugger is not started" unless started?
return if @control_thread
@control_thread = DebugThread.new do
begin
# 127.0.0.1 seemingly works with all systems and with IPv6 as well.
# "localhost" and nil have problems on some systems.
host ||= '127.0.0.1'
gem_name = (defined?(JRUBY_VERSION) || RUBY_VERSION < '1.9.0') ? 'ruby-debug-base' :
RUBY_VERSION < '2.0.0' ? 'ruby-debug-base19x' : 'debase'
server = TCPServer.new(host, port)
gem_name = (defined?(JRUBY_VERSION) || RUBY_VERSION < '1.9.0') ? 'ruby-debug-base' :
RUBY_VERSION < '2.0.0' ? 'ruby-debug-base19x' : 'debase'
$stderr.printf "Fast Debugger (ruby-debug-ide #{IDE_VERSION}, #{gem_name} #{VERSION}) listens on #{host}:#{port}\n"
notify_dispatcher(port) if notify_dispatcher

while (session = server.accept)
$stderr.puts "Connected from #{session.peeraddr[2]}" if Debugger.cli_debug
dispatcher = ENV['IDE_PROCESS_DISPATCHER']
Expand All @@ -134,6 +136,30 @@ def start_control(host, port)
end
end

private

def notify_dispatcher(port)
return unless ENV['IDE_PROCESS_DISPATCHER']
acceptor_host, acceptor_port = ENV['IDE_PROCESS_DISPATCHER'].split(":")
acceptor_host, acceptor_port = '127.0.0.1', acceptor_host unless acceptor_port

connected = false
3.times do |i|
begin
s = TCPSocket.open(acceptor_host, acceptor_port)
s.print(port)
s.close
connected = true
return
rescue => bt
$stderr.puts "#{Process.pid}: connection failed(#{i+1})"
$stderr.puts "Exception: #{bt}"
$stderr.puts bt.backtrace.map { |l| "\t#{l}" }.join("\n")
sleep 0.3
end unless connected
end
end

end

class Exception # :nodoc:
Expand Down
25 changes: 4 additions & 21 deletions lib/ruby-debug-ide/multiprocess/pre_child.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,19 @@ def pre_child
'stop' => false,
'tracing' => false,
'int_handler' => true,
'cli_debug' => (ENV['DEBUGGER_CLI_DEBUG'] == 'true')
'cli_debug' => (ENV['DEBUGGER_CLI_DEBUG'] == 'true'),
'notify_dispatcher' => true
)

acceptor_host, acceptor_port = ENV['IDE_PROCESS_DISPATCHER'].split(":")
acceptor_host, acceptor_port = '127.0.0.1', acceptor_host unless acceptor_port

connected = false
3.times do |i|
begin
s = TCPSocket.open(acceptor_host, acceptor_port)
s.print(port)
s.close
connected = true
start_debugger(options)
return
rescue => bt
$stderr.puts "#{Process.pid}: connection failed(#{i+1})"
$stderr.puts "Exception: #{bt}"
$stderr.puts bt.backtrace.map { |l| "\t#{l}" }.join("\n")
sleep 0.3
end unless connected
end
start_debugger(options)
end

def start_debugger(options)
if Debugger.started?
# we're in forked child, only need to restart control thread
Debugger.breakpoints.clear
Debugger.control_thread = nil
Debugger.start_control(options.host, options.port)
Debugger.start_control(options.host, options.port, options.notify_dispatcher)
end

if options.int_handler
Expand Down

0 comments on commit 4f39744

Please sign in to comment.