From eeef221b4eaf4dd29a73ee1b17500c9d9014975c Mon Sep 17 00:00:00 2001 From: Girish Ramakrishnan Date: Wed, 27 Sep 2017 19:31:07 -0700 Subject: [PATCH] Fix race where pipe finishes before file is created When there are 0 length files, this is easily reproducible. --- src/storage/filesystem.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/storage/filesystem.js b/src/storage/filesystem.js index e42cea07a..0da6dee9d 100644 --- a/src/storage/filesystem.js +++ b/src/storage/filesystem.js @@ -39,6 +39,11 @@ function upload(apiConfig, backupFilePath, sourceStream, callback) { var fileStream = fs.createWriteStream(backupFilePath); + // this pattern is required to ensure that the file got created before 'finish' + fileStream.on('open', function () { + sourceStream.pipe(fileStream); + }); + fileStream.on('error', function (error) { debug('[%s] upload: out stream error.', backupFilePath, error); callback(new BackupsError(BackupsError.EXTERNAL_ERROR, error.message)); @@ -55,8 +60,6 @@ function upload(apiConfig, backupFilePath, sourceStream, callback) { callback(null); }); - - sourceStream.pipe(fileStream); }); }