Skip to content

Commit

Permalink
MOBILE-4653 core: Fix race condition when uploading files
Browse files Browse the repository at this point in the history
Uploading several files in parallel can cause a race condition in the server when trying to insert the directory data in the 'files' table of the DB, in the create_directory function. To avoid that, now files are uploaded 1 by 1 in the same area.
  • Loading branch information
dpalou committed Nov 7, 2024
1 parent 17c387e commit 14a4b83
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/core/features/fileuploader/services/fileuploader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,8 @@ export class CoreFileUploaderProvider {
}
});

await Promise.all(filesToUpload.map(async (file) => {
// Upload files 1 by 1 to avoid race conditions in the server.
for (const file of filesToUpload) {
// Make sure the file name is unique in the area.
const name = CoreFile.calculateUniqueName(usedNames, file.name);
usedNames[name] = file;
Expand All @@ -649,7 +650,7 @@ export class CoreFileUploaderProvider {
const options = this.getFileUploadOptions(filePath, name, undefined, false, 'draft', itemId);

await this.uploadFile(filePath, options, undefined, siteId);
}));
}
}

/**
Expand Down Expand Up @@ -742,15 +743,12 @@ export class CoreFileUploaderProvider {
// Upload only the first file first to get a draft id.
const itemId = await this.uploadOrReuploadFile(files[0], 0, component, componentId, siteId);

const promises: Promise<number>[] = [];

// Upload files 1 by 1 to avoid race conditions in the server.
for (let i = 1; i < files.length; i++) {
const file = files[i];
promises.push(this.uploadOrReuploadFile(file, itemId, component, componentId, siteId));
await this.uploadOrReuploadFile(file, itemId, component, componentId, siteId);
}

await Promise.all(promises);

return itemId;
}

Expand Down

0 comments on commit 14a4b83

Please sign in to comment.