craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.22k stars 627 forks source link

Large temp folder due to failing thumbnail generation (in the admin) #3335

Closed bramus closed 5 years ago

bramus commented 5 years ago

Description

For a Craft project of mine I have defined the Asset Volumes to be S3 Volumes. This way there are no huge disk space requirements on the server hosting the Craft project, as the files are stored on S3. (For reference: there are already 5GB worth of photos uploaded into the bucket)

Combined with imgix for resizing the original images (which are huge) I've moved away some more load from the server as the CPU-expensive image transforms + caching needs are now imgix's thing to tackle.

To my surprise however I was getting disk usage warnings from my server: the project was using a whopping 7.6GB of disk space.

Looking onto the server I noticed that the culprit is the storage/runtime/temp/ folder, which takes up 5.7GB:

account@server [~]# du -h --max-depth=1 storage/runtime/
967M    storage/runtime/assets
1.3M    storage/runtime/compiled_templates
4.0K    storage/runtime/mutex
41M     storage/runtime/debug
24K     storage/runtime/compiled_classes
8.0K    storage/runtime/URI
12K     storage/runtime/imager
104K    storage/runtime/HTML
14M     storage/runtime/composer
5.7G    storage/runtime/temp
492K    storage/runtime/cache

6.7G    storage/runtime/

Digging deeper into that big storage/runtime/temp/ folder, I notice that it's filled with some – but not all – of the images the client has uploaded. Above that those files appear in there a lot of times. Here's a list of just one of the files:

account@server [~/storage/runtime/temp]# ls -Alh ZWIJN*
-rw-r--r-- 1 account account 11M Sep  3 13:35 ZWIJNDRECHT-06b5b8d1c77ef6e24.01389254.jpg
-rw-r--r-- 1 account account 11M Sep  3 13:36 ZWIJNDRECHT-06b5b8d1cc3d205f6.71652631.jpg
-rw-r--r-- 1 account account 11M Sep  3 13:39 ZWIJNDRECHT-06b5b8d1d7ace26a7.06016241.jpg
-rw-r--r-- 1 account account 11M Sep  3 13:40 ZWIJNDRECHT-06b5b8d1d966f1f13.19808347.jpg
-rw-r--r-- 1 account account 11M Sep  3 13:41 ZWIJNDRECHT-06b5b8d1dc8f33569.59978029.jpg
-rw-r--r-- 1 account account 11M Sep  3 13:42 ZWIJNDRECHT-06b5b8d1e3017bc96.99343662.jpg
-rw-r--r-- 1 account account 11M Sep  3 13:43 ZWIJNDRECHT-06b5b8d1e77b61494.95876686.jpg
-rw-r--r-- 1 account account 11M Sep  3 13:45 ZWIJNDRECHT-06b5b8d1ef4996b41.56600078.jpg
-rw-r--r-- 1 account account 11M Sep  3 13:52 ZWIJNDRECHT-06b5b8d2068881e34.11723945.jpg
-rw-r--r-- 1 account account 11M Sep  3 13:56 ZWIJNDRECHT-06b5b8d2159b1e7b0.38374182.jpg
-rw-r--r-- 1 account account 11M Sep  3 13:56 ZWIJNDRECHT-06b5b8d217249f741.64702242.jpg
-rw-r--r-- 1 account account 11M Sep  3 14:04 ZWIJNDRECHT-06b5b8d2359ad5ef3.87878231.jpg
-rw-r--r-- 1 account account 11M Sep  3 14:10 ZWIJNDRECHT-06b5b8d24af445c61.04086205.jpg
-rw-r--r-- 1 account account 11M Sep  3 14:12 ZWIJNDRECHT-06b5b8d252ed21266.89643619.jpg
-rw-r--r-- 1 account account 11M Sep  3 14:42 ZWIJNDRECHT-06b5b8d2c4b84af73.57466355.jpg
-rw-r--r-- 1 account account 11M Sep  3 16:39 ZWIJNDRECHT-06b5b8d47b8dca840.82324013.jpg
-rw-r--r-- 1 account account 11M Sep  3 16:40 ZWIJNDRECHT-06b5b8d47d1c21101.33280858.jpg
-rw-r--r-- 1 account account 11M Sep  3 16:41 ZWIJNDRECHT-06b5b8d4819c1b9a7.98165116.jpg
-rw-r--r-- 1 account account 11M Sep  3 16:46 ZWIJNDRECHT-06b5b8d493012d7a9.10046565.jpg
-rw-r--r-- 1 account account 11M Sep  3 16:46 ZWIJNDRECHT-06b5b8d49531704f5.28198582.jpg
-rw-r--r-- 1 account account 11M Sep  3 16:52 ZWIJNDRECHT-06b5b8d4abcebaf39.85804664.jpg
-rw-r--r-- 1 account account 11M Sep  3 16:53 ZWIJNDRECHT-06b5b8d4add319aa8.08035948.jpg
-rw-r--r-- 1 account account 11M Sep  3 16:53 ZWIJNDRECHT-06b5b8d4af8d470c0.14023634.jpg
-rw-r--r-- 1 account account 11M Sep  3 16:54 ZWIJNDRECHT-06b5b8d4b0eb72945.53374591.jpg
-rw-r--r-- 1 account account 11M Sep  3 16:56 ZWIJNDRECHT-06b5b8d4b820cc579.04059330.jpg
-rw-r--r-- 1 account account 11M Sep  3 17:41 ZWIJNDRECHT-06b5b8d562d8617f3.39158997.jpg
-rw-r--r-- 1 account account 11M Sep  3 17:44 ZWIJNDRECHT-06b5b8d56dd115396.78682068.jpg
-rw-r--r-- 1 account account 11M Sep  3 17:54 ZWIJNDRECHT-06b5b8d59383ae2c1.55426208.jpg
-rw-r--r-- 1 account account 11M Sep  3 18:00 ZWIJNDRECHT-06b5b8d5ab79df523.45229513.jpg
-rw-r--r-- 1 account account 11M Sep  3 18:01 ZWIJNDRECHT-06b5b8d5ad1a4a2b5.05009678.jpg
-rw-r--r-- 1 account account 11M Sep  3 18:56 ZWIJNDRECHT-06b5b8d67b9da8485.07488663.jpg
-rw-r--r-- 1 account account 11M Sep  4 09:38 ZWIJNDRECHT-06b5b8e366e8e4810.54481315.jpg
-rw-r--r-- 1 account account 11M Sep  4 09:39 ZWIJNDRECHT-06b5b8e36bedc6cb9.97563607.jpg
-rw-r--r-- 1 account account 11M Sep  4 09:40 ZWIJNDRECHT-06b5b8e36df7d4839.65294643.jpg
-rw-r--r-- 1 account account 11M Sep  4 15:48 ZWIJNDRECHT-06b5b8e8d381e3698.55649774.jpg
-rw-r--r-- 1 account account 11M Sep  4 15:50 ZWIJNDRECHT-06b5b8e8d92dfcd82.35288386.jpg
-rw-r--r-- 1 account account 11M Sep  4 15:51 ZWIJNDRECHT-06b5b8e8dc7608222.28030620.jpg
-rw-r--r-- 1 account account 11M Sep  4 16:12 ZWIJNDRECHT-06b5b8e92b7473215.03223230.jpg
-rw-r--r-- 1 account account 11M Sep  4 16:17 ZWIJNDRECHT-06b5b8e940caa1353.87481637.jpg
-rw-r--r-- 1 account account 11M Sep  4 16:20 ZWIJNDRECHT-06b5b8e94aaf056b1.89626041.jpg
-rw-r--r-- 1 account account 11M Sep  4 16:22 ZWIJNDRECHT-06b5b8e9508911676.94769597.jpg
-rw-r--r-- 1 account account 11M Sep  4 16:28 ZWIJNDRECHT-06b5b8e96a9e87101.47540287.jpg
-rw-r--r-- 1 account account 11M Sep  4 16:32 ZWIJNDRECHT-06b5b8e9795982df6.70115852.jpg
-rw-r--r-- 1 account account 11M Sep  4 16:33 ZWIJNDRECHT-06b5b8e97ae073411.85424654.jpg
-rw-r--r-- 1 account account 11M Sep  4 16:41 ZWIJNDRECHT-06b5b8e998c3f4846.39501595.jpg
-rw-r--r-- 1 account account 11M Sep  4 16:43 ZWIJNDRECHT-06b5b8e9a051c1778.23717799.jpg
-rw-r--r-- 1 account account 11M Sep 11 13:07 ZWIJNDRECHT-06b5b97a202dd5079.81089722.jpg
-rw-r--r-- 1 account account 11M Sep 11 13:09 ZWIJNDRECHT-06b5b97a279af26a8.62156619.jpg
-rw-r--r-- 1 account account 11M Sep 17 12:10 ZWIJNDRECHT-06b5b9f7dab8b05f3.27064462.jpg
-rw-r--r-- 1 account account 11M Sep 17 16:13 ZWIJNDRECHT-06b5b9fb687ce5ce6.14412329.jpg
-rw-r--r-- 1 account account 11M Sep 17 16:25 ZWIJNDRECHT-06b5b9fb9676fdba2.08657524.jpg
-rw-r--r-- 1 account account 11M Sep 17 16:30 ZWIJNDRECHT-06b5b9fba78c4de11.55500713.jpg
-rw-r--r-- 1 account account 11M Sep 17 16:30 ZWIJNDRECHT-06b5b9fba9745ce52.67239777.jpg
-rw-r--r-- 1 account account 11M Sep 17 16:31 ZWIJNDRECHT-06b5b9fbab2f34e65.99271358.jpg
-rw-r--r-- 1 account account 11M Sep 17 16:41 ZWIJNDRECHT-06b5b9fbd234faf99.05700188.jpg
-rw-r--r-- 1 account account 11M Sep 17 16:45 ZWIJNDRECHT-06b5b9fbdf491aa66.68287390.jpg
-rw-r--r-- 1 account account 11M Sep 17 16:50 ZWIJNDRECHT-06b5b9fbf2527da62.67610228.jpg
-rw-r--r-- 1 account account 11M Sep 17 16:53 ZWIJNDRECHT-06b5b9fbfcae90719.50136099.jpg
-rw-r--r-- 1 account account 11M Sep 17 17:05 ZWIJNDRECHT-06b5b9fc2ba88b954.08683562.jpg
-rw-r--r-- 1 account account 11M Sep 19 17:55 ZWIJNDRECHT-06b5ba27153a01e55.01702789.jpg
-rw-r--r-- 1 account account 11M Sep 19 17:55 ZWIJNDRECHT-06b5ba2718862a661.26958878.jpg
-rw-r--r-- 1 account account 11M Sep 19 17:57 ZWIJNDRECHT-06b5ba271e5632e33.88687205.jpg
-rw-r--r-- 1 account account 11M Sep 19 18:01 ZWIJNDRECHT-06b5ba272ed45ae10.05818928.jpg
-rw-r--r-- 1 account account 11M Sep 19 18:02 ZWIJNDRECHT-06b5ba27315267513.80055302.jpg
-rw-r--r-- 1 account account 11M Sep 27 13:39 ZWIJNDRECHT-06b5bacc178e92756.15845342.jpg
-rw-r--r-- 1 account account 11M Sep 27 13:41 ZWIJNDRECHT-06b5bacc1ea9a84a7.16101916.jpg
-rw-r--r-- 1 account account 11M Sep 27 13:41 ZWIJNDRECHT-06b5bacc1fcdeac94.31619688.jpg
-rw-r--r-- 1 account account 11M Sep 27 14:35 ZWIJNDRECHT-06b5bacce8e50a6d9.18508924.jpg
-rw-r--r-- 1 account account 11M Sep 27 15:01 ZWIJNDRECHT-06b5bacd4c2b52de1.93403238.jpg
-rw-r--r-- 1 account account 11M Sep 27 15:03 ZWIJNDRECHT-06b5bacd50d85dec1.13075064.jpg
-rw-r--r-- 1 account account 11M Sep 27 15:08 ZWIJNDRECHT-06b5bacd667c3daa0.04251756.jpg
-rw-r--r-- 1 account account 11M Sep 27 15:57 ZWIJNDRECHT-06b5bace1ccd583f3.65303212.jpg
-rw-r--r-- 1 account account 11M Sep 27 16:04 ZWIJNDRECHT-06b5bace36a1e67c8.53962797.jpg
-rw-r--r-- 1 account account 11M Sep 27 16:04 ZWIJNDRECHT-06b5bace375c15c15.29536972.jpg
-rw-r--r-- 1 account account 11M Sep 27 16:04 ZWIJNDRECHT-06b5bace3834dcdb5.66311481.jpg
-rw-r--r-- 1 account account 11M Sep 27 16:05 ZWIJNDRECHT-06b5bace39515d7a6.22191507.jpg
-rw-r--r-- 1 account account 11M Sep 27 16:15 ZWIJNDRECHT-06b5bace61b5f7799.00407586.jpg
-rw-r--r-- 1 account account 11M Sep 28 13:42 ZWIJNDRECHT-06b5bae13a9db9cd6.14025396.jpg
-rw-r--r-- 1 account account 11M Sep 28 13:47 ZWIJNDRECHT-06b5bae14dcb48e45.35373295.jpg
-rw-r--r-- 1 account account 11M Sep 28 13:48 ZWIJNDRECHT-06b5bae15298b00f3.12517710.jpg
-rw-r--r-- 1 account account 11M Sep 28 13:49 ZWIJNDRECHT-06b5bae1548df7708.67265694.jpg
-rw-r--r-- 1 account account 11M Sep 28 13:50 ZWIJNDRECHT-06b5bae157543bb01.87232172.jpg
-rw-r--r-- 1 account account 11M Sep 28 13:50 ZWIJNDRECHT-06b5bae158dd4ef86.83717296.jpg
-rw-r--r-- 1 account account 11M Sep 28 13:51 ZWIJNDRECHT-06b5bae15bade1db2.67846135.jpg
-rw-r--r-- 1 account account 11M Sep 28 15:02 ZWIJNDRECHT-06b5bae2679224ea6.62832390.jpg
-rw-r--r-- 1 account account 11M Sep 28 15:11 ZWIJNDRECHT-06b5bae28983697d6.79189673.jpg
-rw-r--r-- 1 account account 11M Sep 28 15:12 ZWIJNDRECHT-06b5bae28ab8b8d17.25981368.jpg
-rw-r--r-- 1 account account 11M Sep 28 15:12 ZWIJNDRECHT-06b5bae28c55f5b23.56467861.jpg
-rw-r--r-- 1 account account 11M Sep 28 15:13 ZWIJNDRECHT-06b5bae28de612d93.22046074.jpg
-rw-r--r-- 1 account account 11M Sep 28 15:14 ZWIJNDRECHT-06b5bae294d52ff02.09905487.jpg
-rw-r--r-- 1 account account 11M Sep 28 15:15 ZWIJNDRECHT-06b5bae2963936cf0.64717385.jpg
-rw-r--r-- 1 account account 11M Sep 28 15:16 ZWIJNDRECHT-06b5bae29ae9cb9f1.45206007.jpg
-rw-r--r-- 1 account account 11M Sep 28 15:41 ZWIJNDRECHT-06b5bae2f75e12857.13009704.jpg
-rw-r--r-- 1 account account 11M Sep 28 15:42 ZWIJNDRECHT-06b5bae2fb7c3cae9.99887660.jpg
-rw-r--r-- 1 account account 11M Sep 28 16:09 ZWIJNDRECHT-06b5bae361e2abf08.06931710.jpg
-rw-r--r-- 1 account account 11M Sep 28 16:23 ZWIJNDRECHT-06b5bae3975b92cf9.34546282.jpg
-rw-r--r-- 1 account account 11M Sep 28 16:25 ZWIJNDRECHT-06b5bae39bdad0281.48503935.jpg
-rw-r--r-- 1 account account 11M Sep 28 16:30 ZWIJNDRECHT-06b5bae3b0c1a5966.24289768.jpg
-rw-r--r-- 1 account account 11M Sep 28 16:32 ZWIJNDRECHT-06b5bae3b6b437f57.26690214.jpg
-rw-r--r-- 1 account account 11M Sep 28 16:35 ZWIJNDRECHT-06b5bae3c15df6831.67736482.jpg

Hmmz … debug modus on:

In short:

The admin needs to generate a thumbnail for the image. Since it it has no knowledge of imgix being available (on the frontend), it performs the resize action itself.

Therefore it downloads the original asset from S3 into storage/runtime/temp/ and starts resizing it. That action however fails (file too big, action taking too much time, …) and the temp downloaded file is left in storage/runtime/temp/.

Next time a thumbnail needs to be shown this thing starts all over again.

Rinse. Repeat.

Steps to reproduce

  1. Upload a huge image asset via the admin
  2. Open a view in the admin where it shows a thumbnail of said image asset
  3. Check the contents of storage/runtime/temp/ after generation has failed, you'll see a temp file left in there.

Additional info

andris-sevcenko commented 5 years ago

I'm unable to reproduce this locally with a 22MB image. To be fair, my thumbnails gets generated. Can you clear out your log folder, reproduce this and see if there's anything in the logs?

The reason I'm asking is that the file should be removed from that folder as soon as it's downloaded and moved to storage/runtime/assets/sources folder, so I'm wondering at which point is the script dying exactly.

bramus commented 5 years ago

It's the memory limit of the server that I'm running into:

[28-Sep-2018 16:36:21 Europe/Brussels] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 16000001 bytes)
in /home/account/vendor/pixelandtonic/imagine/lib/Imagine/Gd/Image.php on line 617

The memory_limit on the server is set to 128M.

andris-sevcenko commented 5 years ago

Ah, I think I see what's going on here.

There's a Craft setting for caching cloud images for generating transforms and thumbnails. (https://docs.craftcms.com/v3/config/config-settings.html#maxcachedcloudimagesize)

So, as you request a thumbnail for an image, the image is pulled down to the server from the remote source and cached locally according to the setting. It defaults to a source image of 2000x2000, so, if you have that setting left at default, Craft tries to resize the source image to that size. At this point, since we're talking about an 11MB image, Craft is unable to process that using GD library with only 128MB of RAM.

There are multiple ways to approach this: 1) You can set that setting to false. Potential drawbacks - no local source is stored, so every new transform or new thumbnail size will warrant a new download. 2) You can increase the memory limit. Potential drawbacks - GD still is slow and the quality can be a bit of a lack-luster. 3) You can switch to Imagick. Potential drawbacks - if you're not increasing the memory limit, Imagick will use a swap file when it runs out of memory which can eat up server space a bit and make Imagick slower.

I would recommend a combination of the last two options.

bramus commented 5 years ago

Thanks for further investigating this, @andris-sevcenko.

I was wondering if you could re-open this issue. Not to try to work around the currently set limitations (which won't be possible), but to have a failsafe in place which prevents the ~/storage/runtime/temp folder from filling up with multiple copies of one and the same file.

A deletion of files that have the same filename(prefix) before the source image is being pulled from the remove source would be nice addition: That way only one copy of a file can be left in ~/storage/runtime/temp, should things go wrong.

andris-sevcenko commented 5 years ago

There we go.

cballenar commented 2 years ago

I'm running into this issue currently 🤔 I'm running latest CraftCMS 3.7.18.2 (latest as of this post) on PHP 7.4.25. I tried to set my Temp folder to be in Google Cloud Storage but that doesn't seem to be working. Instead, my main server is running out of space due to multiple copies of the same images getting stored in the Temp folder.

andris-sevcenko commented 2 years ago

@cballenar can you list a few of the filenames that are multiple copies? Also is there anything relevant in Craft log files?

cballenar commented 2 years ago

@andris-sevcenko thanks for follow up!

Below is the format these files use where the first portion seems to be the actual filename and is then followed by an automatically generated string.

I found that one of these files was created 465 times :|

-rw-r--r--  1 www-data www-data       0 Oct 21 16:28  local-pronaa-16171951695d682.43174614.jpg
-rw-r--r--  1 www-data www-data       0 Oct 21 17:39  local-pronaa-16171a5b5cdada4.22679641.jpg
-rw-r--r--  1 www-data www-data       0 Oct 21 18:06  local-pronaa-16171ac21c05687.95788916.jpg
[...]
-rw-r--r--  1 www-data www-data       0 Oct 26 17:39  minam-equipos-limpieza-publica61783d35c02301.22211731.jpg
-rw-r--r--  1 www-data www-data       0 Oct 26 17:39  minam-equipos-limpieza-publica61783d38950579.94627616.jpg
-rw-r--r--  1 www-data www-data       0 Oct 26 17:39  minam-equipos-limpieza-publica61783d3b426603.80293889.jpg
[...]
-rw-r--r--  1 www-data www-data  124606 Jan 28  2021 'lurifico desague 0601251381ca870.40018390.jfif'
-rw-r--r--  1 www-data www-data  124606 Jan 28  2021 'lurifico desague 06012604cf3e1e5.36797728.jfif'
-rw-r--r--  1 www-data www-data  120317 Jan 28  2021 'lurifico desague 1601255884ec9a3.84250880.jfif'
[...]
-rw-r--r--  1 www-data www-data   39241 Jul 24  2020 'minagri 15f1afafe27d399.69181317.jfif'
-rw-r--r--  1 www-data www-data   40687 Jul 24  2020 'minagri 25f1afb030a21d8.06103496.jfif'
-rw-r--r--  1 www-data www-data   40540 Jul 24  2020 'minagri 35f1afb05233dc7.44841691.jfif'
[...]

I've looked in phperrors.log but didn't find anything associated to this. I had a harder time navigating web.log but I think this one is it:

2021-11-01 00:44:33 [-][-][-][error][yii\web\HttpException:500] yii\web\ServerErrorHttpException: Image transform cannot be created. in /var/www/mywebsite.com/vendor/craftcms/cms/$
Stack trace:
#0 [internal function]: craft\controllers\AssetsController->actionGenerateTransform(50646)
#1 /var/www/mywebsite.com/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#2 /var/www/mywebsite.com/vendor/yiisoft/yii2/base/Controller.php(180): yii\base\InlineAction->runWithParams(Array)
#3 /var/www/mywebsite.com/vendor/craftcms/cms/src/web/Controller.php(190): yii\base\Controller->runAction('generate-transf...', Array)
#4 /var/www/mywebsite.com/vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('generate-transf...', Array)
#5 /var/www/mywebsite.com/vendor/craftcms/cms/src/web/Application.php(274): yii\base\Module->runAction('assets/generate...', Array)
#6 /var/www/mywebsite.com/vendor/craftcms/cms/src/web/Application.php(577): craft\web\Application->runAction('assets/generate...', Array)
#7 /var/www/mywebsite.com/vendor/craftcms/cms/src/web/Application.php(253): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#8 /var/www/mywebsite.com/vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
#9 /var/www/mywebsite.com/web/index.php(21): yii\base\Application->run()
#10 {main}

Does this indicate an issue with permissions? I've seen something funky happen in recent updates where I used to keep all files and directories under myuser:www-data but now some directories are under www-data:www-data. I attempted to change this on a copy of the server and had to kill it cuz it broke Craft pretty bad. I ended up with 500 errors and weird project config files (..0xa) :|

Thanks!

andris-sevcenko commented 2 years ago

@cballenar it's not directly related to the original issue, although it is quite similar. I'll see if we can maybe have garbage collector also prune the temp folder and get rid of any files that are older than, for example, a week or so.

cballenar commented 2 years ago

Thank you! Heads up, these are generating quite rapidly. During my troubleshooting I cleared these files and just a couple days later the server storage was maxed out, which meant ~3Gb were generated in that time. Should I consider bumping up this storage space nonetheless?

andris-sevcenko commented 2 years ago

Do you have a frontend upload form or something like that? Perhaps you can send over your entire log folder to support@craftcms.com and reference this issue?

cballenar commented 2 years ago

Nothing like that :\ In reality only one admin adds content. I will do that! Thank you very much!

cballenar commented 2 years ago

Quick follow up in case anyone stumbles on this in the future. In my case this issue was caused due to a misconfiguration in the permissions of the folders. Some were owned by a different user which led to some things working correctly and others not so much.

Thanks!