Skip to content

Conversation

@willrowe
Copy link
Contributor

@willrowe willrowe commented May 8, 2025

This PR does three things:

  1. Changes booted to only be set after a model class has finished booting, as its name implies.
  2. Adds a new booting flag that is set to true when booting first begins and is removed when booting has finished.
  3. Throws an exception if 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 the boot* method in a trait, the instance is created, but in an inconsistent state. This happens because the second call to bootIfNotBooted skips the boot process because booted is marked as true 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.

@willrowe willrowe force-pushed the feature/disallow-model-instance-during-boot branch from 4c0ce5b to 57207f7 Compare May 8, 2025 21:03
willrowe added 4 commits May 8, 2025 17:04
- A model class is removed from the `booting` array as soon as the boot process has completed.
- This infers that something in the boot process expected the boot to already be finished.
- If we just ignore the call then the model class would be in an inconsistent state for whichever code called it.
@willrowe willrowe force-pushed the feature/disallow-model-instance-during-boot branch from 57207f7 to ad7a45d Compare May 8, 2025 21:04
if (! isset(static::$booted[static::class])) {
static::$booted[static::class] = true;
if (isset(static::$booting[static::class])) {
throw new Exception('"'.__METHOD__.'" cannot be called on the "'.static::class.'" class while it is already being booted.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be a LogicException?

Exception that represents error in the program logic. This kind of exception should lead directly to a fix in your code.

https://www.php.net/manual/en/class.logicexception.php

@taylorotwell taylorotwell merged commit 7672871 into laravel:master May 10, 2025
50 checks passed
@willrowe willrowe deleted the feature/disallow-model-instance-during-boot branch May 12, 2025 13:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants