php / php/php-src

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

Đang mở
#19,253 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Bug Status: Needs Triage
Ngôn ngữ chính
C
Star
40.4k
Fork
8.1k
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
96

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Tái hiện trường hợp daemon chạy lâu trong cache.class.php tại dòng 62 bằng cách sử dụng file_put_contents() trên /dev/shm với PHP 8.2.7 CLI, sau đó lần theo quá trình cấp phát resource-ID mà entry point đó đạt tới. Issue đưa ra một số chiến lược cấp phát cạnh tranh nhau nhưng không xác định hành vi hoặc bài kiểm thử nào sẽ xác lập việc hoàn tất.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
php
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.