From 375a55b0ad28e66536919f9fd7b48e2a1d823a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Thu, 19 Apr 2018 09:52:50 +0200 Subject: [PATCH] Fix race condition when preparing upload folder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before any upload is submitted the upload is registered in a list of known uploads; this is needed to retrieve the upload object at several points of the upload process. When a chunked upload is submitted first a directory to upload all the chunks is created and, once that is done, the chunks are sent; in order to send a chunk the upload object needs to be retrieved from the list of known uploads. When all the active uploads were finished the list of known uploads was cleared. However, an upload is not active until it actually starts sending the data, so while waiting for the upload directory to be created the upload is already in the list of known uploads yet not active. Due to all this, if the active uploads finished while another pending upload was waiting for the upload directory to be created that pending upload would be removed from the list of known uploads too, and once the directory was created and thus the chunks were sent a field of a null upload object would be accessed thus causing a failure. Instead of removing all the known uploads at once when the active uploads finish now each upload is explicitly removed when it finishes. Signed-off-by: Daniel Calviño Sánchez --- apps/files/js/file-upload.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/apps/files/js/file-upload.js b/apps/files/js/file-upload.js index 6bee3c3bdc..824f6a1fd5 100644 --- a/apps/files/js/file-upload.js +++ b/apps/files/js/file-upload.js @@ -576,7 +576,6 @@ OC.Uploader.prototype = _.extend({ * Clear uploads */ clear: function() { - this._uploads = {}; this._knownDirs = {}; }, /** @@ -595,6 +594,19 @@ OC.Uploader.prototype = _.extend({ return null; }, + /** + * Removes an upload from the list of known uploads. + * + * @param {OC.FileUpload} upload the upload to remove. + */ + removeUpload: function(upload) { + if (!upload || !upload.data || !upload.data.uploadId) { + return; + } + + delete this._uploads[upload.data.uploadId]; + }, + showUploadCancelMessage: _.debounce(function() { OC.Notification.show(t('files', 'Upload cancelled.'), {timeout : 7, type: 'error'}); }, 500), @@ -959,6 +971,8 @@ OC.Uploader.prototype = _.extend({ } self.log('fail', e, upload); + self.removeUpload(upload); + if (data.textStatus === 'abort') { self.showUploadCancelMessage(); } else if (status === 412) { @@ -996,6 +1010,8 @@ OC.Uploader.prototype = _.extend({ var that = $(this); self.log('done', e, upload); + self.removeUpload(upload); + var status = upload.getResponseStatus(); if (status < 200 || status >= 300) { // trigger fail handler