fireworm icon indicating copy to clipboard operation
fireworm copied to clipboard

All events are broadcasted twice

Open JsonHunt opened this issue 10 years ago • 8 comments

adding, removing, changing any file matching glob will trigger two events of respective kind

JsonHunt avatar Feb 14 '15 13:02 JsonHunt

Not able to reproduce with my basic manual tests. Could you give more detail? Perhaps a gist or a repo that reproduces it.

airportyh avatar Feb 18 '15 07:02 airportyh

I'm running it on Windows... I'll try to get more details to you when I have a moment

JsonHunt avatar Feb 18 '15 22:02 JsonHunt

here's a gist that reproduces this issue

https://gist.github.com/hexparrot/fc431960d08749d9061b

hexparrot avatar Nov 12 '15 18:11 hexparrot

The bug is somewhere here: https://github.com/airportyh/fireworm/blob/master/lib/dir.js#L57

It's possible that there's some sort of race condition. Do we need to use some sort of mutex here?

Vanuan avatar Feb 14 '16 01:02 Vanuan

Yeah, so if entryNames contains multiple duplicates, multiple fs.stat would be called, resulting in multiple emit signals.

Vanuan avatar Feb 14 '16 01:02 Vanuan

i.e., you need to use the sync version of fs.stat to avoid the bug.

Vanuan avatar Feb 14 '16 01:02 Vanuan

Here's a simpler code that demonstrates a problem.

Array.prototype.includes = function(el) {
  return !(this.indexOf(el) == -1);
}

In the synchronous mode nothing unusual happens:

var seen = [];

['file1', 'file1'].forEach(function(filename) {
  if(!seen.includes(filename)) {
    console.log(filename);
    seen.push(filename);
  }
});

file1 is printed once as expected. But when we add asynchrony, the disaster occurs:

var seen = [];

['file1', 'file1'].forEach(function(filename, i) {
  if(!seen.includes(filename)) { 
    setTimeout(function() { 
      console.log(filename);
      seen.push(filename);
    }, 1000);
  } 
});

Vanuan avatar Feb 14 '16 02:02 Vanuan

Ok, noticed this: https://github.com/airportyh/fireworm/blob/master/lib/dir.js#L110

So the bug isn't there. The bug is that there's no such check for directories, so they're added twice, resulting in duplicate files.

Vanuan avatar Feb 14 '16 03:02 Vanuan