Skip to content

Commit

Permalink
MONGOID-5658 Fix callbacks for embedded (mongodb#5727)
Browse files Browse the repository at this point in the history
  • Loading branch information
comandeo-mongo committed Oct 17, 2023
1 parent c12a285 commit 4d134f2
Show file tree
Hide file tree
Showing 4 changed files with 517 additions and 166 deletions.
13 changes: 13 additions & 0 deletions lib/mongoid/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,19 @@ module Config
# See https://jira.mongodb.org/browse/MONGOID-5542
option :prevent_multiple_calls_of_embedded_callbacks, default: false

# 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

# Returns the Config singleton, for use in the configure DSL.
#
# @return [ self ] The Config singleton.
Expand Down
132 changes: 119 additions & 13 deletions lib/mongoid/interceptable.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true
# rubocop:todo all

module Mongoid

Expand Down Expand Up @@ -43,6 +44,8 @@ module Interceptable
# @api private
define_model_callbacks :persist_parent

define_model_callbacks :commit, :rollback, only: :after

attr_accessor :before_callback_halted
end

Expand Down Expand Up @@ -140,6 +143,28 @@ def run_callbacks(kind, with_children: true, &block)
#
# @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))
with_children = !Mongoid::Config.prevent_multiple_calls_of_embedded_callbacks
if child.nil?
Expand All @@ -148,23 +173,91 @@ def _mongoid_run_child_callbacks(kind, children: nil, &block)
child.run_callbacks(child_callback_type(kind, child), with_children: with_children, &block)
else
child.run_callbacks(child_callback_type(kind, child), with_children: with_children) do
_mongoid_run_child_callbacks(kind, children: tail, &block)
_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.
#
# @return [ Array<Symbol> ] an array of symbols that represent the pending 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.
#
# @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 @@ -299,13 +392,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 @@ -315,5 +402,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
21 changes: 21 additions & 0 deletions spec/integration/callbacks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ def will_save_change_to_attribute_values_before

context 'nested embedded documents' do
config_override :prevent_multiple_calls_of_embedded_callbacks, true
config_override :around_callbacks_for_embeds, true

let(:logger) { Array.new }

Expand All @@ -581,4 +582,24 @@ def will_save_change_to_attribute_values_before
expect(logger).to eq(%i[embedded_twice embedded_once root])
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 4d134f2

Please sign in to comment.