Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory Leak in openFile() with uv Driver #88

Open
Fneufneu opened this issue Dec 12, 2024 · 0 comments
Open

Memory Leak in openFile() with uv Driver #88

Fneufneu opened this issue Dec 12, 2024 · 0 comments

Comments

@Fneufneu
Copy link

Fneufneu commented Dec 12, 2024

Hi,

I have a memory leak in my daemon and i think i found the cause: openFile().
Memory does not go up if i replace it with a new WritableResourceStream(\fopen($filename, 'w'));

Code to reproduce (taken from a amphp/socket issue)

<?php

use Revolt\EventLoop;
use Amp\File\File;
use function Amp\File\openFile;
use function Amp\Socket\listen;

require_once(__DIR__ . '/vendor/autoload.php');

EventLoop::repeat(10, function () {
    echo 'Counters: '. Test::$constructInc . ' - ' . Test::$destructInc . PHP_EOL;
    echo 'Memory: ' . round(memory_get_usage() / 1024, 2) . ' KB' . PHP_EOL;
});

Amp\async(function () {
    $uri = "tcp://127.0.0.1:1337";
    $server = listen($uri);
    $clients = new \WeakMap();
    while ($socket = $server->accept()) {
        $obj = new Test($socket);
        $clients[$obj] = true;
        EventLoop::queue($obj->run(...));
    }
})->await();

class Test
{
    public static int $constructInc = 0;
    public static int $destructInc = 0;
    public string $content;
    public ?File $file = null;

    public function __construct(private \Amp\Socket\ResourceSocket $socket)
    {
        $this->content = str_repeat('a', 2048);
        self::$constructInc++;
    }

    public function __destruct()
    {
        self::$destructInc++;
    }

    public function run()
    {
        $this->file = openFile(rtrim(sys_get_temp_dir(), '/') . '/leaktest-' . mt_rand(), 'w');
        Amp\delay(mt_rand(0, 3));
        $this->socket->write('bye');
        $this->socket->end();
        $this->file->close();
    }
}

Will show:

Counters: 0 - 0
Memory: 1313.06 KB
Counters: 4 - 3
Memory: 1638.35 KB
Counters: 4 - 3
Memory: 1638.35 KB
Counters: 13 - 12
Memory: 1645.09 KB
Counters: 22 - 18
Memory: 1768.31 KB
Counters: 43 - 42
Memory: 1670.28 KB
Counters: 43 - 42
Memory: 1670.28 KB
Counters: 43 - 42
Memory: 1670.28 KB
Counters: 134 - 117
Memory: 2259.37 KB
Counters: 231 - 219
Memory: 2141.72 KB
Counters: 267 - 266
Memory: 1791.28 KB
Counters: 267 - 266
Memory: 1791.28 KB
Counters: 347 - 328
Memory: 2424.6 KB
Counters: 425 - 422
Memory: 1946.41 KB
Counters: 425 - 424
Memory: 1860.41 KB
Counters: 425 - 424
Memory: 1860.41 KB
Counters: 425 - 424
Memory: 1860.41 KB

You can spam connections with: while : ; do (echo | nc localhost 1337 ) & sleep .1 ; done

The memory will grow up until 128M default max limit and the process will crash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant