Bug #79143 [NEW]: Memory leak with streams and HTTP protocol 1.1
| From: | curtisfarnham at gmail dot com | Date: | Mon, 20 Jan 2020 20:41:30 +0000 |
| Subject: | Bug #79143 [NEW]: Memory leak with streams and HTTP protocol 1.1 | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-225015@lists.php.net to get a copy of this message | ||
From: curtisfarnham at gmail dot com
Operating system: Ubuntu, Alpine, MacOS
PHP version: 7.4.1
Package: Streams related
Bug Type: Bug
Bug description:Memory leak with streams and HTTP protocol 1.1
Description:
------------
When using the "http" stream context and protocol 1.1, there is a
substantial memory leak which begins after 2.00 MB of a stream has been
downloaded. It increases linearly with time and is roughly in
proportion to the amount of data downloaded.
This bug seems to have been introduced in PHP 7.2 but is still there in
7.4.1. I have experienced it on Alpine Linux, Ubuntu Linux, and
MacOS/Homebrew. Using Protocol 1.0 successfully avoids the leak.
Strangely, it also doesn't occur with MacOS Catalina's PHP 7.3.9
binary.
This discussion began at https://github.com/guzzle/guzzle/issues/2555
.
Since this test script discards the contents of the stream, the memory
usage should hold constant over time. It's initially constant, but when
it gets to a cumulative 2097152 bytes downloaded (2.00 MB), the bug
begins to appear and memory starts increasing steadily.
Also, there are two stepwise increases in memory usage, each of exactly
2.00 MB, prior to when the script reaches the aforementioned threshold.
The increases occur at different times depending on the specific
environment.
(Credit goes to Tobias Nyholm and Konstantin Kopachev for helping
troubleshoot this. Thanks!)
Test script:
---------------
<?php
$context = stream_context_create(['http' => [
'protocol_version' => '1.1',
]]);
$stream = fopen('https://kpbs.streamguys1.com/?ck=1579203064304',
'r',
null, $context);
$dl = 0;
while (!feof($stream)) {
$chunk = fread($stream, 8192);
$dl += strlen($chunk);
echo "downloaded $dl, memory " . memory_get_usage(true) . "\n";
}
Expected result:
----------------
downloaded 8192, memory 2097152
downloaded 16384, memory 2097152
downloaded 24576, memory 2097152
...
downloaded 1114112, memory 2097152
downloaded 1122304, memory 2097152
downloaded 1130496, memory 2097152
downloaded 1138688, memory 2097152
...
downloaded 2080768, memory 2097152
downloaded 2088960, memory 2097152
downloaded 2097152, memory 2097152
downloaded 2105344, memory 2097152
downloaded 2113536, memory 2097152
downloaded 2121728, memory 2097152
Actual result:
--------------
downloaded 8192, memory 2097152
downloaded 16384, memory 2097152
downloaded 24576, memory 2097152
...
downloaded 1114112, memory 2097152
downloaded 1122304, memory 2097152
downloaded 1130496, memory 4194304
downloaded 1138688, memory 4194304
...
downloaded 2080768, memory 4194304
downloaded 2088960, memory 4194304
downloaded 2097152, memory 6295552
downloaded 2105344, memory 6303744
downloaded 2113536, memory 6311936
downloaded 2121728, memory 6320128
--
Edit bug report at https://bugs.php.net/bug.php?id=79143&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=79143&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=79143&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=79143&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=79143&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=79143&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=79143&r=support
Expected behavior: https://bugs.php.net/fix.php?id=79143&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=79143&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=79143&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=79143&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=79143&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=79143&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=79143&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=79143&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=79143&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=79143&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=79143&r=mysqlcfg