KnpLabs / snappy

PHP library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage

Home Page:https://knplabs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Snappy pdf calls removeTemporaryFiles() twice.

Chuuone opened this issue · comments

Issue

\Knp\Snappy\AbstractGenerator calls

register_shutdown_function([$this, 'removeTemporaryFiles']); on line 55 in the __construct method.

and then also calls it on __destruct()

This causes the class to loop over $this->temporaryFiles twice when the script starts unloading from memory. Polluting my logs with this exception.

Message: unlink(/tmp/knp_snappy614d88ce67eea7.51081642.html): Filen eller katalogen finns inte
File: /vendor/laravel/framework/src/Illuminate/Filesystem/Filesystem.php
Line: 285

Suggestion

Remove register_shutdown_function or the __destruct magic method from AbstractGenerator class.

If for whatever reason you need to keep these calls as they are, can you make your method removeTemporaryFiles() smarter by using unset() on the array $this->temporaryFiles as you unlink them?

You may consider cleaning up temp files yourself. See this issue.

@ethanclevenger91

The issue you linked seems to be in regards to the library not deleting the temp files at all when its ran in a queue:worker environment as its a long-running instance in such case and the deletion is never called. The issue mentioned by me above is the fact that the snappy library tries to delete the same file twice at the end of the instance life cycle. Once through the register_shutdown_function and once more in the __destruct(). This fills my error logs with "File doesnt exist" errors when snappy performs the 2nd delete attempt. Use one or the other, not both.

The solution mentioned in the other thread will work fine for a queue worker environment and it should probably be cleaned up manually. But there is no reason to setup the class to try to delete the same file twice in the same life cycle.

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Activity

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@Chuuone any luck with this? I have been running into this error in my phpunit testings