Skip to content

Instantly share code, notes, and snippets.

@johnstevenson johnstevenson/restart.php
Last active Jan 29, 2018

Embed
What would you like to do?
PHP Windows opcache-xdebug crash
<?php
if (!getenv('RESTART_TEST')) {
showExtensionInfo();
putenv('RESTART_TEST=1');
putenv('PHP_INI_SCAN_DIR=');
$iniFiles = getIniFiles();
$tmpIni = writeTmpIni($iniFiles);
$args = array_merge([PHP_BINARY, '-c', $tmpIni], $_SERVER['argv']);
$command = implode(' ', array_map('escapeshellarg', $args));
passthru($command);
} else {
echo 'Process restarted', PHP_EOL;
showExtensionInfo();
}
function getIniFiles()
{
$iniFiles = [strval(php_ini_loaded_file())];
if ($scanned = php_ini_scanned_files()) {
$iniFiles = array_merge($iniFiles, array_map('trim', explode(',', $scanned)));
}
if (empty($iniFiles[0])) {
array_shift($iniFiles);
}
return $iniFiles;
}
function writeTmpIni(array $iniFiles)
{
$tmpIni = sys_get_temp_dir().DIRECTORY_SEPARATOR.'tmp.ini';
$content = '';
$regex = '/^\s*(zend_extension\s*=.*xdebug.*)$/mi';
$replace = in_array('--keep-xdebug', $_SERVER['argv']) ? '$1' : ';$1';
foreach ($iniFiles as $file) {
$data = preg_replace($regex, $replace, file_get_contents($file));
$content .= $data.PHP_EOL;
}
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
if (in_array('--fix', $_SERVER['argv'])) {
$content .= 'opcache.enable_cli=0'.PHP_EOL;
}
}
file_put_contents($tmpIni, $content);
return $tmpIni;
}
function showExtensionInfo()
{
if (extension_loaded('zend opcache')) {
$opcache = 'extension: opcache loaded';
$opcache .= ', opcache.enable_cli='.ini_get('opcache.enable_cli');
} else {
$opcache = 'extension: opcache not loaded';
}
if (extension_loaded('xdebug')) {
$xdebug = 'extension: xdebug loaded';
} else {
$xdebug = 'extension: xdebug not loaded';
}
echo $opcache, PHP_EOL, $xdebug, PHP_EOL;
}
@johnstevenson

This comment has been minimized.

Copy link
Owner Author

johnstevenson commented Jan 29, 2018

Test script for Windows bug: https://bugs.php.net/bug.php?id=75886

Usage

Config: Enable opcache and xdebug extensions, set opcache.enable_cli=1

# Invoke crash
php.exe restart.php

# Avoid crash by applying user-land fix 
php.exe restart.php --fix

# Avoid crash by keeping xdebug loaded
php.exe restart.php --keep-xdebug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.