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

php_opcache crash - memory access violation

Open jjdunn opened this issue 4 years ago • 28 comments

Description

I don't know how to recreate the problem. it occurs on our production server, sporadically. 6x in past 4 days. never happened on old server (Windows 2012 R2) running same versions of PHP (8.0.14 x64 thread-safe) and apache httpd (2.4.51 x64), and the same opcache configuration. We are not using JIT.

Windows Event Log shows:

Faulting application name: php.exe, version: 8.0.14.0, time stamp: 0x61bb14bc Faulting module name: php_opcache.dll, version: 8.0.14.0, time stamp: 0x61bb1862 Exception code: 0xc0000005 Fault offset: 0x0000000000002fe6 Faulting process id: 0x25d8 Faulting application start time: 0x01d8045afae2031a Faulting application path: C:\Program Files\php\php.exe Faulting module path: C:\Program Files\PHP\ext\php_opcache.dll Report Id: d7c91f64-1d66-4a3c-a206-7f1efaf4d8cc Faulting package full name: Faulting package-relative application ID:

php.ini:

[PHP_OPCACHE]
zend_extension=php_opcache.dll
opcache.enable=1;
opcache.enable_cli=1;
opcache.memory_consumption=512;
opcache.interned_strings_buffer=32;
opcache.validate_timestamps=1;
opcache.save_comments=0;
opcache.revalidate_path=1;
opcache.enable_file_override=1;
opcache.max_file_size=0;
opcache.file_cache_only=0;
opcache.log_verbosity_level=2;
opcache.mmap_base = 0x20000000
opcache.jit_buffer_size=0;
opcache.jit=Off;

Running Windows DebugDiag tool, with PHP 8.0.14 symbols loaded, gives the report attached here: opcache-crash-analysis.pdf

PHP Version

PHP 8.0.14

Operating System

Windows Server 2019; apache httpd 2.4.51 x64

jjdunn avatar Jan 08 '22 18:01 jjdunn

Has something been changed in 8.1 with respect to OPcache sharing in Windows CLI?

I don't think there has been anything specific wrt. Windows (CLI). Maybe your problems are caused by the inheritance cache which has been introduced. There might be bugs to fix, but without some way to reproduce these issues, there's probably not much we can do.

The issues are gone when using opcache.enable_cli=0 or by using pcache.cache_id However, this is not favorable for the reason of performance you likely mentioned in #7915 (comment)

Disabling OPcache (or only enabling file cache) can cause serious performance degradation; opcache_cache_id should not (unless there is not sufficient memory available; each cache_id get's its own SHM). So I suggest you try with different cache_ids.

Generally, I'm not happy with the OPcache re-attaching behavior (which is only implemented on Windows). On other systems, unrelated CLI processes always have their own OPcache instances. The re-attaching on Windows was implemented to be able to share a single cache for mutliple (F)CGI processes, but that has issues (i.e. possibly failing to work, so file_cache_fallback is a thing). And in the case of simulating fork(2) on Windows, sharing the same cache also makes sense. Anyhow, the way OPcache is designed, it can't really work great on Windows due to the lack of fork(2) combined with strong ASLR. I'm afraid there is no general solution on Windows; avoiding re-attaching should solve a couple of issues, but would impose limitations which probably could only be resolved by using multiple threads on Windows (instead of fork(2) on other systems), but that is not necessarily good wrt. stability (one crashing thread will kill the whole process including all other running threads).

cmb69 avatar Jan 27 '23 11:01 cmb69

@cmb69 thanks for your fast reply.

The reproduction of the crash over here is quite easy in PHPstorm:

  • a terminal instance is opened and a NTS PHP instance started with php.exe (we're not doing anything with this instance, just have it running)
  • another instance is opened and a single phpunit test run is executed
  • the first run works flawlessly
  • when redoing this same phpunit test run, it crashes (exit code -1073741819 (0xC0000005))

This process can be repeated with the same result every time. So it doesn't crash during the first run but always at the second run. It also always seems to crash at the same test so the reproduction seems to be consistent.

Update: Further investigation shows that the crash originates in calls to PDO's sqlite::memory: Any ideas what has changed between 8.0 and 8.1 on this matter?

dynasource avatar Jan 27 '23 13:01 dynasource

The reproduction of the crash over here is quite easy in PHPstorm:

If it would generally happen, there certainly have been a lot of respective reports. I assume that this is specific to your production and/or test code, or possibly your machine or PHP configuration.

Further investigation shows that the crash originates in calls to PDO's sqlite::memory:

Do you call that directly (new PDO(":sqlite::memory")), or do you use a wrapper which inherits from PDO? If the latter, that might indeed be caused by the inheritance cache.

Also condider to clear the file cache, or even to disable it via configuration; at least for testing purposes.

cmb69 avatar Jan 27 '23 14:01 cmb69

I digged it bit further, it seems the PDO was not called yet (sorry), but a file prior to that (a query building file named SelectQuery.php).

The process crashes when I call one of its methods. When I comment that out, the tests run fine (having that other php instance open). I can rerun this without problems.

However, when I make a change in that particular SelectQuery.php file, like adding '//' somewhere, everything crashes again. I have to kill that other PHP process to make it all running a again succesfully. Also when a new php instance is started, everything works repeatly fine.

So during runtime something is going on with that particular file and mutating it does have consequences, perhaps because of the new Inheritance cache.

dynasource avatar Jan 27 '23 14:01 dynasource

interestingly, when I mutate the ancestor root file (adding // somewhere), the 0xC0000005 disappears and everything runs smooth again

So we have an inheritance tree like this: SelectQuery > ClausesQuery > Query;

Mutate behavior by adding //: SelectQuery : -> tests and reruns crash with 0xC0000005 ClausesQuery : -> tests and reruns crash with 0xC0000005 Query: -> tests suddenly run OK again (some tree reset?)

dynasource avatar Jan 27 '23 14:01 dynasource

good news, we have found a workaround to this bug, thanks to @cmb69 pointing to looking into the inheritance cache. Highly appreciated.

Before running multiple processes in parallel, we run a single php file in which we loop the composer classmap (composer/autoload_classmap.php) and execute a class_exists on the classes.

With this php instance running in the background, we are able to run our tests in parallel again. Jeej.

dynasource avatar Jan 27 '23 15:01 dynasource

Regarding the opcache.cache_id property - I run all our Windows IIS sites under different user accounts for the apppools. So, would it still apply in this configuration to specify a different cache_id for each site? Thank you.

; Facilitates multiple OPcache instances per user (for Windows only). All PHP ; processes with the same cache ID and user share an OPcache instance. ;opcache.cache_id=

; Enables and sets the second level cache directory. ; It should improve performance when SHM memory is full, at server restart or ; SHM reset. The default "" disables file based caching. opcache.file_cache="D:\PHPopcache\${APP_POOL_ID}"

; 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

gasperj avatar Sep 08 '23 22:09 gasperj

I've detected the same problem when PHP use a symlink and when PHP use the same file between different sites. eg.: site1, site2, siteN, use the c:\common\shared.php

bertasoft avatar Aug 08 '24 08:08 bertasoft