Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race condition when 2 calls copying files to the same nonexistent directory #141

Open
Soulike opened this issue Nov 26, 2021 · 0 comments
Open

Comments

@Soulike
Copy link

Soulike commented Nov 26, 2021

This may be a same bug as #125.

The test case:

const tmpDir = path.join(os.tmpdir(), 'testFolder');

fs.rmSync(tmpDir, {recursive: true, force: true});

// operation A
ncp('./bin', tmpDir, function (err)
{
    if (err)
    {
        return console.error(err);
    }
    console.log('done!');
});

// operation B
ncp('./lib', tmpDir, function (err)
{
    if (err)
    {
        return console.error(err);
    }
    console.log('done!');
});

The execution result is:

[
  [Error: EEXIST: file already exists, mkdir '/tmp/testFolder'] {
    errno: -17,
    code: 'EEXIST',
    syscall: 'mkdir',
    path: '/tmp/testFolder'
  }
]
done!

Both operation A and B first checks the existence of tmpDir and thought tmpDir does not exist. Then both of them will try to create it. One of A and B will success and the other will fail.

The root cause is that the asynchronous call sequence isWritable() -> rmFile() may be violated by another call to ncp()

ncp/lib/ncp.js

Lines 221 to 229 in 6820b0f

function isWritable(path, done) {
fs.lstat(path, function (err) {
if (err) {
if (err.code === 'ENOENT') return done(true);
return done(false);
}
return done(false);
});
}

ncp/lib/ncp.js

Lines 148 to 151 in 6820b0f

isWritable(target, function (writable) {
if (writable) {
return mkDir(dir, target);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant