From bf309f79c23347b5aff30cfcb52ce1720a03ea6c Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Wed, 2 Aug 2023 20:46:38 +0200 Subject: [PATCH] Let on_dir_autoloaded be reentrant --- lib/zeitwerk/loader.rb | 12 +++++++----- lib/zeitwerk/loader/callbacks.rb | 2 +- test/lib/zeitwerk/test_on_load.rb | 11 +++++++++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/zeitwerk/loader.rb b/lib/zeitwerk/loader.rb index 6554affb..1fbdbdac 100644 --- a/lib/zeitwerk/loader.rb +++ b/lib/zeitwerk/loader.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require "monitor" require "set" module Zeitwerk @@ -91,9 +92,9 @@ class Loader attr_reader :mutex private :mutex - # @sig Mutex - attr_reader :mutex2 - private :mutex2 + # @sig Monitor + attr_reader :dirs_autoload_monitor + private :dirs_autoload_monitor def initialize super @@ -103,11 +104,12 @@ def initialize @to_unload = {} @namespace_dirs = Hash.new { |h, cpath| h[cpath] = [] } @shadowed_files = Set.new - @mutex = Mutex.new - @mutex2 = Mutex.new @setup = false @eager_loaded = false + @mutex = Mutex.new + @dirs_autoload_monitor = Monitor.new + Registry.register_loader(self) end diff --git a/lib/zeitwerk/loader/callbacks.rb b/lib/zeitwerk/loader/callbacks.rb index fc91d963..17b97ce0 100644 --- a/lib/zeitwerk/loader/callbacks.rb +++ b/lib/zeitwerk/loader/callbacks.rb @@ -44,7 +44,7 @@ def on_dir_autoloaded(dir) # That not only would reassign the constant (undesirable per se) but, worse, # the module object created by t2 wouldn't have any of the autoloads for its # children, since t1 would have correctly deleted its namespace_dirs entry. - mutex2.synchronize do + dirs_autoload_monitor.synchronize do if cref = autoloads.delete(dir) autovivified_module = cref[0].const_set(cref[1], Module.new) cpath = autovivified_module.name diff --git a/test/lib/zeitwerk/test_on_load.rb b/test/lib/zeitwerk/test_on_load.rb index 3b84ca6b..1c6e5e93 100644 --- a/test/lib/zeitwerk/test_on_load.rb +++ b/test/lib/zeitwerk/test_on_load.rb @@ -191,4 +191,15 @@ class TestOnLoad < LoaderTest assert loader.send(:on_load_callbacks).empty? end end + + test "on_load is reentrant for implicit namespaces" do + files = [["m/x.rb", "M::X = true"], ["n/x.rb", "N::X = true"]] + with_setup(files) do + loaded = false + loader.on_load("M") { loaded = N::X } + + assert M + assert loaded + end + end end