[13.x] Do not allow new model instances to be created during boot #55685
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR does three things:
booted
to only be set after a model class has finished booting, as its name implies.booting
flag that is set totrue
when booting first begins and is removed when booting has finished.bootIfNotBooted
is called while currently booting, since this implies nested booting.These changes allow issues with nested booting to be caught immediately and would have helped track down this very tricky bug much sooner.
Right now, if you create a new instance of a model while booting, whether that is in the
boot
method for a model itself or theboot*
method in a trait, the instance is created, but in an inconsistent state. This happens because the second call tobootIfNotBooted
skips the boot process becausebooted
is marked astrue
when starting to boot. This thankfully avoids a recursive stack overflow, however, it also means that the model class has not been completely booted yet and causing the inconsistent state. Anything done with the returned model instance then has the potential to work improperly, as was seen in #55286.This is a breaking change since existing code may be creating new model instances while booting, which would then trigger an exception where it did not before.