Skip to content

Tweak error/exception handler registration #58372

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

Merged
merged 1 commit into from
Sep 25, 2024

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented Sep 24, 2024

Q A
Branch? 5.4
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix #53812
License MIT

This should allow removing the custom bootstrap file from #58370 /cc @xabbuh

The change on FrameworkBundle leads to a tweaked behavior: we don't override the previous error handler in case it's not the Symfony one. This shouldn't change anything in practice since the error handler is already registered by the runtime component.

The rest is closer to bug fixes.

@carsonbot carsonbot added this to the 7.2 milestone Sep 24, 2024
@xabbuh
Copy link
Member

xabbuh commented Sep 24, 2024

@derrabus FYI as you used the same workaround for Twig

@nicolas-grekas
Copy link
Member Author

I'm wondering about merging this in 6.4 🤔

@nicolas-grekas nicolas-grekas modified the milestones: 7.2, 6.4 Sep 24, 2024
@nicolas-grekas nicolas-grekas changed the base branch from 7.2 to 6.4 September 24, 2024 13:41
@nicolas-grekas
Copy link
Member Author

Rebased on 6.4

Comment on lines 107 to 108
$exceptionHandler = set_exception_handler('var_dump');
restore_exception_handler();
Copy link
Member

Choose a reason for hiding this comment

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

We need a PHP RFC to get get_exception_handler() 👀

@derrabus
Copy link
Member

Thank you. I'm a bit reluctant to merge this as a bugfix because of the change in behavior though.

@nicolas-grekas nicolas-grekas modified the milestones: 6.4, 7.2 Sep 25, 2024
@nicolas-grekas nicolas-grekas changed the base branch from 6.4 to 7.2 September 25, 2024 08:08
@derrabus
Copy link
Member

Then again, not having this change on 6.4 means we need to keep those bootstrap files for low-deps testing. Difficult decision. 🙈

@nicolas-grekas nicolas-grekas changed the base branch from 7.2 to 5.4 September 25, 2024 08:42
@nicolas-grekas nicolas-grekas modified the milestones: 7.2, 5.4 Sep 25, 2024
@nicolas-grekas nicolas-grekas force-pushed the tweak-error-handler-reg branch 2 times, most recently from 2775afb to 97c08ad Compare September 25, 2024 09:15
Copy link
Member Author

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

I rebased this on 5.4 and did a fix on symfony/runtime so that we have simpler setup of the error-handler register.
This should unlock moving to phpunit11 (deps=low included)

@@ -91,7 +92,16 @@ class FrameworkBundle extends Bundle
{
public function boot()
{
ErrorHandler::register(null, false)->throwAt($this->container->getParameter('debug.error_handler.throw_at'), true);
if (class_exists(SymfonyRuntime::class)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

When symfony/runtime is installed, we assume it will register the ErrorHandler when appropriate.
That's not the case right now, but this PR fixes it.

$handler = set_error_handler('var_dump');
restore_error_handler();
} else {
$handler = [ErrorHandler::register(null, false)];
Copy link
Member Author

Choose a reason for hiding this comment

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

When symfony/runtime is not found, the index.php file registers ErrorHandler only in debug mode, so we need to register it to not break prod mode.

Copy link
Member

Choose a reason for hiding this comment

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

shouldn't we check whether it is already registered ? Maybe some devs have changed the index.php file.

And wouldn't this still cause issues with PHPUnit 11 then when booting a kernel during tests ?

Copy link
Member Author

@nicolas-grekas nicolas-grekas Sep 25, 2024

Choose a reason for hiding this comment

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

The false argument is here to skip registering if ErrorHandler is already registered.
This line preserves the current behavior when symfoy/runtime isn't installed so I wouldn't add more logic here.

@@ -86,6 +86,7 @@
"symfony/mime": "<4.4",
"symfony/property-info": "<4.4",
"symfony/property-access": "<5.3",
"symfony/runtime": "<5.4.45|>=6.0,<6.4.13|>=7.0,7.1.6",
Copy link
Member Author

Choose a reason for hiding this comment

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

ensures that we symfony/runtime is installed, it's a version that always registers ErrorHandler

} else {
$_SERVER[$debugKey] = $_ENV[$debugKey] = '0';
}

if (false !== $errorHandler = ($options['error_handler'] ?? BasicErrorHandler::class)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

untightening the error_handler option from the debug one

@xabbuh
Copy link
Member

xabbuh commented Sep 25, 2024

The test failures are related, aren't they?

@nicolas-grekas nicolas-grekas force-pushed the tweak-error-handler-reg branch 2 times, most recently from 704e252 to 6d0b67d Compare September 25, 2024 13:30
@nicolas-grekas
Copy link
Member Author

The test failures are related, aren't they?

yes they are, this line is deprecated indeed when $debug = false: ini_set('assert.active', $debug);

PR updated

@nicolas-grekas nicolas-grekas merged commit de1a6ac into symfony:5.4 Sep 25, 2024
11 of 12 checks passed
@nicolas-grekas nicolas-grekas deleted the tweak-error-handler-reg branch October 24, 2024 14:18
This was referenced Oct 27, 2024
}

if (0 <= \ini_get('zend.assertions')) {
ini_set('zend.assertions', (int) $debug);

Choose a reason for hiding this comment

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

@nicolas-grekas why are you disabling assertions in non-debug mode ? We are relying on assertions in production mode and this change breaks our application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Framework bundle] Cleanup error and exception handlers on Kernel shutdown
7 participants