8000 Type error results in zero exit code starting with PHP 8.3.0 · Issue #53946 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

Type error results in zero exit code starting with PHP 8.3.0 #53946

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

Closed
greg0ire opened this issue Feb 14, 2024 · 7 comments
Closed

Type error results in zero exit code starting with PHP 8.3.0 #53946

greg0ire opened this issue Feb 14, 2024 · 7 comments

Comments

@greg0ire
Copy link
Contributor
greg0ire commented Feb 14, 2024

Symfony version(s) affected

7.0.3

Description

When a type error occurs in a Symfony app that is run with the CLI SAPI, the exit code is zero.

How to reproduce

  • paste this in a Symfony command, at the beginning of the execute method, it should result in a type error: (function (int $toto): void { echo $toto; })('toto');
  • run that command
  • run echo $? to get the exit code

You should see the type error, and you will also see that it exits with zero, when I would expect it to exit with anything else, just like for exceptions.

Possible Solution

Deprecate not calling setCatchErrors() in the next minor
Change this default value and remove the deprecation in the next major version:

private bool $catchErrors = false;

Additional Context

I cannot reproduce that issue with throw new \Exception, so maybe it only affects errors.
A workaround is to add $application->setCatchErrors(true);.

@ghost
Copy link
ghost commented Feb 14, 2024

How did it work before? I believe that before #50420 type error also didn't result in non-zero code. If so then your issue is more like a feature rather than bug.

@greg0ire
Copy link
Contributor Author
greg0ire commented Feb 14, 2024

I believe that before #50420 type error also didn't result in non-zero code.

That sounds a lot like a bug, and I think the root cause is this line:

$exitCode = $e->getCode();

It was introduced right from the beginning: 6feaecf#diff-604e88acd1f392ef7bd3ba584f8ddf736bee74777ed59cce245e387a36183fb1R119

I wonder what the rationale behind doing this is, I mean, the default value is 0: https://3v4l.org/cUGg9

On my machine, PHP exits with a 255 exit code when I throw an exception, so if anything, it feels like Symfony silences the error 😬

These lines are supposed to set it back to 1:

if ($exitCode <= 0) {
$exitCode = 1;
}

@greg0ire
Copy link
Contributor Author
greg0ire commented Feb 15, 2024

New suspect that agrees with @derrabus ' tagging: the error handler component. Here is a reproducer:

<?php

use Symfony\Component\ErrorHandler\ErrorHandler;

require_once 'vendor/autoload.php';

ErrorHandler::register();

(function (int $toto): void { echo $toto; })('toto');

@llupa
Copy link
Contributor
llupa commented Feb 20, 2024

This problem just caused a lot of confusion for some post deployment commands we have in our K8s cluster. A prod image made it into a dev env (due to tagging name collision) and all commands complained that require-dev classes were not found, but the exit code was 0 anyway and the pod went live.

Adding the following, if others encounter this too, from a slack thread, thanks to @wouterj, will help:

// in your bin/console change:

return new Application($kernel);

// to

$app = new Application($kernel);
$app->setCatchErrors(true);

return $app;

@greg0ire greg0ire changed the title Type error result in zero exit code Type error results in zero exit code Feb 20, 2024
@greg0ire
Copy link
Contributor Author

My reproducer above does not seem to always work. I think depending on the php configuration, but I'm really not sure. I discarded the environment because the error handler does not seem to pull much from the env, at least not through getenv. When the issue happen, using var_dump(set_exception_handler('is_int'));die(); as the first line of ErrorHandler::handleException() produces… null. So that method is called, but there is no exception handler?

@greg0ire
Copy link
Contributor Author
greg0ire commented Feb 20, 2024

Ah, this seems to be PHP-8.3-specific 💡

Reported the regression at php/php-src#13446

@greg0ire greg0ire changed the title Type error results in zero exit code Type error results in zero exit code starting with PHP 8.3.0 Feb 20, 2024
@greg0ire
Copy link
Contributor Author
greg0ire commented Feb 21, 2024

The logic for accessing the exception handler within ErrorHandler::handleFatalError was added in #25408

Before that, fatal errors were handled with the error handler rather than the exception handler.

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

No branches or pull requests

5 participants
0