On one of our projects we've met an issue with no free disk space. After an investigation it was found that /tmp directory was full of imagemagick_* files. The project uses https://www.drupal.org/project/s3fs
It was found that imagemagick_imagemagick_pre_parse_file_alter() and imagemagick_imagemagick_arguments_alter() create local copies of remote files, but never delete them.
(Temporary copies are deleted by imagemagick_imagemagick_post_save_alter(), but this one is only called on file save, which is not always the case.)
UPD: The latest 8.x-2.x branch uses an event subscriber instead of hooks, but the issue is still the same.
UPD2: Just tested on our project: five temporary files were left in the temp directory after creating a new image media.
| Comment | File | Size | Author |
|---|---|---|---|
| #31 | 3112424-31-8.x-2.x.patch | 3.89 KB | leksat |
| #31 | 3112424-31-8.x-2.x--FAIL.patch | 2.21 KB | leksat |
Comments
Comment #2
leksat commentedComment #3
leksat commentedComment #5
leksat commentedComment #7
leksat commentedComment #9
leksat commentedThe above patch uses
drupal_register_shutdown_function()because there is no way to detect when a temporary file can be removed. Or I could not find one 🤷♂️Comment #10
leksat commentedComment #11
mondrakeExcellent catch, thank you! We need to fix 8.x-3.x first, then backport.
I would prefer to use a static function here. See for example how this was done for Image Effects in #3046522: Remove imagemagick texttowrapper temporary files on shutdown, see commit https://git.drupalcode.org/project/image_effects/commit/3893f53.
We could use
assertCounthere instead, and be more PHPUnit compliant...Comment #12
leksat commented@mondrake, since
file_unmanaged_delete()is deprecated, we will have to use thefile_systemservice, which is already injected intoImagemagickEventSubscriber. That's why I did not use a static method - we have to call a service anyway.Could you please confirm your wish for a static function one more time :)
Comment #13
leksat commentedFor now: used
assertCountand re-rolled on 8.x-3.xComment #16
leksat commentedWhoops, did not notice that 3.x requires Drupal 8.8+
Comment #18
mondrake#12 well that was changed in Image Effects 8.x-3.x likewise, see https://git.drupalcode.org/project/image_effects/blob/8.x-3.x/src/Plugin...
Also, see the
getSupportedExtensionsmethod in the ImagemagickToolkit class, which is a static - we have an example of using the service from the static method, too.I do not know, honestly. What concerns me is that keeping it with the instantiated service in the container may have side effects when running the callbacks in
drupal_register_shutdown_function. Now it works, but in the future who knows... just for an example, if you were to run the callbacks inregister_shutdown_function(i.e. normal PHP, not Drupal), at the stage of executing the callbacks the container is already destructed (reason why we made a change in #3046522-5: Remove imagemagick texttowrapper temporary files on shutdown). OTOH, I see in drupal core usages also with instantiated methods in services.Comment #19
leksat commentedOkay, let's keep it consistent with image_effects 👍
Comment #21
mondrakeThank you @Leksat.
Just a couple minor things, then it's ready:
Let's add an
assertCountfor the files in tmp here too, to show that the file exists before being deleted.Let's use the ::class notation here, so
ImagemagickEventSubscriber::classinstead of the FQCN string.Comment #22
leksat commentedThank you for hints!
Comment #24
mondrakeCrediting
Comment #26
mondrakeCommitted to 8.x-3.x. Leaving open for backport to 8.x-2.x, if necessary. Thanks!
Comment #27
leksat commentedComment #28
leksat commentedComment #30
mondrakeI know, I know... but we need to use the old
file_unmanaged_deletehere since 8.x-2.x is still supporting Drupal core before 8.7.Comment #31
leksat commented🤞
Comment #34
mondrakeCommitted #31 to 8.x-2.x. Thanks!
Comment #35
leksat commented🎉🎉🎉
Thank you for your patience!