From ca01aa9148440b8b18a9bb5e41282f37447739b3 Mon Sep 17 00:00:00 2001 From: Dennis-SEG Date: Sat, 24 Jan 2026 23:38:49 +0100 Subject: [PATCH] fix: prevent race condition in localfilesystem context storage during close --- .../lib/nodes/context/localfilesystem.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/node_modules/@node-red/runtime/lib/nodes/context/localfilesystem.js b/packages/node_modules/@node-red/runtime/lib/nodes/context/localfilesystem.js index ef499f0f9..5697f329d 100644 --- a/packages/node_modules/@node-red/runtime/lib/nodes/context/localfilesystem.js +++ b/packages/node_modules/@node-red/runtime/lib/nodes/context/localfilesystem.js @@ -155,6 +155,7 @@ function LocalFileSystem(config){ } this.pendingWrites = {}; this.knownCircularRefs = {}; + this.closing = false; if (config.hasOwnProperty('flushInterval')) { this.flushInterval = Math.max(0,config.flushInterval) * 1000; @@ -233,16 +234,19 @@ LocalFileSystem.prototype.open = function(){ LocalFileSystem.prototype.close = function(){ var self = this; - if (this.cache && this._pendingWriteTimeout) { - clearTimeout(this._pendingWriteTimeout); - delete this._pendingWriteTimeout; + this.closing = true; + if (this.cache) { + if (this._pendingWriteTimeout) { + clearTimeout(this._pendingWriteTimeout); + delete this._pendingWriteTimeout; + } this.flushInterval = 0; + // Always flush pending writes on close, even if no timeout was pending self.writePromise = self.writePromise.then(function(){ return self._flushPendingWrites.call(self).catch(function(err) { log.error(log._("context.localfilesystem.error-write",{message:err.toString()})); }); }); - } return this.writePromise; } @@ -298,8 +302,9 @@ LocalFileSystem.prototype.set = function(scope, key, value, callback) { if (this.cache) { this.cache.set(scope,key,value,callback); this.pendingWrites[scope] = true; - if (this._pendingWriteTimeout) { - // there's a pending write which will handle this + if (this._pendingWriteTimeout || this.closing) { + // there's a pending write which will handle this, + // or we're closing and the close() flush will handle it return; } else { this._pendingWriteTimeout = setTimeout(function() {