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

Nested `CallbackFilterIterator` is leaking memory

Open mvorisek opened this issue 4 years ago • 3 comments

Description

The following code:

https://3v4l.org/3nIOH

https://3v4l.org/YQIVJ/rfc#vgit.master

is expected to output:

 -- c 1
 Array
 (
     [0] => a
     [1] => b
 )
 -- d 1
 -- c 1
 Array
 (
     [0] => a
     [1] => b
 )
 -- d 1
 -- c 1
 Array
 (
     [0] => a
 )
+ -- d 1
 ----------------
- -- d 1

PHP Version

8.0.14, 8.1.1, master (all tested)

Operating System

linux

mvorisek avatar Jan 17 '22 13:01 mvorisek

Can anyone from PHP team confirm this issue? It is probably related to https://github.com/php/php-src/commit/afab9eb48c883766b7870f76f2e2b0a4bd575786 , the issue with CallbackFilterIterator is however fixed, thanks @nikic

mvorisek avatar Feb 01 '22 10:02 mvorisek

Doesn't seem like this is caused by afab9eb48c883766b7870f76f2e2b0a4bd575786. In fact, it fixes the second iteration in your example, which also leaked.

Here's a somewhat condensed example:

class Action
{
    public \Iterator $iterator;

    public function __construct(array $data)
    {
        $this->iterator = new ArrayIterator($data);
        echo '-- c ' . spl_object_id($this) . "\n";
    }

    public function __destruct()
    {
        echo '-- d ' . spl_object_id($this) . "\n";
    }

    public function filter()
    {
        $this->iterator = new \CallbackFilterIterator($this->iterator, fn() => true);
        $this->iterator->rewind();
    }
}

$action = new Action(['a', 'b']);
$action->filter();
$action->filter();
print_r(iterator_to_array($action->iterator));
$action = null;
gc_collect_cycles();
-- c 1
Array
(
    [0] => a
    [1] => b
)
===DONE===
-- d 1
[Thu Feb  3 15:51:02 2022]  Script:  './ext/spl/tests/bug-gh7958.php'
./Zend/zend_closures.c(505) :  Freeing 0x00007f5edee6b900 (328 bytes), script=./ext/spl/tests/bug-gh7958.php
[Thu Feb  3 15:51:02 2022]  Script:  './ext/spl/tests/bug-gh7958.php'
./Zend/zend_objects_API.h(92) :  Freeing 0x00007f5edee84540 (176 bytes), script=./ext/spl/tests/bug-gh7958.php
[Thu Feb  3 15:51:02 2022]  Script:  './ext/spl/tests/bug-gh7958.php'
./Zend/zend_interfaces.c(206) :  Freeing 0x00007f5edee87280 (112 bytes), script=./ext/spl/tests/bug-gh7958.php
=== Total 3 memory leaks detected ===

It's caused by the reference to $this from the CallbackFilterIterator closure. Making the closure static solves the issue. It only happens for nested CallbackFilterIterator iterators.

iluuu1994 avatar Feb 03 '22 15:02 iluuu1994

It's caused by the reference to $this from the CallbackFilterIterator closure. Making the closure static solves the issue. It only happens for nested CallbackFilterIterator iterators.

Yes, it seems ref count is not properly decremented during GC cycle when CallbackFilterIterator is nested - https://3v4l.org/mNt0C/rfc#vgit.master

Can you please mark this issue as verified and do you think it can be fixed in PHP v8.0 or at least v8.1?

mvorisek avatar Feb 04 '22 10:02 mvorisek