-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.1] Make sure unguarded() does not change unguarded state on exception #13186
Conversation
$result = $callback(); | ||
} finally { | ||
static::reguard(); | ||
} | ||
|
||
return $result; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this now be undefined if an exception is thrown and $result is never set?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeh, this code is broken atm.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change it to:
try {
return $callback();
} finally {
static::reguard();
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it does not matter. When the callback throws an exception, this method throws that exception and thus does not return anything. A method either returns or throws an exception. Not both. Or am I wrong now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that is true, but writing the return inside the try like that is definitely preferred please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, that looks indeed better. I will change it a.s.a.p.
Updated as proposed by @GrahamCampbell: moved the return statement into the try block. |
👍 |
I don't like But that's just me. |
Yeah.. It's subjective. |
This makes sure that when an exception is thrown in the callback passed to
Model::unguarded()
, the Model is still guarded afterwards. It uses thetry .. finally
construct. I also added a unit test.A use case when this can happen is in
FactoryBuilder::makeInstance
, which throws an exception when it cannot find the model factory. In tests, this can result in strange errors in other tests, because all models are unguarded afterwards (also in other test cases) when this exception is thrown. So then tests will fail when they shouldn't. This PR prevents this kind of situations.