php-src icon indicating copy to clipboard operation
php-src copied to clipboard

ftp_nlist memory leak

Open pyrsmk opened this issue 4 years ago • 6 comments

Description

I have a micro service that is watching a FTP directory every N seconds. I noticed on my VM metrics that I had a memory leak somewhere. It didn't come from my code since the used memory was stable, so it probably came from a PHP extension instead. After narrow down the responsible code, and tested a couple of FTP libraries, I soon realized that it came from ftp_nlist.

Here's a snippet to reproduce the issue, while watching the used memory with watch -n 1 free -m:

<?php

$ftp = ftp_connect('localhost', 2121, 1);
ftp_login($ftp, 'anonymous', 'anonymous');

while (true) {
    ftp_nlist($ftp, '.');
    usleep(1000);
}

I tried to close the connection after each call, but the leak was still the same.

PHP Version

PHP 8.1.1

Operating System

Alpine Linux inside Docker

pyrsmk avatar Feb 22 '22 11:02 pyrsmk

I cannot confirm a memory leak. Since free -m reports the systems free memory, the leak might be caused by the FTP server, or even some unrelated process. So please add something like echo memory_get_usage(), PHP_EOL; to the loop body, or run the script under valgrind (valgrind php script.php; you need to convert the infinite loop to some finite loop in this case). If you do so, is there still a leak?

cmb69 avatar Feb 22 '22 13:02 cmb69

I didn't know about valgrind, thanks! It will help a lot. Here's the summary:

==217== HEAP SUMMARY:
==217==     in use at exit: 0 bytes in 0 blocks
==217==   total heap usage: 176,315 allocs, 176,315 frees, 143,632,034 bytes allocated
==217==
==217== All heap blocks were freed -- no leaks are possible
==217==
==217== For lists of detected and suppressed errors, rerun with: -s

~I found that those results are stable between two runs with a different duration so it does not seems to come from ftp_nlist after all.~

I don't understand where this leak is. I'll dig this up more and will come back later.

edit : the summary was wrong, I'll update it in a few minutes edit 2 : ok, better

pyrsmk avatar Feb 22 '22 14:02 pyrsmk

Oh, sorry, forgot to mention that you should run valgrind with USE_ZEND_ALLOC=0, i.e.:

USE_ZEND_ALLOC=0 valgrind php script.php

cmb69 avatar Feb 22 '22 14:02 cmb69

I updated the results above with this option.

pyrsmk avatar Feb 22 '22 15:02 pyrsmk

Thanks! That currently shows no PHP memory leaks. You can also try:

USE_ZEND_ALLOC=0 ZEND_DONT_UNLOAD_MODULES=1 valgrind php script.php

(or you may want to have a look at https://www.phpinternalsbook.com/php7/memory_management/memory_debugging.html?highlight=zend_dont_unload_modules)

cmb69 avatar Feb 22 '22 15:02 cmb69

After some tests, the memory leak in local seems to come from the tiny Python FTP server I was running. But in production this is completely another story, since the FTP server is running on another server.

I run valgrind with a more complete version of my service to see if it would have some leak somewhere but it does not seem to be the case...

The issue probably comes from my hosting provider.

Thanks for your time !

pyrsmk avatar Feb 22 '22 16:02 pyrsmk