Skip to content

Commit

Permalink
MONGOID-5658 Fix callbacks for embedded
Browse files Browse the repository at this point in the history
  • Loading branch information
comandeo-mongo committed Oct 18, 2023
1 parent e52036b commit 2a62a8a
Show file tree
Hide file tree
Showing 4 changed files with 517 additions and 180 deletions.
13 changes: 13 additions & 0 deletions lib/mongoid/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@ module Config
end
end

# When this flag is true, callbacks for embedded documents will not be
# called. This is the default in 8.x, but will be changed to false in 9.0.
#
# Setting this flag to true (as it is in 8.x) may lead to stack
# overflow errors if there are more than cicrca 1000 embedded
# documents in the root document's dependencies graph.
#
# It is strongly recommended to set this flag to false in 8.x, if you
# are not using around callbacks for embedded documents.
#
# See https://jira.mongodb.org/browse/MONGOID-5658 for more details.
option :around_callbacks_for_embeds, default: true

# Has Mongoid been configured? This is checking that at least a valid
# client config exists.
#
Expand Down
141 changes: 122 additions & 19 deletions lib/mongoid/interceptable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def in_callback_state?(kind)
# @example Run only the after save callbacks.
# model.run_after_callbacks(:save)
#
# @param [ Array<Symbol> ] kinds The events that are occurring.
# @param [ Symbol... ] *kinds The events that are occurring.
#
# @return [ Object ] The result of the chain executing.
def run_after_callbacks(*kinds)
Expand All @@ -96,7 +96,7 @@ def run_after_callbacks(*kinds)
# @example Run only the before save callbacks.
# model.run_before_callbacks(:save, :create)
#
# @param [ Array<Symbol> ] kinds The events that are occurring.
# @param [ Symbol... ] *kinds The events that are occurring.
#
# @return [ Object ] The result of the chain executing.
def run_before_callbacks(*kinds)
Expand Down Expand Up @@ -134,36 +134,126 @@ def run_callbacks(kind, with_children: true, &block)
# Run the callbacks for embedded documents.
#
# @param [ Symbol ] kind The type of callback to execute.
# @param [ Array<Document> ] children Children to exeute callbacks on. If
# @param [ Array<Document> ] children Children to execute callbacks on. If
# nil, callbacks will be executed on all cascadable children of
# the document.
#
# @api private
def _mongoid_run_child_callbacks(kind, children: nil, &block)
if Mongoid::Config.around_callbacks_for_embeds
_mongoid_run_child_callbacks_with_around(kind, children: children, &block)
else
_mongoid_run_child_callbacks_without_around(kind, children: children, &block)
end
end

# Execute the callbacks of given kind for embedded documents including
# around callbacks.
#
# @note This method is prone to stack overflow errors if the document
# has a large number of embedded documents. It is recommended to avoid
# using around callbacks for embedded documents until a proper solution
# is implemented.
#
# @param [ Symbol ] kind The type of callback to execute.
# @param [ Array<Document> ] children Children to execute callbacks on. If
# nil, callbacks will be executed on all cascadable children of
# the document.
#
# @api private
def _mongoid_run_child_callbacks_with_around(kind, children: nil, &block)
child, *tail = (children || cascadable_children(kind))
if child.nil?
return block&.call
block&.call
elsif tail.empty?
return child.run_callbacks(child_callback_type(kind, child), &block)
child.run_callbacks(child_callback_type(kind, child), &block)
else
return child.run_callbacks(child_callback_type(kind, child)) do
_mongoid_run_child_callbacks(kind, children: tail, &block)
child.run_callbacks(child_callback_type(kind, child)) do
_mongoid_run_child_callbacks_with_around(kind, children: tail, &block)
end
end
end

# This is used to store callbacks to be executed later. A good use case for
# this is delaying the after_find and after_initialize callbacks until the
# associations are set on the document. This can also be used to delay
# applying the defaults on a document.
# Execute the callbacks of given kind for embedded documents without
# around callbacks.
#
# @param [ Symbol ] kind The type of callback to execute.
# @param [ Array<Document> ] children Children to execute callbacks on. If
# nil, callbacks will be executed on all cascadable children of
# the document.
#
# @api private
def _mongoid_run_child_callbacks_without_around(kind, children: nil, &block)
children = (children || cascadable_children(kind))
callback_list = _mongoid_run_child_before_callbacks(kind, children: children)
return false if callback_list == false
value = block&.call
callback_list.each do |_next_sequence, env|
env.value &&= value
end
return false if _mongoid_run_child_after_callbacks(callback_list: callback_list) == false

value
end

# Execute the before callbacks of given kind for embedded documents.
#
# @param [ Symbol ] kind The type of callback to execute.
# @param [ Array<Document> ] children Children to execute callbacks on.
# @param [ Array<ActiveSupport::Callbacks::CallbackSequence, ActiveSupport::Callbacks::Filters::Environment> ] callback_list List of
# pairs of callback sequence and environment. This list will be later used
# to execute after callbacks in reverse order.
#
# @return [ Array<Symbol> ] an array of symbols that represent the pending callbacks.
# @api private
def _mongoid_run_child_before_callbacks(kind, children: [], callback_list: [])
children.each do |child|
chain = child.__callbacks[child_callback_type(kind, child)]
env = ActiveSupport::Callbacks::Filters::Environment.new(child, false, nil)
next_sequence = compile_callbacks(chain)
unless next_sequence.final?
Mongoid.logger.warn("Around callbacks are disabled for embedded documents. Skipping around callbacks for #{child.class.name}.")
Mongoid.logger.warn("To enable around callbacks for embedded documents, set Mongoid::Config.around_callbacks_for_embeds to true.")
end
next_sequence.invoke_before(env)
return false if env.halted
env.value = !env.halted
callback_list << [next_sequence, env]
if (grandchildren = child.send(:cascadable_children, kind))
_mongoid_run_child_before_callbacks(kind, children: grandchildren, callback_list: callback_list)
end
end
callback_list
end

# Execute the after callbacks.
#
# @param [ Array<ActiveSupport::Callbacks::CallbackSequence, ActiveSupport::Callbacks::Filters::Environment> ] callback_list List of
# pairs of callback sequence and environment.
def _mongoid_run_child_after_callbacks(callback_list: [])
callback_list.reverse_each do |next_sequence, env|
next_sequence.invoke_after(env)
return false if env.halted
end
end

# Returns the stored callbacks to be executed later.
#
# @return [ Array<Symbol> ] Method symbols of the stored pending callbacks.
#
# @api private
def pending_callbacks
@pending_callbacks ||= [].to_set
end

# Stores callbacks to be executed later. A good use case for
# this is delaying the after_find and after_initialize callbacks until the
# associations are set on the document. This can also be used to delay
# applying the defaults on a document.
#
# @param [ Array<Symbol> ] value Method symbols of the pending callbacks to store.
#
# @return [ Array<Symbol> ] Method symbols of the stored pending callbacks.
#
# @api private
def pending_callbacks=(value)
@pending_callbacks = value
Expand Down Expand Up @@ -298,13 +388,7 @@ def run_targeted_callbacks(place, kind)
end
self.class.send :define_method, name do
env = ActiveSupport::Callbacks::Filters::Environment.new(self, false, nil)
sequence = if chain.method(:compile).arity == 0
# ActiveSupport < 7.1
chain.compile
else
# ActiveSupport >= 7.1
chain.compile(nil)
end
sequence = compile_callbacks(chain)
sequence.invoke_before(env)
env.value = !env.halted
sequence.invoke_after(env)
Expand All @@ -314,5 +398,24 @@ def run_targeted_callbacks(place, kind)
end
send(name)
end

# Compile the callback chain.
#
# This method hides the differences between ActiveSupport implementations
# before and after 7.1.
#
# @param [ ActiveSupport::Callbacks::CallbackChain ] chain The callback chain.
# @param [ Symbol | nil ] type The type of callback chain to compile.
#
# @return [ ActiveSupport::Callbacks::CallbackSequence ] The compiled callback sequence.
def compile_callbacks(chain, type = nil)
if chain.method(:compile).arity == 0
# ActiveSupport < 7.1
chain.compile
else
# ActiveSupport >= 7.1
chain.compile(type)
end
end
end
end
20 changes: 20 additions & 0 deletions spec/integration/callbacks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,24 @@ class PreviouslyPersistedPerson
expect(saved_person.previously_persisted_value).to be_truthy
end
end

context 'cascade callbacks' do
ruby_version_gte '3.0'
config_override :around_callbacks_for_embeds, false

let(:book) do
Book.new
end

before do
1500.times do
book.pages.build
end
end

# https://jira.mongodb.org/browse/MONGOID-5658
it 'does not raise SystemStackError' do
expect { book.save! }.not_to raise_error(SystemStackError)
end
end
end
Loading

0 comments on commit 2a62a8a

Please sign in to comment.