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

Fixed memory leak in HTML5::saveHTML() #187

Merged
merged 1 commit into from
Jun 30, 2020

Conversation

lyrixx
Copy link
Contributor

@lyrixx lyrixx commented Jun 30, 2020

With the following reproducer:

<?php

use Masterminds\HTML5;

require __DIR__.'/vendor/autoload.php';

cli_set_process_title('leak');
$html = file_get_contents('https://www.php.net/');
$html5 = new HTML5();
$dom = $html5->loadHTML($html);
echo "Converting to HTML 5\n";
for ($i=0; $i < 100; $i++) {
    $html5->saveHTML($dom);
    // printf("%.2f\n", memory_get_usage(false) / 1024 / 1024);
}

printf("%.2f\n", memory_get_usage(false) / 1024 / 1024);

Without my patch: 8.51
With my patch: 0.67


Note: there are another leak, but I can not find it for now...
But this patch is really mandatory!

@goetas goetas merged commit ff916f1 into Masterminds:master Jun 30, 2020
@goetas goetas added the bug label Jun 30, 2020
@goetas
Copy link
Member

goetas commented Jun 30, 2020

thanks!

@lyrixx lyrixx deleted the fix-memory-leak branch June 30, 2020 16:15
Copy link

@bburnichon bburnichon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just saw your Tweet and I saw that leaks could also occurs in save method if an exception is thrown.

Working with streams, there should always be a one-one relation between fopen and fclose.

@@ -234,6 +234,10 @@ public function saveHTML($dom, $options = array())
$stream = fopen('php://temp', 'wb');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fopen could return false

        $stream = fopen('php://temp', 'wb');
        if ($stream === false) {
            // Acts accordingly I would throw an exception
            throw new \RuntimeException('Cannot create temporary stream');
        }

        try {
            $this->save($dom, $stream, array_merge($this->defaultOptions, $options));
            return stream_get_contents($stream, -1, 0);
        } finally {
          fclose($stream);
        }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good catch. Would you mind to submit a pr ?
If you can't, no problem, I'll do it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I do not use this library and I am already occupied on other things. Feel free to submit a PR to fix it.

Btw, the same trick should be used in the above method where return from fopen is not checked for correctness.

I would split save method in with a private method doSaveStream(...) and save calling this directly whether $file is a resource or a filename.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arf, this lib should run on PHP 5.3 (yes :p )
I can not do that... I think we will keep this code like this

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$stream = fopen('php://temp', 'wb');
$stream = fopen('php://temp', 'wb');
if ($stream === false) {
// Acts accordingly I would throw an exception
throw new \RuntimeException('Cannot create temporary stream');
}
try {
$this->save($dom, $stream, array_merge($this->defaultOptions, $options));
$result = stream_get_contents($stream, -1, 0);
} catch (\Exception $exception) {
fclose($stream);
throw $exception;
}
fclose($stream);
return $result;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arf, this lib should run on PHP 5.3 (yes :p )
I can not do that... I think we will keep this code like this

I think it would be preferable to update the library to require at least PHP 5.6.

@goetas
Copy link
Member

goetas commented Jul 2, 2020

@lyrixx @mundschenk-at is someone of you interested in providing the fix for the situation where $stream = fopen('php://temp', 'wb'); returns false ? (but keep in mind my comment on #189)

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

Successfully merging this pull request may close these issues.

None yet

4 participants