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

PHP8.1.3. opcache bug with monolog/monolog

Open R4c00n opened this issue 4 years ago • 31 comments

Description

Using monolog/monolog on PHP8.1.3 results in some very weird problems with the class vendor/monolog/monolog/src/Monolog/DateTimeImmutable.php not being found. There's also already a bugreport in monolog https://github.com/Seldaek/monolog/issues/1626, but it looks like this is an opcache bug.

Our deployment process looks like this:

  • Upload files to server into release directory
  • symlink release directory
  • reset opcache (opcache_reset)

After that, we get the PHP message: PHP Fatal error: Uncaught Error: Class "Monolog\DateTimeImmutable" not found in ... error. Resetting opcache a second time fixes the bug and everything is fine until the next deployment.

PHP Version

PHP 8.1.3

Operating System

Debian

R4c00n avatar Mar 02 '22 09:03 R4c00n

Also: With PHP7.4 and same opcache settings the same workflow & code is working

R4c00n avatar Mar 02 '22 10:03 R4c00n

Is a file cache involved?

cmb69 avatar Mar 02 '22 12:03 cmb69

All file_cache settings are on default, so should be turned off.

; ...
;opcache.file_cache=

; Enables or disables opcode caching in shared memory.
;opcache.file_cache_only=0

; Enables or disables checksum validation when script loaded from file cache.
;opcache.file_cache_consistency_checks=1

; Implies opcache.file_cache_only=1 for a certain process that failed to
; reattach to the shared memory (for Windows only). Explicitly enabled file
; cache is required.
;opcache.file_cache_fallback=1
; ...

R4c00n avatar Mar 02 '22 12:03 R4c00n

Thanks for the further info! Did you check the return value of opcache_reset() (i.e. did it return true?) Are there any relevant messages in opcache.error_log (please also check with opcache.log_verbosity_level=4).

cmb69 avatar Mar 02 '22 13:03 cmb69

Same problem here, no file cache, will check with log and verbosity.

temp avatar Mar 02 '22 18:03 temp

opcache_reset returns true, both during release and also when executing manually The logs also look good.

Here are some logs with grep for the file in question: After release (when it's broken) we get:

Thu Mar  3 10:51:49 2022 (24268): Message Cached script '/home/xxx/release-2882/vendor/monolog/monolog/src/Monolog/DateTimeImmutable.php'
Thu Mar  3 10:51:49 2022 (24268): Message Added key '/home/xxx/release-2882/vendor/composer/../monolog/monolog/src/Monolog/DateTimeImmutable.php'

After manual reset (when it works again) this gets added:

Thu Mar  3 10:57:23 2022 (24272): Message Cached script '/home/xxx/release-2882/vendor/monolog/monolog/src/Monolog/DateTimeImmutable.php'
Thu Mar  3 10:57:23 2022 (24272): Message Added key '/home/xxx/release-2882/vendor/composer/../monolog/monolog/src/Monolog/DateTimeImmutable.php'

R4c00n avatar Mar 03 '22 10:03 R4c00n

Thanks! First, it is important to understand that when opcache_reset() returns, the SHM cache has not necessarily been reset; it may take up to opcache.force_restart_timeout seconds for the reset to actually happen. This might cause issues when a new deployment just switches symlinks. For a more robust way to deploy a new app version, see http://blog.jpauli.tech/2015-03-05-opcache-html/#understandingtheopcachememoryconsumption.

Since in this case the issue apparently happens as of PHP 8.1.0, it might be related to the inheritance cache (introduced with commit 4b79dba93202ed5640dff317046ce2fdd42e1d82).

cmb69 avatar Mar 03 '22 12:03 cmb69

Thanks for the link explaining a bit about the opcache! Changing our release process for all projects would be a bit too much right now. The hotfix with requiring the monolog DateTimeImmutable seems to work for us too.

Is this something that's likely to be fixed in a future bugfix release, or do we need to invest some time and change our release process in the long run?

R4c00n avatar Mar 03 '22 13:03 R4c00n

We are facing this exact issue on one of our servers running PHP 8.0.13. And in our case this error occurs when running a Symfony (v5.4) Console command for which the OpCache is disabled (opcache.enable_cli => Off => Off). Thought this might be a helpful information for the debugging process.

Using the "hotfix" works for us now but it's not a satisfying solution ofc.

felixwatzka avatar Mar 03 '22 15:03 felixwatzka

@R4c00n, does that issue occur for you with OPcache disabled? If so, we we're probably looking at the wrong places; otherwise, @felixwatzka's issue is something different.

cmb69 avatar Mar 03 '22 15:03 cmb69

I did a quick test and it seems like everything is working for us in CLI without opcache.

php -a
php > var_dump(opcache_get_status());
bool(false)
php > require_once __DIR__ . '/vendor/autoload.php';
php > $logger = new \Monolog\Logger('debug', [new \Monolog\Handler\StreamHandler('php://output', \Monolog\Logger::DEBUG, true, null, true)]);
php > $logger->addRecord(\Monolog\Logger::DEBUG, 'test');
[2022-03-04T08:40:17.450865+01:00] debug.DEBUG: test [] []

R4c00n avatar Mar 04 '22 07:03 R4c00n

PHP Version 7.4.27

PHP Version 7.4.27 System OpenBSD uxm-server.uxm 7.0 GENERIC.MP#5 amd64 Build Date Feb 17 2022 09:40:26 Configure Command './configure' '--with-pcre-jit=no' '--without-valgrind' '--with-zip=shared' '--enable-gd=shared' '--with-jpeg=/usr/local' '--with-png=/usr/local' '--with-webp=/usr/local' '--with-freetype=/usr/X11R6' '--enable-shared' '--disable-static' '--disable-rpath' '--with-config-file-path=/etc' '--enable-inline-optimization' '--with-pic' '--with-pear=/usr/local/share/php-7.4' '--with-config-file-scan-dir=/etc/php-7.4' '--with-pdo-sqlite' '--with-sqlite3' '--with-password-argon2' '--program-suffix=-7.4' '--with-readline' '--with-apxs2=/usr/local/sbin/apxs2' '--with-openssl' '--with-zlib' '--enable-xml' '--enable-wddx' '--enable-cli' '--with-iconv=/usr/local' '--with-gettext=/usr/local' '--enable-bcmath' '--enable-session' '--enable-calendar' '--enable-ctype' '--enable-ftp' '--with-pcre-regex' '--enable-sockets' '--enable-sysvmsg' '--enable-sysvsem' '--enable-sysvshm' '--enable-mbstring' '--enable-exif' '--enable-cgi' '--enable-fpm' '--with-fpm-user=www' '--with-fpm-group=www' '--with-bz2=shared,/usr/local' '--with-curl=shared,/usr/local' '--enable-dba=shared' '--with-gdbm=/usr/local' '--with-gd=shared' '--with-jpeg-dir=/usr/local' '--with-png-dir=/usr/local' '--with-webp-dir=/usr/local' '--with-zlib-dir=/usr' '--with-t1lib=/usr/local' '--with-freetype-dir=/usr/X11R6' '--with-xpm-dir=/usr/X11R6' '--with-gmp=shared,/usr/local' '--enable-intl=shared' '--with-icu-dir=/usr/local' '--with-imap=shared,/usr/local' '--with-imap-ssl' '--with-ldap=shared,/usr/local' '--with-mysqli=shared' '--disable-iodbc' '--enable-pcntl=shared' '--with-pdo-mysql=shared' '--with-pdo-pgsql=shared,/usr/local' '--with-pgsql=shared,/usr/local' '--with-pspell=shared,/usr/local' '--enable-shmop=shared,/usr/local' '--enable-soap=shared,/usr/local' '--with-snmp=shared,/usr/local' '--with-tidy=shared,/usr/local' '--with-xmlrpc=shared' '--with-xsl=shared' '--enable-dom' '--enable-zip=shared,/usr/local' '--prefix=/usr/local' '--sysconfdir=/etc' '--mandir=/usr/local/man' '--infodir=/usr/local/info' '--localstatedir=/var' '--disable-silent-rules' '--disable-gtk-doc' 'CFLAGS=-O2 -pipe -I/usr/local/include -pthread' Server API Apache 2.0 Handler Virtual Directory Support disabled Configuration File (php.ini) Path /etc Loaded Configuration File /etc/php-7.4.ini Scan this dir for additional .ini files /etc/php-7.4 Additional .ini files parsed /etc/php-7.4/bz2.ini, /etc/php-7.4/curl.ini, /etc/php-7.4/dba.ini, /etc/php-7.4/gd.ini, /etc/php-7.4/gmp.ini, /etc/php-7.4/intl.ini, /etc/php-7.4/mongodb.ini, /etc/php-7.4/mysqli.ini, /etc/php-7.4/opcache.ini, /etc/php-7.4/pdo_mysql.ini, /etc/php-7.4/pdo_pgsql.ini, /etc/php-7.4/pgsql.ini, /etc/php-7.4/zip.ini PHP API 20190902 PHP Extension 20190902 Zend Extension 320190902 Zend Extension Build API320190902,NTS PHP Extension Build API20190902,NTS Debug Build no Thread Safety disabled Zend Signal Handling enabled Zend Memory Manager enabled Zend Multibyte Support provided by mbstring IPv6 Support enabled DTrace Support disabled Registered PHP Streams https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar, zip Registered Stream Socket Transports tcp, udp, unix, udg, ssl, tls, tlsv1.0, tlsv1.1, tlsv1.2, tlsv1.3 Registered Stream Filters zlib., convert.iconv., bzip2., string.rot13, string.toupper, string.tolower, string.strip_tags, convert., consumed, dechunk Zend logo This program makes use of the Zend Scripting Language Engine: Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.27, Copyright (c), by Zend Technologies

unix-world avatar Mar 04 '22 09:03 unix-world

apache2handler

Apache Version Apache
Apache API Version 20120211
Server Administrator webmaster@localsslserver
Hostname:Port 86.122.12.131:443
User/Group www(67)/67
Max Requests Per Child: 8192 - Keep Alive: on - Max Per Connection: 256
Timeouts Connection: 720 - Keep-Alive: 5
Virtual Server Yes
Server Root /var/www
Loaded Modules core mod_so http_core mod_authn_file mod_authn_core mod_authz_host mod_authz_user mod_authz_core mod_access_compat mod_auth_basic mod_auth_digest mod_allowmethods mod_file_cache mod_cache mod_cache_disk mod_cache_socache mod_socache_shmcb mod_socache_memcache mod_buffer mod_reqtimeout mod_request mod_include mod_filter mod_deflate mod_mime mod_log_config mod_env mod_mime_magic mod_expires mod_headers mod_unique_id mod_setenvif mod_version mod_session mod_ssl prefork mod_unixd mod_dav mod_status mod_autoindex mod_dav_fs mod_dav_lock mod_vhost_alias mod_negotiation mod_dir mod_alias mod_rewrite mod_php7 mod_dav_svn mod_authz_svn

apache2handler Apache Version Apache Apache API Version 20120211 Server Administrator webmaster@localsslserver Hostname:Port 86.122.12.131:443 User/Group www(67)/67 Max Requests Per Child: 8192 - Keep Alive: on - Max Per Connection: 256 Timeouts Connection: 720 - Keep-Alive: 5 Virtual Server Yes Server Root /var/www Loaded Modules core mod_so http_core mod_authn_file mod_authn_core mod_authz_host mod_authz_user mod_authz_core mod_access_compat mod_auth_basic mod_auth_digest mod_allowmethods mod_file_cache mod_cache mod_cache_disk mod_cache_socache mod_socache_shmcb mod_socache_memcache mod_buffer mod_reqtimeout mod_request mod_include mod_filter mod_deflate mod_mime mod_log_config mod_env mod_mime_magic mod_expires mod_headers mod_unique_id mod_setenvif mod_version mod_session mod_ssl prefork mod_unixd mod_dav mod_status mod_autoindex mod_dav_fs mod_dav_lock mod_vhost_alias mod_negotiation mod_dir mod_alias mod_rewrite mod_php7 mod_dav_svn mod_authz_svn

unix-world avatar Mar 04 '22 09:03 unix-world

Zend OPcache

Opcode Caching Up and Running
Optimization Enabled
SHM Cache Enabled
File Cache Disabled
Startup OK
Shared memory model mmap
Cache hits 0
Cache misses 1
Used memory 17160440
Free memory 519710472
Wasted memory 0
Interned Strings Used memory 387632
Interned Strings Free memory 12194832
Cached scripts 1
Cached keys 2
Max keys 7963
OOM restarts 0
Hash keys restarts 0
Manual restarts 0

Zend OPcache Opcode Caching Up and Running Optimization Enabled SHM Cache Enabled File Cache Disabled Startup OK Shared memory model mmap Cache hits 0 Cache misses 1 Used memory 17160440 Free memory 519710472 Wasted memory 0 Interned Strings Used memory 387632 Interned Strings Free memory 12194832 Cached scripts 1 Cached keys 2 Max keys 7963 OOM restarts 0 Hash keys restarts 0 Manual restarts 0 Directive Local Value Master Value opcache.blacklist_filename no value no value opcache.consistency_checks 0 0 opcache.dups_fix Off Off opcache.enable On On opcache.enable_cli Off Off opcache.enable_file_override Off Off opcache.error_log /var/log/apache2/php-opcache-errors.log /var/log/apache2/php-opcache-errors.log opcache.file_cache no value no value opcache.file_cache_consistency_checks On On opcache.file_cache_only Off Off opcache.file_update_protection 2 2 opcache.force_restart_timeout 180 180 opcache.huge_code_pages Off Off opcache.interned_strings_buffer 16 16 opcache.lockfile_path /tmp /tmp opcache.log_verbosity_level 1 1 opcache.max_accelerated_files 5000 5000 opcache.max_file_size 0 0 opcache.max_wasted_percentage 5 5 opcache.memory_consumption 512 512 opcache.opt_debug_level 0 0 opcache.optimization_level 0x7FFEBFFF 0x7FFEBFFF opcache.preferred_memory_model no value no value opcache.preload no value no value opcache.preload_user no value no value opcache.protect_memory Off Off opcache.restrict_api no value no value opcache.revalidate_freq 2 2 opcache.revalidate_path Off Off opcache.save_comments Off Off opcache.use_cwd On On opcache.validate_permission Off Off opcache.validate_root Off Off opcache.validate_timestamps On On

unix-world avatar Mar 04 '22 09:03 unix-world

Environment

Variable Value
HOME /
PWD /
INRC 1
LD_LIBRARY_PATH /usr/local/lib
_ /usr/local/sbin/apachectl2
PATH /sbin:/bin:/usr/sbin:/usr/bin

unix-world avatar Mar 04 '22 09:03 unix-world

@unix-world, PHP 7.4 is no longer actively supported, and as such is irrelevant here.

cmb69 avatar Mar 04 '22 09:03 cmb69

Is this something that's likely to be fixed in a future bugfix release, […]

It looks like a bug, so it should be fixed; the problem is that it's hard to find the root cause without a somewhat reliable reproduce script.

cmb69 avatar Mar 08 '22 11:03 cmb69

@cmb69

@unix-world, PHP 7.4 is no longer actively supported, and as such is irrelevant here.

Yes, I know, and I agree. The target is not PHP 7.4 but having PHP 8.0 or 8.1 without crashing. Currently only 7.3 is not crashing ...

I put in this discussion the PHP 7.4 only for some particular reasons:

  • the opcache in PHP 7.4 changed a lot since 7.3 ; the 7.4 opcache is much more similar with 8.0 opcache
  • the opcache in 7.4 does not include opcache JIT thus it is important to know that not the opcache JIT it is the cause

Does it make sense ?

unix-world avatar Mar 11 '22 20:03 unix-world

@R4c00n could you share the output of opcache_get_status() before a reset that triggered the issue, and during the issue ?

arnaud-lb avatar Apr 01 '22 09:04 arnaud-lb

FYI also had this issue with Monolog but after the third restart of the PHP FPM service the issue is gone now and currently I'm not able to reproduce it anymore. See also https://github.com/Seldaek/monolog/issues/1626#issuecomment-1085793033

gilbertsoft avatar Apr 01 '22 11:04 gilbertsoft