Bug #78902 [PATCH]: Memory leak when using stream_filter_append

From: Date: Wed, 04 Dec 2019 14:18:24 +0000
Subject: Bug #78902 [PATCH]: Memory leak when using stream_filter_append
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-224064@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78902&edit=1

 ID:                 78902
 Patch added by:     leopold.jacquot@gmail.com
 Reported by:        leopold dot jacquot at gmail dot com
 Summary:            Memory leak when using stream_filter_append
 Status:             Open
 Type:               Bug
 Package:            Streams related
 Operating System:   linux/amd64 (docker)
 PHP Version:        7.4.0
 Block user comment: N
 Private report:     N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[2019-12-03 23:10:33] cmb@php.net

This happens as of commit 05560b6[1].

<http://git.php.net/?p=php-src.git;a=commit;h=05560b67bc87a2bcbfd5b48a48443a62f3311e7d>.

------------------------------------------------------------------------
[2019-12-03 19:33:06] 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


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=78902&edit=1


Thread (8 messages)

« previous php.bugs (#224064) next »