You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The test case below can trigger a race condition and cause the copied file corrupted:
consteventEmitter=newEventEmitter();letfinishCount=0;consthash1=crypto.createHash('sha1');consthash2=crypto.createHash('sha1');constdestination=path.join(os.tmpdir(),'bigFile');constbigFile1=path.join(__dirname,'bigFiles','bigFile1');constbigFile2=path.join(__dirname,'bigFiles','bigFile2');constfile1Content=fs.readFileSync(bigFile1);constoriginal1Hash=hash1.update(file1Content).digest();constfile2Content=fs.readFileSync(bigFile2);constoriginal2Hash=hash2.update(file2Content).digest();fs.rmSync(destination,{recursive: true,force: true});// operation Ancp(bigFile1,destination,function(err){if(err){returnconsole.error(err);}eventEmitter.emit('done');console.log('done!');});// operation Bncp(bigFile2,destination,function(err){if(err){returnconsole.error(err);}eventEmitter.emit('done');console.log('done!');});eventEmitter.on('done',()=>{finishCount++;if(finishCount===2){consthash3=crypto.createHash('sha1');constcopiedBigFile=destination;constcopiedFileContext=fs.readFileSync(copiedBigFile);constcopiedHash=hash3.update(copiedFileContext).digest();assert.ok(copiedHash.equals(original1Hash)||copiedHash.equals(original2Hash),`Copied file is different from any original files originalFile1Hash: ${original1Hash.toString('hex')} originalFile2Hash: ${original2Hash.toString('hex')} copiedFileHash: ${copiedHash.toString('hex')} `);}});
The execution result is:
done!
node:assert:400
throw err;
^
AssertionError [ERR_ASSERTION]: Copied file is different from any original files
originalFile1Hash: b9b855fd78c3e68c7e127dcba3db8111be1eea6c
originalFile2Hash: 36c07a7f47f5478248695505f6993e2ecb83b3f5
copiedFileHash: 62736f6530b1903a91113ebe32a2943a2654942e
At the beginning, both A and B think that destination does not exist. So both operation A and B will directly create write streams to destination and write through the streams at the same time, which I think is problematic.
The text was updated successfully, but these errors were encountered:
The test case below can trigger a race condition and cause the copied file corrupted:
The execution result is:
I think the bug is caused by the codes here:
ncp/lib/ncp.js
Lines 113 to 126 in 6820b0f
At the beginning, both A and B think that
destination
does not exist. So both operation A and B will directly create write streams todestination
and write through the streams at the same time, which I think is problematic.The text was updated successfully, but these errors were encountered: