Skip to content

Commit 96e695a

Browse files
Fix flakiness in TestGc#test_thrashing_for_young_objects
I caught a reproduction of this test failing under rr, and was able to replay it to isolate the failure. The call to `before_stat_heap = GC.stat_heap` is itself allocating a hash, which in unlucky circumstances can result in a new page being allocated and thus `before_stats[:heap_allocated_pages]` no longer equals `after_stats[:heap_allocated_pages]`. The solution is to use the form of GC.stat/stat_heap which takes a hash as an argument, and thus needs to perform no Ruby allocations itself.
1 parent 261f5d3 commit 96e695a

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

test/ruby/test_gc.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -636,17 +636,28 @@ def test_thrashing_for_young_objects
636636
# Warmup to make sure heap stabilizes
637637
1_000_000.times { Object.new }
638638
639-
before_stats = GC.stat
639+
# We need to pre-allocate all the hashes for GC.stat calls, because
640+
# otherwise the call to GC.stat/GC.stat_heap itself could cause a new
641+
# page to be allocated and the before/after assertions will fail
642+
before_stats = {}
643+
after_stats = {}
644+
# stat_heap needs a hash of hashes for each heap; easiest way to get the
645+
# right shape for that is just to call stat_heap with no argument
640646
before_stat_heap = GC.stat_heap
647+
after_stat_heap = GC.stat_heap
648+
649+
# Now collect the actual stats
650+
GC.stat before_stats
651+
GC.stat_heap nil, before_stat_heap
641652
642653
1_000_000.times { Object.new }
643654
644655
# Previous loop may have caused GC to be in an intermediate state,
645656
# running a minor GC here will guarantee that GC will be complete
646657
GC.start(full_mark: false)
647658
648-
after_stats = GC.stat
649-
after_stat_heap = GC.stat_heap
659+
GC.stat after_stats
660+
GC.stat_heap nil, after_stat_heap
650661
651662
# Debugging output to for failures in trunk-repeat50@phosphorus-docker
652663
debug_msg = "before_stats: #{before_stats}\nbefore_stat_heap: #{before_stat_heap}\nafter_stats: #{after_stats}\nafter_stat_heap: #{after_stat_heap}"

0 commit comments

Comments
 (0)