From 54074fdac77801c41887d0ad45fd19aaeda4bb4d Mon Sep 17 00:00:00 2001 From: David Glasser Date: Thu, 20 Feb 2014 15:34:36 -0800 Subject: [PATCH] Fix crash if populating packages in two processes --- tools/warehouse.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/tools/warehouse.js b/tools/warehouse.js index be6c6015da..999d28cce4 100644 --- a/tools/warehouse.js +++ b/tools/warehouse.js @@ -341,11 +341,24 @@ _.extend(warehouse, { // (whether or not we just downloaded them). (Don't do this if we didn't // print the installing message!) if (newPieces && showInstalling) { + var unlinkIfExists = function (file) { + try { + fs.unlinkSync(file); + } catch (e) { + // If two processes populate the warehouse in parallel, the other + // process may have deleted the fresh file. That's OK! + if (e.code === "ENOENT") + return; + throw e; + } + }; + if (newPieces.tools) { - fs.unlinkSync(warehouse.getToolsFreshFile(newPieces.tools.version)); + unlinkIfExists(warehouse.getToolsFreshFile(newPieces.tools.version)); } _.each(newPieces.packages, function (packageInfo, name) { - fs.unlinkSync(warehouse.getPackageFreshFile(name, packageInfo.version)); + unlinkIfExists( + warehouse.getPackageFreshFile(name, packageInfo.version)); }); }