Multiple calls per watch event on linux with rb-inotify #98

Closed
jeffutter opened this Issue Jun 29, 2011 · 8 comments

6 participants

@jeffutter

It seems that in some cases rb-inotify is generating multiple events when it should only be generating one.

My specific scenario:

I'm using the sublimetext editor. When saving a file with sublimetext it uses one fwrite operation and then multiple writes in 4k blocks.

as can be seen here:
https://github.com/guard/guard/blob/master/lib/guard/listeners/linux.rb#L50

Guard is watching for :close_write and :modify events. A modify event gets triggered for every 4k block written by the editor. If guard just listened for :close_write it would only trigger once when the editor has closed the file handle. I can't really think of any situation where :modify would be needed, but there may be some.

@netzpirat

From the inotifywatch man page:

modify
    A watched file or a file within a watched directory was written to.

close_write
    A watched file or a file within a watched directory was closed, after being opened
    in writeable mode. This does not necessarily imply the file was written to.

So when the editor writes a file in chunks, it fires several :modify and a single :close_write. So from my understanding, when removing :modify from the listener, we'll catch only the end of the write operation. However there may be other editors that keeps a file handle open and thus fires only :modify events.

I'm not sure how to approach this, since it may break another editor and I'm not using Linux on my development machine. I'd like to see more analysis of inotify events for other popular editors like vim, nano, joe, gedit, kate, SciTE, etc.

@jeffutter

Here is the inotifywatch output from a few common editors. I opened the file, launched inotifywatch, changed it, saved it, killed inotify watch:

Sublime Text

total  modify  close_write  open  filename
5      3       1            1     coffee/script.coffee

Vim

total  attrib  move_self  delete_self  filename
4      1       1          1            coffee/script.coffee

Gedit

total  access  attrib  close_write  close_nowrite  open  delete_self  filename
8      1       1       1            1              2     1            coffee/script.coffee

nano

total  modify  close_write  open  filename
5      3       1            1     coffee/script.coffee

joe

total  access  modify  attrib  close_write  close_nowrite  open  filename
10     1       4       1       1            1              2     coffee/script.coffee

jedit

total  move_self  filename
1      1          coffee/script.coffee

scite

total  modify  close_write  open  filename
5      3       1            1     coffee/script.coffee

bluefish

total  attrib  close_write  open  filename
4      2       1            1     coffee/script.coffee

echo 'foo' > coffee/test.txt

total  modify  close_write  open  filename
4      2       1            1     coffee/test.txt

It looks to me like everything has a close_write except for a few that have neither close_write nor modify. I think these ones write to a tmp file and then move it into place over the existing file.

@netzpirat

Awesome job! I will take care of it tomorrow. Thanks a lot.

@thibaudgg
Guard member

Seems like supporting only close_write & move_self (for vim & jedit) would cover 100% of editors listed above. Thanks these info, that's really helpful!

@schadenfred

I'd like to add this issue, which is now closed, to this thread:

guard/guard-rspec#26 (comment)

@thibaudgg
Guard member

Fixed in this commit

@thibaudgg thibaudgg closed this Jul 2, 2011
@jonmast

With Kate 3.12 run_on_modifications is no longer being fired. Output from inotifywatch is:

total  attrib  delete_self  filename
3      1       1            catalogdetail.tpl
@rymai
Guard member

@onlineamateur Please report any listener issue directly in the Listen issue tracker since it's handled there now. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment