rjeczalik / notify

File system event notification library on steroids.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Missing events: Not getting any event for the recreated directory

vivek-bansal-VB opened this issue · comments

I have set a watch on directory /home/mydir and created a subdirectory (tmp) under it. I dropped some files in directory /home/mydir/tmp and I got the file create events. Then I deleted the directory (/home/mydir/tmp) and recreated it. Again I dropped some files in the directory (/home/mydir/tmp), but this time I am not getting any events in this directory. Seems a probable bug. Any help is much appreciated.

Could you please share some reproducer? Go snippet to show how you set watches and some example bash commands how you create temp dir and do all these operations. It would be greatly helpful.

`func main() {
WatchFS("/home/mydir")
}

func WatchFS(rootDir string) {
events := make(chan notify.EventInfo, 100)
setupWatch(rootDir, events)
handleFSEventsInfinite(events)
notify.Stop(events)
}

func setupWatch(path string, events chan notify.EventInfo) error {
if err := notify.Watch(path, events, notify.All, notify.Create, notify.InCreate); err != nil {
fmt.Errorf("failed to setup watch on %q : %v.", path, err)
return err
}
return nil
}

func handleFSEventsInfinite(events chan notify.EventInfo) {
for {
ei := <-events
switch event := ei.Event(); event {
case notify.Create, notify.InCreate:
path := ei.Path()
fi, err := os.Stat(path)
if err != nil {
break
}
if fi.IsDir() {
setupWatch(path, events)
} else {
log.Infof("event is a file create event on file: %q", path)
}
default:
log.Infof("Got non-create event %q", event)
}
}
}`

Below is the shell script which I used to run the commands:
sudo mkdir /home/mydir/tmp
cd /home/mydir/tmp
touch a.txt
cd ..
sudo rm -rf tmp
sudo mkdir /home/mydir/tmp
touch b.txt

Output of the above script:
event is a directory create event: "/home/mydir/tmp"
event is a directory create event: "/home/mydir/tmp"
event is a directory create event: "/home/mydir/tmp"
event is a file create event on file: "/home/mydir/tmp/a.txt"
event is a file create event on file: "/home/mydir/tmp/a.txt"
Got non-create event "notify.Remove"
Got non-create event "notify.Remove"
Got non-create event "notify.Remove"
Got non-create event "notify.Remove"
Got non-create event "notify.Remove"
event is a directory create event: "/home/mydir/tmp"
event is a directory create event: "/home/mydir/tmp"
event is a directory create event: "/home/mydir/tmp"
event is a directory create event: "/home/mydir/tmp"

As you can see, there is no event generated for file /home/mydir/tmp/b.txt

I checked this package: https://github.com/fsnotify/fsnotify and it seems to be working fine even when the directory with the same name is recreated.

@vivek-bansal-VB Thanks for the repro! Since I don't have access to macOS nowadays, someone else would need to troubleshoot it.

Out of curiosity, does it reproduce when you build with kqueue instead of FSEvents? go build -tags kqueue ...

@rjeczalik Yes I tried with -tags kqueue and the same issue is there as well.

Other solution for this problem can be: If there would be a function: RemoveWatch(path) exposed by the library. So before deleting the directory I will remove the watch from that directory, and later on when the same directory will be created again, AddWatch(path) method will work on that correctly. Currently AddWatch(path) method is not working correctly since I didn't remove the watch from the directory as there is no method exposed to remove the watch.

Might also be fixed by #201 - could you test if it actually is please? @vivek-bansal-VB