fix: prevent race condition in localfilesystem context storage during close

pull/5462/head
Dennis-SEG 2026-01-24 23:38:49 +01:00
parent 1019d52f78
commit ca01aa9148
1 changed files with 11 additions and 6 deletions

View File

@ -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() {