Segment fault 11 when trying to catch exception
Description
Hello,
There is a very specific scenario which I know to explain how to reproduce but not to explain why it happens. I will do my best to explain below:
Using Symfony version 6 only platform:
using set_error_handler, and using include <file> that does not exists multiple times
throws a Segment 11 error which I could not trace and explain what it means.
This happens only on specific scenario, since doing a loop always catches it. I would like to add that this happens only on docker environment and does not happen on a normal php server. I have tried and it reproduces on alpine and debian containers.
To simplify the explanation they use set_error_handler with the following function:
$this->includeHandler = static function ($type, $msg, $file, $line) {
throw new \ErrorException($msg, 0, $type, $file, $line);
};
The code runs with it is:
set_error_handler($this->includeHandler);
try {
foreach ($this->scanHashDir($this->directory) as $file) {
try {
if (\is_array($expiresAt = include $file)) { // <----- This is the problametic line [include...]
$expiresAt = $expiresAt[0];
}
} catch (\ErrorException $e) {
$expiresAt = $time;
}
if ($time >= $expiresAt) {
$pruned = $this->doUnlink($file) && !file_exists($file) && $pruned;
}
}
} finally {
restore_error_handler();
}
This will actually catch the error for a dozen of times and than throws the segment fault. I have a ready project with explanation which file it is happening in and how to reproduce it.
There is also a link for this in Symfony community: Here
PHP Version
php 8.1.3
Operating System
Docker container php:8.1-fpm latest
Hello
Thank you for the reproducing repo :)
I was not able to reproduce the problem, though. What action is triggering the issue ? Which code path leads to $expiresAt = include $file ?
Hi,
You very welcome. The code path that gets there is when the Symfony cache files are created. Make sure and delete the following path: /var/www/html/var/cache
The Symfony mechanism checks if it has the data file cached, if not then it will create it for next time. And so if it throws an error, they manually catch it and add it to the $missingIds array.
Hi guys! I found a way to reproduce the error:
composer install symfony/cache
test.php
<?php
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
require __DIR__ . '/vendor/autoload.php';
$cache = new FilesystemAdapter();
$cache->get('wrong@key', function () {});
docker run -it --rm -v $PWD:/app -w /app php:8.1.2-cli bash
pecl install xdebug-3.1.3
docker-php-ext-enable xdebug
php test.php
The error only reproduces with:
- PHP 8.1.0 - 8.1.3
- Xdebug <= 3.1.3
The error comes from creating \Closure here with a class with static variables here
Bug is already fixed in PHP and Xdebug
I think we can close this issue.
@madmortigan1 can you test it with PHP 8.1.4?
Hello,
@mrsuh Thank you for your help, it works on version 8.1.4.
Thank you @mrsuh @madmortigan1