opcache_compile_file() fails with Cannot redeclare function error when compiling multiple files with the same function
还没有人认领这个 Issue。
- 主要语言
- C
- 星标
- 40.4k
- 派生
- 8.2k
- 平均合并
- 2 天 13 小时
- 30 天内合并 PR
- 96
描述
Description
The following code:
compile-test.php
<?php
var_dump(opcache_compile_file(__DIR__ . '/releases/v1/functions.php'));
// expecting `bool(false)` since opcache_compile_file() shouldn't have executed the compiled file.
var_dump(in_array('foo', get_defined_functions()['user']));
// expecting successful compilation: `bool(true)`
var_dump(opcache_compile_file(__DIR__ . '/releases/v2/functions.php'));
releases/v1/functions.php
<?php
function foo(): string {
return 'foo_v1';
}
releases/v2/functions.php
<?php
function foo(): string {
return 'foo_v2';
}
Resulted in this output:
bool(true)
bool(true)
PHP Fatal error: Cannot redeclare foo() (previously declared in /private/tmp/opcache/releases/v1/functions.php:4) in /private/tmp/opcache/releases/v2/functions.php on line 4
Fatal error: Cannot redeclare foo() (previously declared in /private/tmp/opcache/releases/v1/functions.php:4) in /private/tmp/opcache/releases/v2/functions.php on line 4
PHP Warning: Zend OPcache could not compile file /private/tmp/opcache/releases/v2/functions.php in /private/tmp/opcache/compile-test.php on line 4
Warning: Zend OPcache could not compile file /private/tmp/opcache/releases/v2/functions.php in /private/tmp/opcache/compile-test.php on line 4
bool(false)
But I expected this output instead:
bool(true)
bool(false)
bool(true)
Additional Context:
The issue happens on both cli and php-fpm. It seems to be similar to https://bugs.php.net/bug.php?id=66066 . While the use case in bug 66066 might be less common in the real world and probably should be avoided, it's a perfectly valid use case to run multiple releases with similar files under the same php-fpm instance.
Workaround
Our current workaround is to compile different releases with different requests, i.e. PHP will happily process the following logics in two different requests.
compile-v1.php
<?php
var_dump(opcache_compile_file(__DIR__ . '/releases/v1/functions.php'));
compile-v2.php
<?php
var_dump(opcache_compile_file(__DIR__ . '/releases/v2/functions.php'));
opcache_get_status()['scripts'] shows that the two files have been compiled successfully.
[/var/www/html/releases/v2/functions.php] => Array
(
[full_path] => /var/www/html/releases/v2/functions.php
[hits] => 0
[memory_consumption] => 992
[last_used] => Fri Nov 1 11:32:01 2024
[last_used_timestamp] => 1730460721
[timestamp] => 1730458160
[revalidate] => 1730460723
)
[/var/www/html/releases/v1/functions.php] => Array
(
[full_path] => /var/www/html/releases/v1/functions.php
[hits] => 5
[memory_consumption] => 992
[last_used] => Fri Nov 1 11:31:58 2024
[last_used_timestamp] => 1730460718
[timestamp] => 1730458130
[revalidate] => 1730460720
)
PHP Version
PHP 8.3.13
Operating System
No response
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先,在 PHP 8.3.13 上使用 opcache_compile_file() 运行 issue 的 compile-test.php reproducer,然后将其与单独的 compile-v1.php 和 compile-v2.php 请求进行比较。跟踪 opcache_compile_file() 入口点及其对包含重复函数的已编译文件的处理;完成的标准是两个文件都能在一次请求中成功编译,而不执行其中的代码,也不产生 redeclare 错误。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- php
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 52/100