Problem with notify crate ( v6.1 )

the problem is when i change/edit a file it showing the event twice instead of one, in terminal it showing
Data(Any)
Data(Any)
for a single file change/edit

my code

fn watch<P: AsRef<Path>>(path: P) -> notify::Result<()> {
    let (tx, rx) = sync::mpsc::channel();
    let mut watcher = RecommendedWatcher::new(tx, Config::default())?;
    watcher.watch(path.as_ref(), RecursiveMode::Recursive)?;
    for res in rx {
        let event = res?;
        match event.kind {
            EventKind::Modify(s) => println!("{s:?}"),
            _ => (),
        }
    }
    Ok(())
}

Depending on how you edited the file, there may have been multiple syscalls. e.g. truncate then write.

Yes, this can be a problem. How is it related to Rust, though?

Crate documentation even explicitly says: Editor Behaviour : If you rely on precise events (Write/Delete/Create..), you will notice that the actual events can differ a lot between file editors. Some truncate the file on save, some create a new one and replace the old one. See also this and this issues for example.

Maybe someone would crate a crate which includes extensive online-database with process-tracker and lots of other anti-cheat things which would solve your “problem”, but I, for one, is happy than simple notify crate is not a privacy disaster like such tool would, by necessity, be.

notify-debouncer-mini might be useful, here.

One of the issues that notify tries to avoid is uses that cause TOCTOU bugs. This is gleaned roughly from conversation in "A debounced event. Does not emit any specific event type on purpose" – please consider adding information about the reasoning, and how to work around this limitation · Issue #468 · notify-rs/notify (github.com)

Whether it is successful at this goal is another matter, but the point is that using file system events is surprisingly difficult. In part because a lot of the behavior is not only platform-specific, but application-specific. Debouncing is a strategy to avoid spurious duplicate events that comes with its own downsides. Including but not limited to throwing away information which might seem useful, but it actually isn't in practice.

1 Like

oh thanks

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.