saveObjectToFile() writes the object to a temporary file first, and then uses asynchronous fs.rename() to move the temporary file to the target file:
|
fs.writeFile(tmpFileName, json, 'utf8', (err) => { |
|
if (err) return cb(err); |
|
|
|
fs.rename(tmpFileName, file, cb); |
|
}); |
|
}); |
If 2 fs.rename()s are called at almost the same time, there will be a race condition, and the execution result is not determined. One of them will lose its operations.
db.save("id", {number: 1}, function (err)
{
if (err)
{
console.error(err);
}
});
db.save("id", {number: 2}, function (err)
{
if (err)
{
console.error(err);
}
});
// {"number":1} or {"number":2} in id.json
This problem affects both save() and delete().
saveObjectToFile()writes the object to a temporary file first, and then uses asynchronousfs.rename()to move the temporary file to the target file:json-file-store/Store.es6.js
Lines 78 to 83 in 6aada66
If 2
fs.rename()s are called at almost the same time, there will be a race condition, and the execution result is not determined. One of them will lose its operations.This problem affects both
save()anddelete().