Bug #78902 [NEW]: Memory leak when using stream_filter_append
From: leopold dot jacquot at gmail dot com
Operating system: linux/amd64 (docker)
PHP version: 7.4.0
Package: Streams related
Bug Type: Bug
Bug description:Memory leak when using stream_filter_append
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 bug report at https://bugs.php.net/bug.php?id=78902&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=78902&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=78902&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=78902&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=78902&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=78902&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=78902&r=support
Expected behavior: https://bugs.php.net/fix.php?id=78902&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=78902&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=78902&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=78902&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=78902&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=78902&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=78902&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=78902&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=78902&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=78902&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=78902&r=mysqlcfg
Thread (8 messages)
- leopold dot jacquot at gmail dot com