Closed
Description
Description
The following code:
<?php
function readLinesFrom(string $filename): iterable
{
if (!is_readable($filename)) {
throw new Exception("File {$filename} does not exist or is not readable");
}
$file = fopen($filename, 'r');
try {
while (($line = fgets($file)) !== false) {
yield trim($line);
}
} finally {
fclose($file);
}
}
$lines = iterator_to_array(readLinesFrom("/bad/file/path/1"));
when run multiple times with php -dopcache.enable_cli=on -dopcache.optimization_level=0x0010 -derror_log=/proc/self/fd/2 myfile.php
Resulted in this output:
PHP Warning: Undefined variable $file in myfile.php on line 15
Warning: Undefined variable $file in myfile.php on line 15
PHP Fatal error: Uncaught Exception: File /bad/file/path/1 does not exist or is not readable in myfile.php:7
Stack trace:
#0 [internal function]: readLinesFrom('/bad/file/path/...')
#1 myfile.php(19): iterator_to_array(Object(Generator))
#2 {main}
Next TypeError: fclose(): Argument #1 ($stream) must be of type resource, null given in myfile.php:15
Stack trace:
#0 myfile.php(15): fclose(NULL)
#1 [internal function]: readLinesFrom('/bad/file/path/...')
#2 myfile.php(19): iterator_to_array(Object(Generator))
#3 {main}
thrown in myfile.php on line 15
Fatal error: Uncaught Exception: File /bad/file/path/1 does not exist or is not readable in myfile.php:7
Stack trace:
#0 [internal function]: readLinesFrom('/bad/file/path/...')
#1 myfile.php(19): iterator_to_array(Object(Generator))
#2 {main}
Next TypeError: fclose(): Argument #1 ($stream) must be of type resource, null given in myfile.php:15
Stack trace:
#0 myfile.php(15): fclose(NULL)
#1 [internal function]: readLinesFrom('/bad/file/path/...')
#2 myfile.php(19): iterator_to_array(Object(Generator))
#3 {main}
thrown in myfile.php on line 15
But when run with php -dopcache.enable_cli=on -dopcache.optimization_level=0 -derror_log=/proc/self/fd/2 myfile.php
I get this instead:
PHP Fatal error: Uncaught Exception: File /bad/file/path/1 does not exist or is not readable in myfile.php:7
Stack trace:
#0 [internal function]: readLinesFrom('/bad/file/path/...')
#1 myfile.php(19): iterator_to_array(Object(Generator))
#2 {main}
thrown in myfile.php on line 7
Fatal error: Uncaught Exception: File /bad/file/path/1 does not exist or is not readable in myfile.php:7
Stack trace:
#0 [internal function]: readLinesFrom('/bad/file/path/...')
#1 myfile.php(19): iterator_to_array(Object(Generator))
#2 {main}
thrown in myfile.php on line 7
Slightly unsure if this is just another example of issue #8269 or not
PHP Version
PHP 8.3.16 and PHP 8.2.19
Operating System
Amazon Linux 2 and MacOS 15.2 (likely not relevant?)