spatie / async

Easily run code asynchronously
https://spatie.be/en/opensource/php
MIT License
2.65k stars 179 forks source link

PHP errors not catched? #183

Closed mmartinello closed 2 years ago

mmartinello commented 2 years ago

Hi, I'm successfully using spatie/async and my threads are ran without any problems until I changed some of my internal classes so that I generated some PHP errors for non-existent methods and functions.

I'm using Monolog to write logs and redirect errors to stderr so that spatie/async recognises them and makes children fail, but I realised that children are not failing if they encounters any internal PHP error!

The Monolog configuration is really the same for the main thread and children also.

I also tried to configure error_log = /tmp/php_errors.log into the php.ini and I realised that errors from the main thread are written in this log file, and not errors generated from the children!

Is spatie/async hiding PHP errors or I am making something wrong?

mmartinello commented 2 years ago

I'm making some tests trying found out what's going.

When the pool children are running I got one of the PHP child commands executed as thread, for example:

/usr/local/Cellar/php@7.4/7.4.27/bin/php /Users/me/Projects/my-project/vendor/spatie/async/src/Runtime/ChildRuntime.php /Users/me/Projects/my-project/_classes/../_inc/inclusions.php QzozMjoiT3Bpc1xDbG9zdXJlXFNlcmlhbGl6YWJsZUNsb3N1cmUiOjQ5NDgyOnthOjU6e3M6MzoidXNlIjthOjEwOntzOjEwOiJjb250ZW50X2lkIjtzOjg6IjI1OTYzODQyIjtzOjM6InBkZiI7TzozOiJQZGYiOjM3Njp7czo5OiIAKgBzZW50cnkiO047czo2OiIAKgBjbWQiO047czoxMDoiACoAdG1wX2RpciI7czo1ODoiL1VzZ... (base 64 continues)

Which is: <php executable> <async ChildRuntime.php> <autoloader file> <base 64 of my child function>

I executed the command on my terminal to see what happens, thinking that I would receive the PHP errors on the stdout, but if I insert one PHP error on purpose (in this case I inserted one call to one non-existent PHP function), the children thread immediately ends (which is correct), but strangely I see some base64 code in the stdout after my logs:

~/P/my-project> bash php-child.sh                                            Tue Feb 15 09:09:04 2022

Notice: Undefined offset: 3 in /Users/me/Projects/my-project/vendor/spatie/async/src/Runtime/ChildRuntime.php on line 8

Call Stack:
   0.0006     474272   1. {main}() /Users/me/Projects/my-project/vendor/spatie/async/src/Runtime/ChildRuntime.php:0

[2022-02-15T10:10:47.504753+01:00] /4837.DEBUG: My log
[2022-02-15T10:10:52.272628+01:00] 1454686003/4837.DEBUG: My log
[2022-02-15T10:10:52.273079+01:00] 1454686003/4837.DEBUG: My log
Tzo0MToiU3BhdGllXEFzeW5jXE91dHB1dFxTZXJpYWxpemFibGVFeGNlcHRpb24iOjM6e3M6ODoiACoAY2xhc3MiO3M6NToiRXJyb3IiO3M6MTA6IgAqAG1lc3NhZ2UiO3M6NDk6IkNhbGwgdG8gdW5kZWZpbmVkIGZ1bmN0aW9uIG5vbmV4aXN0ZW50X2Z1bmN0aW9uKCkiO3M6ODoiACoAdHJhY2UiO3M6MjIyNToiIzAgY2xvc3VyZTovL2Z1bmN0aW9uICgpIHVzZSgkY29udGVudF9pZCwgJHBkZiwgJHVzZXJfaWQsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICRjdXN0b21lcl9pZCwgJGRpcl9wYXRoLCAkdXVpZCwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgJHVwZGF0ZV9wcm9ncmVzc192YWx1ZSwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgJGN1cnJlbnRfY29udGVudF9uLCAkY29udGVudHNfY291bnQsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICR1c2VyX2NhdGVnb3JpZXMpIHsKICAgICAgICAgICAgICAgICRzdGFydF90aW1lID0gXHRpbWUoKTsKCiAgICAgICAgICAgICAg... (base 64 continues)

The base 64 code I see in the stdout really contains the PHP error:

O:41:"Spatie\Async\Output\SerializableException":3:{s:8:"�*�class";s:5:"Error";s:10:"�*�message";s:49:"Call to undefined function nonexistent_function()";s:8:"�*�trace";s:2225:"#0 closure://function () use($content_id, $pdf, $user_id,

What I understand is that PHP errors are not even catched by PHP itself, as they are not redirected to the error file configured into php.ini:

 ~> /usr/local/Cellar/php@7.4/7.4.27/bin/php -i|grep "Loaded Configuration File"
Loaded Configuration File => /usr/local/etc/php/7.4/php.ini

 ~> grep "error_log =" /usr/local/etc/php/7.4/php.ini                                                    Tue Feb 15 09:19:48 2022
error_log = /tmp/php_errors.log

 ~> cat /tmp/php_errors.log
[15-Feb-2022 10:10:47 Europe/Rome] PHP Notice:  Undefined offset: 3 in /Users/me/Projects/my-project/vendor/spatie/async/src/Runtime/ChildRuntime.php on line 8
[15-Feb-2022 10:10:47 Europe/Rome] PHP Stack trace:
[15-Feb-2022 10:10:47 Europe/Rome] PHP   1. {main}() /Users/me/Projects/my-project/vendor/spatie/async/src/Runtime/ChildRuntime.php:0

Is this behaviour expected or am I making something wrong?

spatie-bot commented 2 years ago

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.