Skip to content

Commit

Permalink
Use AS::Dependencies interlock with classic autoloader
Browse files Browse the repository at this point in the history
With zeitwerk, we actually get deadlocks with the use of the interlock.
This change allows us to use the interlock on master when using the classic autoloader and skip the interlock when using zeitwerk.

#22000
  • Loading branch information
jrafanie committed Jun 2, 2023
1 parent 7f2dd2f commit d311bed
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/extensions/as_dependencies_interlock.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module AsDependenciesInterlock
# The classic autoloader needs the ActiveSupport::Dependencies.interlock
# to avoid deadlocks. With zeitwerk, this actually causes deadlocks so
# we only use the interlock with the classic autoloader.
def loading(&block)
if Rails.application.config.autoloader == :zeitwerk
warn_about_zeitwerk_and_interlock
yield
else
super
end
end

def permit_concurrent_loads(&block)
if Rails.application.config.autoloader == :zeitwerk
warn_about_zeitwerk_and_interlock
yield
else
super
end
end

def warn_about_zeitwerk_and_interlock
@warn_about_zeitwerk_and_interlock ||= begin
warn ":zeitwerk is the configured autoloader. This patched file should be removed: #{__FILE__} once support for the classic loader is dropped. Additionally remove all uses of AS::Depedencies.interlock."
true
end
end
end

ActiveSupport::Dependencies.interlock.singleton_class.prepend(AsDependenciesInterlock)

0 comments on commit d311bed

Please sign in to comment.