Fix recent CPU spikes from image encoding.

Certain animated images take an exceedingly long time (~minutes) to
encode to WEBP using method=6. Such a file was obtained, and it
encoded significantly faster using method=5 and somehow turned out
smaller than the result generated with method=6. We switch now to
use method=5 so we stop getting occasional massive increases in
CPU load when multiple users upload animations at the same time.

Additionally, a timeout was added to the ImageMagick subprocess just
in case there are even more pathological files in the future. Better
to error out than have to get someone to kill the process.
remotes/1693176582716663532/tmp_refs/heads/watchparty
Snakes 2022-11-06 00:48:54 -04:00
parent 84f3805aa6
commit 9f7adb2614
Signed by: Snakes
GPG Key ID: E745A82778055C7E
2 changed files with 7 additions and 3 deletions

View File

@ -454,6 +454,7 @@ MAX_IMAGE_AUDIO_SIZE_MB = 8
MAX_IMAGE_AUDIO_SIZE_MB_PATRON = 16
MAX_VIDEO_SIZE_MB = 32
MAX_VIDEO_SIZE_MB_PATRON = 64
MAX_IMAGE_CONVERSION_TIMEOUT = 15 # seconds
ANTISPAM_BYPASS_IDS = ()

View File

@ -127,15 +127,18 @@ def process_image(filename=None, resize=0, trim=False, uploader=None, patron=Fal
abort(413, f"Max image/audio size is {MAX_IMAGE_AUDIO_SIZE_MB} MB ({MAX_IMAGE_AUDIO_SIZE_MB_PATRON} MB for paypigs)")
with Image.open(filename) as i:
params = ["convert", "-coalesce", filename, "-quality", "88", "-define", "webp:method=6", "-strip", "-auto-orient"]
params = ["convert", "-coalesce", filename, "-quality", "88", "-define", "webp:method=5", "-strip", "-auto-orient"]
if trim and len(list(Iterator(i))) == 1:
params.append("-trim")
if resize and i.width > resize:
params.extend(["-resize", f"{resize}>"])
params.append(filename)
subprocess.run(params)
try:
subprocess.run(params, timeout=MAX_IMAGE_CONVERSION_TIMEOUT)
except subprocess.TimeoutExpired:
abort(413, ("An uploaded image took too long to convert to WEBP. "
"Consider uploading elsewhere."))
if resize:
if os.stat(filename).st_size > MAX_IMAGE_SIZE_BANNER_RESIZED_KB * 1024: