php / php/php-src

Resource ID space overflow: Better approach to incremental resource id assignment

未关闭
#19,253 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Bug Status: Needs Triage
主要语言
C
星标
40.4k
派生
8.1k
平均合并
2 天 13 小时
30 天内合并 PR
96

描述

Description

We have the following simple code that runs in a daemon which is expected to to run very long time. However it looks like a new resource id is assigned every time this runs. And this runs a lot as we write to a ramdisk.
The following code:

61
62 file_put_contents("/dev/shm/somefile.txt", $data, LOCK_EX);
63 

Resulted in this output:

PHP Fatal error:  Resource ID space overflow in cache.class.php on line 62

But I expected this output instead:

No errors.
I understand that file_put_contents() creates a temporary file resource on each run and probably it's reaching  PHP_INT_MAX to cause this fatal error.
There is only one function get_resource_id() to guess and deal with this issue. Workaround is to keep file handles active during execution.

A)
PHP can handle this by resetting it back to 0 and adding a check to compare it to existing active resources(to avoid conflict) If this is not feasible, I could propose following:
1) In addition to get_resource_id(), we need **get_all_resource_ids**() : Return all active variables with resource ids
2) **resource_id_update**(existing, new) to move active variables to a specific range,
3) **resource_id_range** (0, PHP_INT_MAX) : This would reset back to start when max is reached.

With these extra methods, long running processes can allocate their active resources to a range and let dynamic resource id allocation out of conflict.

B) Another simpler approach could be: Assuming program creates all permanent resources for that run session after a few seconds, program can call a new method: resource_set_dynamic(). This method should record current+1 resource id in memory as "start point" and whenever overflow is due, it can reset it back to start point. This would avoid conflict with permanent resources which would be already created.

C) Checking against existing IDs could be another approach but this may have some impact on performance.
PHP Version
PHP 8.2.7 (cli) (built: Jul 20 2023 18:02:54) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.7, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.7, Copyright (c), by Zend Technologies
Operating System

Debian 12.11

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

使用 PHP 8.2.7 CLI 通过 file_put_contents() 在 /dev/shm 上复现 cache.class.php 第 62 行中的长时间运行 daemon 场景,然后跟踪该入口点所到达的 resource-ID 分配过程。该 issue 提出了几种相互竞争的分配策略,但没有定义应由哪种行为或测试来确定完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
php
领域
backend
Issue 类型
缺陷
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。