This repository was archived by the owner on Aug 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Possible race condition in saveObjectToFile() #22
Copy link
Copy link
Open
Description
saveObjectToFile() writes the object to a temporary file first, and then uses asynchronous fs.rename() to move the temporary file to the target file:
Lines 78 to 83 in 6aada66
| 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.jsonThis problem affects both save() and delete().
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels