php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78902 Memory leak when using stream_filter_append
Submitted: 2019-12-03 19:33 UTC Modified: 2019-12-03 23:10 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:2 (100.0%)
From: leopold dot jacquot at gmail dot com Assigned:
Status: Open Package: Streams related
PHP Version: 7.4.0 OS: linux/amd64 (docker)
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2019-12-03 19:33 UTC] leopold dot jacquot at gmail dot com
Description:
------------
When using stream_filter_append, used memor increase until a Fatal error: Allowed memory error occurred.

This bug appears on PHP 7.3.11 and greater.

To reproduce, you will need to have the script test.php and a large file (here named file.txt). If needed, you can create a dummy blank file with this command (unix)

dd if=/dev/urandom of=file.txt bs=1048576 count=100

For easier test, I used official docker image and mount a folder with this two files.

In the actual result, you will see that the memory usage is increasing until a fatal error occurred.

In the expected result, you will see that the memory usage is stable all the long of the file reading. You can reproduce the expected result using the php:7.3.10-cli docker image.


Test script:
---------------
docker run -it --rm -v "/home/public":/usr/src/myapp -w /usr/src/myapp php:7.4.0-cli php test.php

<?php /** test.php **/
class useless_filter extends php_user_filter
{
    function filter($in, $out, &$consumed, $closing)
    {
        while ($bucket = stream_bucket_make_writeable($in)) {
            $consumed += $bucket->datalen;
            stream_bucket_append($out, $bucket);
        }
        echo memory_get_usage() . "\n";
        return PSFS_PASS_ON;
    }
}
stream_filter_register("useless", "useless_filter") or die("Failed to register filter");
$fp = fopen("file.txt", "r");
stream_filter_append($fp, "useless");
while (!feof($fp)) { fread($fp, 8192); }
fclose($fp);

Expected result:
----------------
431784
431784
431784
431784
431784
431784
431784
431784
431784
423544

Actual result:
--------------
65287872
65296064
65304256
65312448
65320640
65328832
65337024
65345216
65353408
65361600
65369792
65377984
65386176
65394368
65402560
65410752
65418944
65427136
65435328

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65019904 bytes) in /usr/src/myapp/filter.php on line 39

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-12-03 23:10 UTC] cmb@php.net
This happens as of commit 05560b6[1].

<http://git.php.net/?p=php-src.git;a=commit;h=05560b67bc87a2bcbfd5b48a48443a62f3311e7d>.
 [2019-12-04 14:18 UTC] leopold dot jacquot at gmail dot com
The following pull request has been associated:

Patch Name: Add unit test for bug #78902
On GitHub:  https://github.com/php/php-src/pull/4963
Patch:      https://github.com/php/php-src/pull/4963.patch
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Mon Dec 09 04:01:25 2019 UTC