Merge pull request #4316 from node-red/4221-handle-concurrent-write-file

Ensure storage/util.writeFile handles concurrent write attempts
This commit is contained in:
Nick O'Leary 2023-09-05 15:57:27 +01:00 committed by GitHub
commit 2bc739194e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 23 deletions

View File

@ -71,30 +71,30 @@ function readFile(path,backupPath,emptyResponse,type) {
});
}
const writeFileLocks = {}
module.exports = {
/**
* Write content to a file using UTF8 encoding.
* This forces a fsync before completing to ensure
* the write hits disk.
*/
writeFile: function(path,content,backupPath) {
var backupPromise;
if (backupPath && fs.existsSync(path)) {
backupPromise = fs.copy(path,backupPath);
} else {
backupPromise = Promise.resolve();
writeFile: async function(path,content,backupPath) {
if (!writeFileLocks[path]) {
writeFileLocks[path] = Promise.resolve()
}
const dirname = fspath.dirname(path);
const tempFile = `${path}.$$$`;
return backupPromise.then(() => {
if (backupPath) {
const result = writeFileLocks[path].then(async () => {
var backupPromise;
if (backupPath && fs.existsSync(path)) {
await fs.copy(path,backupPath);
log.trace(`utils.writeFile - copied ${path} TO ${backupPath}`)
}
return fs.ensureDir(dirname)
}).then(() => {
return new Promise(function(resolve,reject) {
const dirname = fspath.dirname(path);
const tempFile = `${path}.$$$`;
await fs.ensureDir(dirname)
await new Promise(function(resolve,reject) {
var stream = fs.createWriteStream(tempFile);
stream.on('open',function(fd) {
stream.write(content,'utf8',function() {
@ -110,10 +110,11 @@ module.exports = {
log.warn(log._("storage.localfilesystem.fsync-fail",{path: tempFile, message: err.toString()}));
reject(err);
});
});
}).then(() => {
})
log.trace(`utils.writeFile - written content to ${tempFile}`)
return new Promise(function(resolve,reject) {
await new Promise(function(resolve,reject) {
fs.rename(tempFile,path,err => {
if (err) {
log.warn(log._("storage.localfilesystem.fsync-fail",{path: path, message: err.toString()}));
@ -122,8 +123,10 @@ module.exports = {
log.trace(`utils.writeFile - renamed ${tempFile} to ${path}`)
resolve();
})
});
});
})
})
writeFileLocks[path] = result.catch(() => {})
return result
},
readFile: readFile,

View File

@ -14,11 +14,33 @@
* limitations under the License.
**/
var should = require("should");
var NR_TEST_UTILS = require("nr-test-utils");
var util = NR_TEST_UTILS.require("@node-red/runtime/lib/storage/localfilesystem/util");
const should = require("should");
const NR_TEST_UTILS = require("nr-test-utils");
const util = NR_TEST_UTILS.require("@node-red/runtime/lib/storage/localfilesystem/util");
const { mkdtemp, readFile } = require('fs/promises');
const { join } = require('path');
const { tmpdir } = require('os');
describe('storage/localfilesystem/util', function() {
describe('writeFile', function () {
it('manages concurrent calls to modify the same file', async function () {
const testDirectory = await mkdtemp(join(tmpdir(), 'nr-test-'));
const testFile = join(testDirectory, 'foo.txt')
const testBackupFile = testFile + '.$$$'
let counter = 0
const promises = [
util.writeFile(testFile, `update-${counter++}`, testBackupFile ),
util.writeFile(testFile, `update-${counter++}`, testBackupFile ),
util.writeFile(testFile, `update-${counter++}`, testBackupFile )
]
await Promise.all(promises)
const result = await readFile(testFile, { encoding: 'utf-8' })
result.should.equal('update-2')
})
})
describe('parseJSON', function() {
it('returns parsed JSON', function() {
var result = util.parseJSON('{"a":123}');