hannobraun / inotify-rs

Idiomatic inotify wrapper for the Rust programming language
ISC License
254 stars 64 forks source link

duplicate event reports #195

Closed gneworld closed 2 years ago

gneworld commented 2 years ago

NA

hannobraun commented 2 years ago

Hey @gneworld!

inotify-rs is only a thin layer on top of inotify, so the most likely explanation is that inotify is emitting multiple events, and there's little we can do about it.

I found this Stack Overflow answer: https://stackoverflow.com/questions/32377517/inotify-event-in-modify-occurring-twice-for-tftp-put/32424150#32424150

Maybe WatchMask::CLOSE_WRITE is more suitable for your use case?

gneworld commented 2 years ago

@hannobraun Thanks for your reply, while after change WatchMask::MODIFY to WatchMask::CLOSE_WRITE,there is nothing triggered when echo something into /tmp/file ...

hannobraun commented 2 years ago

Could you post your updated code?

gneworld commented 2 years ago

@hannobraun Sorry, I made a mistake with a typo, after correct it, it works fine. But as said in Stack Overflow answer, ''' many logs and similar files must be monitored using IN_MODIFY - in such cases where these files are permanently open and thus no IN_CLOSE_WRITE can be emitted. ''' how to fix the issue that one write trigger twice events report under this log file case

hannobraun commented 2 years ago

I actually don't know under which exact circumstances multiple IN_MODIFY events can be generated, but I would assume that it is outside of our control and we can't really do anything about that. How you react to it depends on the needs of your application.

If reacting to the event is cheap (for example, if you just want to store the time the log was last modified in a variable somewhere), then I don't think you need to do anything about it. Just handle each event. If handling the event is expensive, then you could deduplicate events by waiting for a short time before handling one (let's say 50 ms, or a whole second, whatever works for you), and then handle all IN_MODIFY events that arrived during that time all at once.

gneworld commented 2 years ago

I just watch a file's modification event in /tmp path as above sample code, when I execute "echo xxxx > /tmp/file" with only once, I randomly got two same modified events reported, In fact, I want to launch my app when specified file was modified, so two app processes launched because this issue

hannobraun commented 2 years ago

Then remember whether you already launched the app, and don't do it again?

gneworld commented 2 years ago

So I should use the singleton pattern to design my app, thanks very much

hannobraun commented 2 years ago

I didn't say that. I didn't even suggest it. You can design your app however you want.

I'm going to close this issue now, since this doesn't seem to be an issue in inotify-rs.