AvianFlu / ncp

Asynchronous recursive file copying with Node.js.
MIT License
680 stars 103 forks source link

Callback called multiple times, but not always #111

Open pvencill opened 7 years ago

pvencill commented 7 years ago

Not sure how to replicate; I am running a test for another project where I'm using ncp to duplicate a file directory (so the original sample isn't modified for the test). I am getting an error on some (but not all) of my tests that "Done is being called multiple times", and I can verify that behavior by passing my own function in the callback and logging every time it's called. It appears to be just happening on one of the project directories that I'm copying.

In any case, there's no instance ever that a callback should be called more than once, so this is a bug.

jurosh commented 7 years ago

I also noticed strange behavior... seems like longer copy tasks callbacks get called more times (instead of faster copy actions) BUT only when I run it for second time - so that files already exists.

Look at the results, numbers are assigned before staring any copy actions:

C:\bin\commands\backup\backuper>node index.js
STARTED 1||hosts
STARTED 2||.dotvm
STARTED 3||.homestead
STARTED 4||.ssh
STARTED 5||.vagrant.d
STARTED 6||.VirtualBox
STARTED 7||Desktop
STARTED 8||Scanned Documents
STARTED 9||Downloads
STARTED 10||Pictures
STARTED 11||Videos
STARTED 12||.gitconfig
FINISHED 1||hosts
FINISHED 12||.gitconfig
FINISHED 11||Videos
FINISHED 8||Scanned Documents
FINISHED 7||Desktop
FINISHED 6||.VirtualBox
FINISHED 4||.ssh
FINISHED 2||.dotvm
FINISHED 3||.homestead
FINISHED 9||Downloads
FINISHED 10||Pictures
FINISHED 5||.vagrant.d

C:\bin\commands\backup\backuper>node index.js
STARTED 1||hosts
STARTED 2||.dotvm
STARTED 3||.homestead
STARTED 4||.ssh
STARTED 5||.vagrant.d
STARTED 6||.VirtualBox
STARTED 7||Desktop
STARTED 8||Scanned Documents
STARTED 9||Downloads
STARTED 10||Pictures
STARTED 11||Videos
STARTED 12||.gitconfig
FINISHED 1||hosts
FINISHED 12||.gitconfig
FINISHED 8||Scanned Documents
FINISHED 9||Downloads
FINISHED 11||Videos
FINISHED 3||.homestead
FINISHED 2||.dotvm
FINISHED 5||.vagrant.d
FINISHED 5||.vagrant.d
FINISHED 5||.vagrant.d
FINISHED 5||.vagrant.d
FINISHED 5||.vagrant.d
FINISHED 5||.vagrant.d

And ugly debug code:

var count = 0;
config(cm).srcs.forEach(function(src) {
  count ++;
  const srcc = count + '||' + src.split('\\')[src.split('\\').length - 1];
  console.log('STARTED', srcc);
  const target = '_backup\\' + src.replace(cm.SYSTEM_DRIVE + '\\', '');
  const targetDir = target.split(path.sep).slice(0, -1).join(path.sep);
  prepareDirSync(targetDir);
  ncp(src, target, function (err) {
    console.log('FINISHED', srcc);
  });
});
ericis commented 6 years ago

Same issue. Outstanding for a year?

no instance ever that a callback should be called more than once

The Typescript code below ends up logging [DefaultFileReader] copyDirectoryTree() only once, but [DefaultFileReader] ncp callback is logged 5 times.

    copyDirectoryTree(sourceDirectory: string, destinationDirectory: string, callback: () => void, errCallback: (err: NodeJS.ErrnoException) => void) {
        console.log('[DefaultFileReader] copyDirectoryTree()');

        ncp.limit = 16;

        ncp(sourceDirectory, destinationDirectory, err => {
            console.log('[DefaultFileReader] ncp callback');
            if (err) {
                errCallback(err);
                return;
            }

            callback();
        });
    }
ericis commented 6 years ago

Switching to fs-extra copy command worked for me.

https://github.com/jprichardson/node-fs-extra/blob/HEAD/docs/copy.md