Skip to content
This repository was archived by the owner on Aug 3, 2022. It is now read-only.

Possible race condition in saveObjectToFile() #22

@Soulike

Description

@Soulike

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().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions