-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Gallery: Upload images one at a time #11565
Conversation
Makes mediaUpload() upload image files one at a time. This matches the existing Media Library's behaviour. By waiting for each image to process before starting on the next, the PHP/CGI backend isn't overloaded with requests to resize multiple image files at the same time--something that always causes timeout errors for large images.
Props to @talldan for pairing on this with me! ❤️ |
…ut in the case of mulitple files, valid uploads continue to be processed
Removed the request for review since I think I've spotted an issue. Will pick up with @noisysocks tomorrow. |
Properly mock the modules that mediaUpload() uses so that we can more easily test calling mediaUpload().
I pushed up some changes which:
We should look at writing more tests for We should also look at splitting But all these problems already exist in |
Kind of a bummer that we have to revert this. Look at this 11-year-old ticket from @m. 🙂 |
Is this ready to be reviewed or still in progress? |
@youknowriad Ready to review now. Just giving it a test. |
Spotted a bug—when an image fails validation it's replaced by the next one that passes. e.g. if you try to upload images: and C fails validation you actually end up with the following images in the gallery: I'll debug it now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
This has been a long-standing issue, I'm happy to see it fixed.
(I missed the bug, let me know if you need testing after that) |
@youknowriad - My local branch wasn't up to date 🤦♂️ - looks good, will merge. |
Thanks @talldan and @youknowriad!
Yeah, wow, in five years that ticket will be able to drive. I'm not sure that we are able to solve the general problem here which is that the server can be overloaded from requests to resize images. Server admins would have to configure nginx and php-fpm so that there's a sufficient queue for requests. WordPress doesn't have a whole lot of say in what environment it runs on. I'd say that going forward we could look at making it so that we upload two or three images at a time. But, for now, this fixes the issue and isn't any worse than what's already in Core. |
Wanted to create some issues for the future work identified here.
This already sort of exists: #8810
Created: #11650 |
Fixes #8935.
The problem 🤔
Note: I've tested this all using VVV, but other development and production environments would be similar.
When 5-10 large (10 MB) images are uploaded at once using the Gallery block, it's highly likely that a few
The response was not a valid JSON response.
errors appear on the screen.This is because the server has responded to some of the upload requests with a
502 Bad Gateway
error. The body of these responses is a nginx HTML error page, which explains the 'Invalid JSON' message.Tailing the nginx error log (
/vagrant/www/wordpress-default/log/error.log
) reveals that the error occurs because its connection to php-fpm was reset:Tailing the system log (
/var/syslog
) reveals that this is because the operating system ran out of memory and so decided to kill php-fpm:You can see from the system log that the php-fpm workers were altogether consuming about 1.7 GB of memory—easily enough to cause an OOM in this virtual machine that's configured with 2 GB of RAM:
This is because WordPress generates thumbnails for each image that is uploaded. Generating thumbnails for a large image is very memory intensive. In our case, we're generating thumbnails for several large images at once.
I'm not entirely clear on why the PHP runtime itself doesn't throw an OOM exception from exceeding the
memory_limit
set inphp.ini
. My guess is that it's because image resizing happens in a seperate module running under the php-fpm process.Proposed fix 😮
Both the Upload New Media page and the Media Library both handle uploading multiple images by uploading one image at a time. That is, we wait until the first
POST
request toasync-upload.php
finishes before starting on the second.Borrowing this approach for Gutenberg is a nice fix for the above issue because:
It's also really simple to implement thanks to
async
andawait
.How to test 📋
Future work 📆
There are some unrelated issues that should be fixed separately:
blob:
URL for a non-cachedhttp://
URL.