@@ -386,27 +386,29 @@ def max_compact_name_size
386386
387387 def inspect_with_allocation_control ( slice , memory_limit )
388388 curr_thread = Thread . current
389-
390- start_alloc_size = ObjectSpace . memsize_of_all
391-
392- trace = TracePoint . new ( :c_call , :call ) do |tp |
393- curr_alloc_size = ObjectSpace . memsize_of_all
394-
395- if ( curr_alloc_size - start_alloc_size > 1e6 * memory_limit )
389+ result = nil
390+ inspect_thread = DebugThread . start {
391+ start_alloc_size = ObjectSpace . memsize_of_all
392+ trace = TracePoint . new ( :c_call , :call ) do |tp |
396393
397- trace . disable
398- curr_thread . raise MemoryLimitError , "Out of memory: evaluation of inspect took more than #{ memory_limit } mb." if curr_thread . alive?
399- end
400- end
401-
402- trace . enable
403- result = slice . inspect
404- trace . disable
405- result
394+ curr_alloc_size = ObjectSpace . memsize_of_all
395+ start_alloc_size = curr_alloc_size if ( curr_alloc_size < start_alloc_size )
396+
397+ if ( curr_alloc_size - start_alloc_size > 1e6 * memory_limit )
398+ curr_thread . raise MemoryLimitError , "Out of memory: evaluation of inspect took more than #{ memory_limit } mb. \n #{ caller . map { |l | "\t #{ l } " } . join ( "\n " ) } "
399+ trace . disable
400+ end
401+ end . enable {
402+ result = slice . inspect
403+ }
404+ }
405+ inspect_thread . join
406+ inspect_thread . kill
407+ return result
406408 rescue MemoryLimitError => e
407409 print_debug ( e . message )
408410 return nil
409- end
411+ end
410412
411413 def compact_array_str ( value )
412414 slice = value [ 0 ..10 ]
@@ -420,7 +422,7 @@ def compact_array_str(value)
420422 if compact && value . size != slice . size
421423 compact [ 0 ..compact . size -2 ] + ", ...]"
422424 end
423- compact
425+ compact
424426 end
425427
426428 def compact_hash_str ( value )
0 commit comments