1818module  Debugger 
1919
2020  class  << self 
21+     def  find_free_port ( host ) 
22+       server  =  TCPServer . open ( host ,  0 ) 
23+       port    =  server . addr [ 1 ] 
24+       server . close 
25+       port 
26+     end 
27+ 
2128    # Prints to the stderr using printf(*args) if debug logging flag (-d) is on. 
2229    def  print_debug ( *args ) 
2330      if  Debugger . cli_debug 
@@ -111,9 +118,15 @@ def start_control(host, port, notify_dispatcher)
111118          # 127.0.0.1 seemingly works with all systems and with IPv6 as well. 
112119          # "localhost" and nil have problems on some systems. 
113120          host  ||= '127.0.0.1' 
114-           server  =  TCPServer . new ( host ,  port ) 
115-           print_greeting_msg ( $stderr,  host ,  port )  if  defined?  IDE_VERSION 
116-           notify_dispatcher ( port )  if  notify_dispatcher 
121+ 
122+           server  =  notify_dispatcher_if_needed ( host ,  port ,  notify_dispatcher )  do  |real_port ,  port_changed  =  false |
123+             s  =  TCPServer . new ( host ,  real_port ) 
124+             print_greeting_msg  $stderr,  host ,  real_port ,  port_changed  ? "Subprocess"  : "Fast"  if  defined?  IDE_VERSION 
125+             s 
126+           end 
127+ 
128+           return  unless  server 
129+ 
117130          while  ( session  =  server . accept ) 
118131            $stderr. puts  "Connected from #{ session . peeraddr [ 2 ] }  "  if  Debugger . cli_debug 
119132            dispatcher  =  ENV [ 'IDE_PROCESS_DISPATCHER' ] 
@@ -141,8 +154,9 @@ def start_control(host, port, notify_dispatcher)
141154
142155    private 
143156
157+     def  notify_dispatcher_if_needed ( host ,  port ,  need_notify ) 
158+       return  yield  port  unless  need_notify 
144159
145-     def  notify_dispatcher ( port ) 
146160      return  unless  ENV [ 'IDE_PROCESS_DISPATCHER' ] 
147161      acceptor_host ,  acceptor_port  =  ENV [ 'IDE_PROCESS_DISPATCHER' ] . split ( ":" ) 
148162      acceptor_host ,  acceptor_port  =  '127.0.0.1' ,  acceptor_host  unless  acceptor_port 
@@ -151,11 +165,19 @@ def notify_dispatcher(port)
151165      3 . times  do  |i |
152166        begin 
153167          s  =  TCPSocket . open ( acceptor_host ,  acceptor_port ) 
168+           dispatcher_answer  =  s . gets . chomp 
169+ 
170+           if  dispatcher_answer  == "true" 
171+             port  =  Debugger . find_free_port ( host ) 
172+           end 
173+ 
174+           server  =  yield  port ,  dispatcher_answer  == "true" 
175+ 
154176          s . print ( port ) 
155177          s . close 
156178          connected  =  true 
157179          print_debug  "Ide process dispatcher notified about sub-debugger which listens on #{ port } \n " 
158-           return 
180+           return   server 
159181        rescue  =>  bt 
160182          $stderr. puts  "#{ Process . pid }  : connection failed(#{ i +1 }  )" 
161183          $stderr. puts  "Exception: #{ bt }  " 
@@ -164,7 +186,6 @@ def notify_dispatcher(port)
164186        end  unless  connected 
165187      end 
166188    end 
167- 
168189  end 
169190
170191  class  Exception  # :nodoc: 
0 commit comments