From 22d5dfb503ae7e16a0c368d6c648f540a3d23d7f Mon Sep 17 00:00:00 2001 From: Jonathan Hefner Date: Thu, 20 Jan 2022 17:39:15 -0600 Subject: [PATCH] Fix flakey EventedFileUpdateChecker GC test Example failure: https://buildkite.com/rails/rails/builds/84109#4a72a57f-20d3-44e1-b0a1-61c6a0baf3b3/1191-1201 Similar to #42845, this should ensure that there are no transient references to the `EventedFileUpdateChecker`, and that Ruby performs a full garbage collection cycle. --- .../test/evented_file_update_checker_test.rb | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/activesupport/test/evented_file_update_checker_test.rb b/activesupport/test/evented_file_update_checker_test.rb index 650464fb64..edbb860e87 100644 --- a/activesupport/test/evented_file_update_checker_test.rb +++ b/activesupport/test/evented_file_update_checker_test.rb @@ -85,14 +85,23 @@ class EventedFileUpdateCheckerTest < ActiveSupport::TestCase end test "can be garbage collected" do - previous_threads = Thread.list - checker_ref = WeakRef.new(ActiveSupport::EventedFileUpdateChecker.new([], tmpdir => ".rb") { }) - listener_threads = Thread.list - previous_threads + # Use a separate thread to isolate objects and ensure they will be garbage collected. + checker_ref, listener_threads = Thread.new do + threads_before_checker = Thread.list + checker = ActiveSupport::EventedFileUpdateChecker.new([], tmpdir => ".rb") { } - wait # Wait for listener thread to start processing events. - GC.start + # Wait for listener thread to start processing events. + wait - assert_not_predicate checker_ref, :weakref_alive? + [WeakRef.new(checker), Thread.list - threads_before_checker] + end.value + + # Calling `GC.start` 4 times should trigger a full GC run. + 4.times do + GC.start + end + + assert_not checker_ref.weakref_alive?, "EventedFileUpdateChecker was not garbage collected" assert_empty Thread.list & listener_threads end