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:10
Avg. Score:5.0 ± 0.0
Reproduced:10 of 10 (100.0%)
Same Version:8 (80.0%)
Same OS:10 (100.0%)
From: leopold dot jacquot at gmail dot com Assigned:
Status: Closed Package: Streams related
PHP Version: 7.4.0 OS: linux/amd64 (docker)
Private report: No CVE-ID: None
 [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
 [2019-12-16 09:29 UTC] cmb@php.net
The following pull request has been associated:

Patch Name: Fixed bug #78902
On GitHub:  https://github.com/php/php-src/pull/5014
Patch:      https://github.com/php/php-src/pull/5014.patch
 [2020-01-14 08:27 UTC] iretemd at gmail dot com
same in version 7.2.24 ?
If so, will it be fixed in 7.2.x ?
 [2020-01-14 08:28 UTC] bugreports at gmail dot com
> If so, will it be fixed in 7.2.x ?

given that 7.2 is simply EOL the answer is clearly no
 [2020-01-22 21:23 UTC] fredrik dot lindvall at unx dot nu
Can this patch be added to 7.3.13 as well?
 [2020-01-23 13:58 UTC] nikic@php.net
Automatic comment on behalf of dinosaur.liu@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=67421a780d670ea2eec8157c39f2682bb3cfb7dd
Log: Fixed bug #78902
 [2020-01-23 13:58 UTC] nikic@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 00:01:41 2024 UTC