From c1d50e82e1bbf09f41626066f9fac90076a99cd4 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 17 Sep 2018 10:31:00 +0100 Subject: [PATCH] Fix race condition in non-cache lfs context Fixes #1888 --- red/runtime/nodes/context/localfilesystem.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/red/runtime/nodes/context/localfilesystem.js b/red/runtime/nodes/context/localfilesystem.js index a507ce2b4..0e6f8735b 100644 --- a/red/runtime/nodes/context/localfilesystem.js +++ b/red/runtime/nodes/context/localfilesystem.js @@ -140,9 +140,9 @@ function stringify(value) { function LocalFileSystem(config){ this.config = config; this.storageBaseDir = getBasePath(this.config); + this.writePromise = Promise.resolve(); if (config.hasOwnProperty('cache')?config.cache:true) { this.cache = MemoryStore({}); - this.writePromise = Promise.resolve(); } this.pendingWrites = {}; this.knownCircularRefs = {}; @@ -219,9 +219,8 @@ LocalFileSystem.prototype.close = function(){ clearTimeout(this._pendingWriteTimeout); delete this._pendingWriteTimeout; this.flushInterval = 0; - return this.writePromise; } - return Promise.resolve(); + return this.writePromise; } LocalFileSystem.prototype.get = function(scope, key, callback) { @@ -290,7 +289,7 @@ LocalFileSystem.prototype.set = function(scope, key, value, callback) { } else if (callback && typeof callback !== 'function') { throw new Error("Callback must be a function"); } else { - loadFile(storagePath + ".json").then(function(data){ + self.writePromise = self.writePromise.then(function() { return loadFile(storagePath + ".json") }).then(function(data){ var obj = data ? JSON.parse(data) : {} if (!Array.isArray(key)) { key = [key];