[php-src] Issue #8548: memory leak when using fopen with a stream wrapper
| From: | manueldimmler | Date: | Fri, 13 May 2022 12:10:21 +0000 |
| Subject: | [php-src] Issue #8548: memory leak when using fopen with a stream wrapper | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-241483@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/8548
Author: manueldimmler
### Description
The following code takes up a lot of memory and keeps increasing in a endless loop:
```php
#!/usr/bin/php
<?php
class Wrapper
{
public function stream_open(string $path, string $mode, int $options): bool
{
return true;
}
}
$filename = tempnam(__DIR__, '');
touch($filename);
$wrapper = new Wrapper();
$counter = 0;
while ($counter < 1_000_000) {
$counter++;
stream_wrapper_register('symfony', Wrapper::class);
fopen('symfony://' . $filename, 'r');
stream_wrapper_unregister('symfony');
}
```

With a small change, the problem disappears:
```php
#!/usr/bin/php
<?php
class Wrapper
{
public function stream_open(string $path, string $mode, int $options): bool
{
return true;
}
}
$filename = tempnam(__DIR__, '');
touch($filename);
$wrapper = new Wrapper();
$counter = 0;
stream_wrapper_register('symfony', Wrapper::class);
while ($counter < 1_000_000) {
$counter++;
fopen('symfony://' . $filename, 'r');
}
stream_wrapper_unregister('symfony');
```

The memory leak was described in the following ticket: https://github.com/symfony/symfony/issues/46256
### PHP Version
PHP 8.1.5 (cli)
### Operating System
Ubuntu 20.04