php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Opcache breaks autoloading after E_COMPILE_ERROR

Open shira-374 opened this issue 4 years ago • 3 comments

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

shira-374 avatar Feb 08 '22 20:02 shira-374

This looks like an unintended side-effect of dd86987b2cbead2af1e95460cc5cebe03e83f736.

cmb69 avatar Feb 09 '22 13:02 cmb69

Fixed in https://github.com/php/php-src/pull/8297

arnaud-lb avatar Apr 22 '22 16:04 arnaud-lb

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.

sahilpurav avatar Feb 10 '25 13:02 sahilpurav