Opcache breaks autoloading after E_COMPILE_ERROR
Description
If an E_COMPILE_ERROR is raised during autoloading with opcache enabled, it breaks autoloading during the shutdown process in subsequent requests.
Multiple files are required to reproduce this, so I've put them in this gist: https://gist.github.com/shira-374/d2077e5581ce5ec53c655c7373846147
With opcache enabled, the script works once, then it always fatals twice. Without opcache it always works as expected.
<?php
spl_autoload_register(function (string $name) {
echo "Autoloading: {$name}<br>\n";
require __DIR__ . '/' . $name . '.php';
});
register_shutdown_function(function () {
new Bar(); // this triggers E_COMPILE_ERROR again for BadClass (???)
new Baz();
echo "Finished<br>\n";
});
new BadClass(); // this triggers E_COMPILE_ERROR
Resulted in this output:
Autoloading: BadClass
Autoloading: Foo
Fatal error: Declaration of BadClass::dummy() must be compatible with Foo::dummy(): void in /var/www/html/BadClass.php on line 5
Autoloading: Bar
Fatal error: Declaration of BadClass::dummy() must be compatible with Foo::dummy(): void in /var/www/html/BadClass.php on line 5
But I expected this output instead:
Autoloading: BadClass
Autoloading: Foo
Fatal error: Declaration of BadClass::dummy() must be compatible with Foo::dummy(): void in /var/www/html/BadClass.php on line 5
Autoloading: Bar
Autoloading: Baz
Finished
PHP Version
8.1.2
Operating System
Debian 11.2
This looks like an unintended side-effect of dd86987b2cbead2af1e95460cc5cebe03e83f736.
Fixed in https://github.com/php/php-src/pull/8297
I'm on PHP 8.3.9 and I'm still receiving the error. The log says: include(): Failed opening '/var/www/dragonglass/crm/vendor/composer/../laravel/framework/src/Illuminate/Foundation/Bootstrap/BootProviders.php' for inclusion (include_path='.:/usr/share/php')
As soon as I hit opcache_reset(). It fixes the issue. It happens only after deployment. I have added opcache_reset() as a part of our deployment script. It does not break everytime. It happens randomly after 10-15 deployments.