fsnotify: "Write" op never reported on Ubuntu.

I’ve tested this program on two flavors of Linux, openSUSE 13.2 and Ubuntu 14.04:

package main

import (
    "fmt"
    "gopkg.in/fsnotify.v1"
)

func main() {
    watcher, err := fsnotify.NewWatcher()
    if err != nil {
        panic(err)
    }
    watcher.Add("hello.txt")
    defer watcher.Close()

    for {
        select {
        case event := <-watcher.Events:
            if event.Op&fsnotify.Create == fsnotify.Create {
                fmt.Println("CREATE " + event.Name)
            } else if event.Op&fsnotify.Write == fsnotify.Write {
                fmt.Println(" WRITE " + event.Name)
            } else if event.Op&fsnotify.Remove == fsnotify.Remove {
                fmt.Println("REMOVE " + event.Name)
            } else if event.Op&fsnotify.Rename == fsnotify.Rename {
                fmt.Println("RENAME " + event.Name)
            } else if event.Op&fsnotify.Chmod == fsnotify.Chmod {
                fmt.Println(" CHMOD " + event.Name)
            } else {
                fmt.Printf("unexpected: %d\n", event.Op)
            }
        case err := <-watcher.Errors:
            panic(err)
        }
    }
}

When I run this on openSUSE and make a change to hello.txt, I get the following output:

 WRITE hello.txt
 CHMOD hello.txt

but when I do the same thing on Ubuntu, this is the output:

RENAME hello.txt
 CHMOD hello.txt
REMOVE hello.txt

and then due to the remove, no further notifications are sent unless it’s manually added back.

Given that it works properly on openSUSE, this is probably not a bug for fsnotify itself, but I’m very thrown off by the results I got from Ubuntu. Why does it behave this way, and is there any workaround for it?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 15 (2 by maintainers)

Commits related to this issue

Most upvoted comments

I’m having the same issue with Ubuntu 16.04.1 LTS

monk at BorgCube in ~
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.1 LTS
Release:        16.04
Codename:       xenial

monk at BorgCube in ~
$ uname -a
Linux BorgCube 4.4.0-47-generic #68-Ubuntu SMP Wed Oct 26 19:39:52 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

I don’t have a problem if I write to the file using shell redirection.

Moreover, using vim, if I set the following:

set nobackup
set nowritebackup

I have no issues.

It would seem this issue is due to the backup scheme of the editor (at least in the case of vim), where it

  1. writes the buffer to a new file
  2. deletes the original file
  3. renames the new file

Same on CentOS 7: I1013 23:15:14.649953 9265 server.go:340] Catched file: “/etc/sysconfig/docker”: CHMOD I1013 23:15:14.650019 9265 server.go:340] Catched file: “/etc/sysconfig/docker”: REMOVE I1013 23:15:55.602021 9265 server.go:303] Watching file: /etc/sysconfig/docker