@@ -386,27 +386,29 @@ def max_compact_name_size
386
386
387
387
def inspect_with_allocation_control ( slice , memory_limit )
388
388
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 |
396
393
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
406
408
rescue MemoryLimitError => e
407
409
print_debug ( e . message )
408
410
return nil
409
- end
411
+ end
410
412
411
413
def compact_array_str ( value )
412
414
slice = value [ 0 ..10 ]
@@ -420,7 +422,7 @@ def compact_array_str(value)
420
422
if compact && value . size != slice . size
421
423
compact [ 0 ..compact . size -2 ] + ", ...]"
422
424
end
423
- compact
425
+ compact
424
426
end
425
427
426
428
def compact_hash_str ( value )
0 commit comments