@@ -13,25 +13,21 @@ module OverflowMessageType
1313 SPECIAL_SYMBOL_MESSAGE = lambda { |e | '<?>' }
1414 end
1515
16- class MemoryLimitError < StandardError
16+ class ExecError < StandardError
1717 attr_reader :message
18+ attr_reader :trace_point
1819 attr_reader :backtrace
1920
20- def initialize ( message , backtrace = [ ] )
21+ def initialize ( message , trace_point , backtrace = [ ] )
2122 @message = message
23+ @trace_point = trace_point
2224 @backtrace = backtrace
2325 end
2426 end
2527
26- class TimeLimitError < StandardError
27- attr_reader :message
28- attr_reader :backtrace
28+ class MemoryLimitError < ExecError ; end
2929
30- def initialize ( message , backtrace = [ ] )
31- @message = message
32- @backtrace = backtrace
33- end
34- end
30+ class TimeLimitError < ExecError ; end
3531
3632 class XmlPrinter # :nodoc:
3733 class ExceptionProxy
@@ -170,47 +166,45 @@ def print_string(string)
170166 def exec_with_allocation_control ( value , memory_limit , time_limit , exec_method , overflow_message_type )
171167 return value . send exec_method if ( RUBY_VERSION < '2.0' )
172168
173- check_memory_limit = true
174- if ( defined? ( JRUBY_VERSION ) || ENV [ 'DEBUGGER_MEMORY_LIMIT' ] . to_i <= 0 )
175- check_memory_limit = false
176- end
169+ check_memory_limit = !defined? ( JRUBY_VERSION ) && ENV [ 'DEBUGGER_MEMORY_LIMIT' ] . to_i > 0
177170 curr_thread = Thread . current
178- result = nil
179- inspect_thread = DebugThread . start {
180171
172+ result = nil
173+ inspect_thread = DebugThread . start do
181174 start_alloc_size = ObjectSpace . memsize_of_all if ( check_memory_limit )
182175 start_time = Time . now . to_f
183176
184- trace = TracePoint . new ( :c_call , :call ) do |tp |
177+ trace_point = TracePoint . new ( :c_call , :call ) do | |
178+ next unless Thread . current == inspect_thread
179+ next unless rand > 0.75
185180
186- if ( rand > 0.75 )
187- curr_time = Time . now . to_f
181+ curr_time = Time . now . to_f
188182
189- if ( ( curr_time - start_time ) * 1e3 > time_limit )
190- curr_thread . raise TimeLimitError . new ( "Timeout: evaluation of #{ exec_method } took longer than #{ time_limit } ms." , caller . to_a )
191- inspect_thread . kill
192- end
183+ if ( curr_time - start_time ) * 1e3 > time_limit
184+ curr_thread . raise TimeLimitError . new ( "Timeout: evaluation of #{ exec_method } took longer than #{ time_limit } ms." , caller . to_a , trace_point )
185+ end
193186
194- if ( check_memory_limit )
195- curr_alloc_size = ObjectSpace . memsize_of_all
196- start_alloc_size = curr_alloc_size if ( curr_alloc_size < start_alloc_size )
187+ if check_memory_limit
188+ curr_alloc_size = ObjectSpace . memsize_of_all
189+ start_alloc_size = curr_alloc_size if ( curr_alloc_size < start_alloc_size )
197190
198- if ( curr_alloc_size - start_alloc_size > 1e6 * memory_limit )
199- curr_thread . raise MemoryLimitError . new ( "Out of memory: evaluation of #{ exec_method } took more than #{ memory_limit } mb." , caller . to_a )
200- inspect_thread . kill
201- end
191+ if curr_alloc_size - start_alloc_size > 1e6 * memory_limit
192+ curr_thread . raise MemoryLimitError . new ( "Out of memory: evaluation of #{ exec_method } took more than #{ memory_limit } mb." , caller . to_a , trace_point )
202193 end
203194 end
204- end . enable {
205- result = value . send exec_method
206- }
207- }
195+ end
196+ trace_point . enable
197+ result = value . send exec_method
198+ trace_point . disable
199+ end
208200 inspect_thread . join
209- inspect_thread . kill
210201 return result
211- rescue MemoryLimitError , TimeLimitError => e
212- print_debug ( e . message + "\n " + e . backtrace . map { |l | "\t #{ l } " } . join ( "\n " ) )
202+ rescue ExecError => e
203+ e . trace_point . disable
204+ print_debug ( e . message + "\n " + e . backtrace . map { |l | "\t #{ l } " } . join ( "\n " ) )
213205 return overflow_message_type . call ( e )
206+ ensure
207+ inspect_thread . kill if inspect_thread && inspect_thread . alive?
214208 end
215209
216210 def print_variable ( name , value , kind )
@@ -514,4 +508,4 @@ def build_value_attr(escaped_value_str)
514508
515509 end
516510
517- end
511+ end
0 commit comments