php / php/php-src

stream_copy_to_stream returns false when data copied is less than $maxlength

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

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

Category: Streams Feature
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

The following code:

<?php
$streaminput = popen("my_command", "r");  // Some command which has generated output that sometimes has less than the 10000 read size bytes of data (if you care, I was actually calling postgres's pg_dump)
$streamoutput = popen("another_command", "w"); // I was actually using input pipe (i.e. stdin) of command started with proc_open() here (actually it was gzip, and I used proc_open so that I could read stdout here too), but AFAIK, that's just a stream to stdin as is the case in this example)
stream_set_blocking($streamoutput, false);

$expectedPos = 0;
while (!feof($streaminput)) {
  $copied = stream_copy_to_stream($streaminput, $streamoutput, 10000);
  $expectedPos += $copied;

  // if ($copied === false) { echo "\nCopied is false!";
  // if ($copied === 0) { echo "\nCopied is 0!"; // This message never printed

  if ($copied == 0) {  // Note: I later discovered that the unexpected false return caused the branch to execute because of == instead of ===, even though the stream position had advanced.
   echo ".";
   sleep 1;
   continue;
  else {
    echo "\nCopied $copied, Expected position $expectedPos, actual position " . ftell($streaminput);
  }

}

Resulted in this output:

Copied 10000, Expected position 10000, actual position 10000
Copied 10000, Expected position 20000, actual position 20000
Copied 10000, Expected position 30000, actual position 30000.
Copied 10000, Expected position 40000, actual position 48192
Copied 10000, Expected position 50000, actual position 58192

But I expected this output instead:

Copied 10000, Expected position 10000, actual position 10000
Copied 10000, Expected position 20000, actual position 20000
Copied 10000, Expected position 30000, actual position 30000
Copied 8192, Expected position 38192, actual position 38192
Copied 10000, Expected position 48192, actual position 48192

Notice how, in the real output, when the dot appears after 30000 (suggesting 0 bytes were copied, in fact 8192 bytes appear to have been copied according to the position returned by ftell()) - you can confirm $copied is false by uncommenting the relevant lines.

Each time I got a "." in the output I observed that the actual stream position had advanced, but always by less than the 10000 .... if it advanced by 10000, the value of $copied was 10000 and I got the full message. (edit: I've confirmed this by printing ftell($inputstream) with the "." in my local code, but I've not updated the example code/output above)

Also note that $streaminput never got to an eof state, and appeared most time to have simply stopped adding to the stream, and I end up killing the php script with ^C

PHP Version

PHP 8.1.2-1ubuntu2.13 (cli) (built: Jun 28 2023 14:01:49) (NTS)

Operating System

Ubuntu 22.04.2 LTS \n \l (running on WSL in Windows 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

Bắt đầu bằng cách tái hiện ví dụ stream_copy_to_stream được cung cấp với một stream đầu ra không chặn, rồi kiểm tra entry point stream_copy_to_stream trong mã nguồn của PHP. So sánh giá trị được trả về với vị trí của stream đầu vào khi có ít hơn 10000 byte khả dụng. Được xem là hoàn tất khi các thao tác sao chép một phần báo cáo số byte đã sao chép và vòng lặp có thể đạt EOF mà không bị treo.

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

Đánh giá

Công nghệ
c, php
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/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.