提交 f0528a54 编写于 作者: M Matthew Draper

Prevent nested ExecutionWrapper calls even when using run! directly

上级 2c53e279
...@@ -4,6 +4,10 @@ module ActiveSupport ...@@ -4,6 +4,10 @@ module ActiveSupport
class ExecutionWrapper class ExecutionWrapper
include ActiveSupport::Callbacks include ActiveSupport::Callbacks
Null = Object.new # :nodoc:
def Null.complete! # :nodoc:
end
define_callbacks :run define_callbacks :run
define_callbacks :complete define_callbacks :complete
...@@ -22,7 +26,11 @@ def self.to_complete(*args, &block) ...@@ -22,7 +26,11 @@ def self.to_complete(*args, &block)
# #
# Where possible, prefer +wrap+. # Where possible, prefer +wrap+.
def self.run! def self.run!
new.tap(&:run!) if active?
Null
else
new.tap(&:run!)
end
end end
# Perform the work in the supplied block as an execution. # Perform the work in the supplied block as an execution.
...@@ -43,17 +51,17 @@ class << self # :nodoc: ...@@ -43,17 +51,17 @@ class << self # :nodoc:
def self.inherited(other) # :nodoc: def self.inherited(other) # :nodoc:
super super
other.active = Concurrent::Hash.new(0) other.active = Concurrent::Hash.new
end end
self.active = Concurrent::Hash.new(0) self.active = Concurrent::Hash.new
def self.active? # :nodoc: def self.active? # :nodoc:
@active[Thread.current] > 0 @active[Thread.current]
end end
def run! # :nodoc: def run! # :nodoc:
self.class.active[Thread.current] += 1 self.class.active[Thread.current] = true
run_callbacks(:run) run_callbacks(:run)
end end
...@@ -63,7 +71,8 @@ def run! # :nodoc: ...@@ -63,7 +71,8 @@ def run! # :nodoc:
# Where possible, prefer +wrap+. # Where possible, prefer +wrap+.
def complete! def complete!
run_callbacks(:complete) run_callbacks(:complete)
self.class.active.delete Thread.current if (self.class.active[Thread.current] -= 1) == 0 ensure
self.class.active.delete Thread.current
end end
end end
end end
...@@ -22,8 +22,6 @@ module ActiveSupport ...@@ -22,8 +22,6 @@ module ActiveSupport
# unloaded. # unloaded.
# #
class Reloader < ExecutionWrapper class Reloader < ExecutionWrapper
Null = Class.new(ExecutionWrapper) # :nodoc:
define_callbacks :prepare define_callbacks :prepare
define_callbacks :class_unload define_callbacks :class_unload
...@@ -54,7 +52,7 @@ def self.run! # :nodoc: ...@@ -54,7 +52,7 @@ def self.run! # :nodoc:
if check! if check!
super super
else else
Null.run! Null
end end
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册