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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
use Symfony\Component\Mime\DependencyInjection\AddMimeTypeGuesserPass;
use Symfony\Component\PropertyInfo\DependencyInjection\PropertyInfoPass;
use Symfony\Component\Routing\DependencyInjection\RoutingResolverPass;
use Symfony\Component\Runtime\SymfonyRuntime;
use Symfony\Component\Serializer\DependencyInjection\SerializerPass;
use Symfony\Component\Translation\DependencyInjection\TranslationDumperPass;
use Symfony\Component\Translation\DependencyInjection\TranslationExtractorPass;
Expand Down Expand Up @@ -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.

}

if (\is_array($handler) && $handler[0] instanceof ErrorHandler) {
$handler[0]->throwAt($this->container->getParameter('debug.error_handler.throw_at'), true);
}

if ($this->container->getParameter('kernel.http_method_override')) {
Request::enableHttpMethodParameterOverride();
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"symfony/serializer": "<5.2",
"symfony/service-contracts": ">=3.0",
"symfony/security-csrf": "<5.3",
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ public function run(?InputInterface $input = null, ?OutputInterface $output = nu
}
}

$this->configureIO($input, $output);

try {
$this->configureIO($input, $output);

$exitCode = $this->doRun($input, $output);
} catch (\Exception $e) {
if (!$this->catchExceptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct(?callable $exceptionHandler = null, ?LoggerInterface
$deprecationLogger = $fileLinkFormat;
}

$handler = set_exception_handler('is_int');
$handler = set_exception_handler('var_dump');
$this->earlyHandler = \is_array($handler) ? $handler[0] : null;
restore_exception_handler();

Expand All @@ -84,7 +84,7 @@ public function configure(?object $event = null)
$this->firstCall = $this->hasTerminatedWithException = false;
$hasRun = null;

$handler = set_exception_handler('is_int');
$handler = set_exception_handler('var_dump');
$handler = \is_array($handler) ? $handler[0] : null;
restore_exception_handler();

Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/Runtime/GenericRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ public function __construct(array $options = [])
if ($debug) {
umask(0000);
$_SERVER[$debugKey] = $_ENV[$debugKey] = '1';

if (false !== $errorHandler = ($options['error_handler'] ?? BasicErrorHandler::class)) {
$errorHandler::register($debug);
$options['error_handler'] = false;
}
} 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

$errorHandler::register($debug);
$options['error_handler'] = false;
}

$this->options = $options;
}

Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Runtime/Internal/BasicErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public static function register(bool $debug): void
}

if (0 <= \ini_get('zend.assertions')) {
ini_set('zend.assertions', 1);
ini_set('assert.active', $debug);
ini_set('assert.exception', 1);
ini_set('zend.assertions', (int) $debug);
}
ini_set('assert.active', 1);
ini_set('assert.exception', 1);

set_error_handler(new self());
}
Expand Down
27 changes: 23 additions & 4 deletions src/Symfony/Component/Runtime/Internal/SymfonyErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,31 @@ class SymfonyErrorHandler
{
public static function register(bool $debug): void
{
BasicErrorHandler::register($debug);
if (!class_exists(ErrorHandler::class)) {
BasicErrorHandler::register($debug);

if (class_exists(ErrorHandler::class)) {
return;
}

error_reporting(-1);

if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
ini_set('display_errors', $debug);
} elseif (!filter_var(\ini_get('log_errors'), \FILTER_VALIDATE_BOOL) || \ini_get('error_log')) {
// CLI - display errors only if they're not already logged to STDERR
ini_set('display_errors', 1);
}

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.

}
ini_set('assert.active', 1);
ini_set('assert.exception', 1);

if ($debug) {
DebugClassLoader::enable();
restore_error_handler();
ErrorHandler::register(new ErrorHandler(new BufferingLogger(), $debug));
}

ErrorHandler::register(new ErrorHandler(new BufferingLogger(), $debug));
}
}
Loading