gmethvin / directory-watcher

A cross-platform Java recursive directory watcher, with a JNA macOS watcher and Scala better-files integration
Apache License 2.0
264 stars 34 forks source link

Duplicate events when editing file on Samba share #92

Open J-N-K opened 1 year ago

J-N-K commented 1 year ago

We have found that sometimes editing files that are exposed on a samba share results in multiple create/delete events (up to six events in about half a second) instead of a single modify event.

We have now implemented a workaround in our code (https://github.com/openhab/openhab-core/pull/3404) but it would be great if this could be handled here as I believe it'll be encountered by a larger audience.

gmethvin commented 1 year ago

Are there specific types of edits that cause that behavior? Did you check if it's the underlying Java watch service that's duplicating the events or if they're generated by directory-watcher itself? (you should be able to see with debug logging enabled)

Depending on how the events are generated, we can probably detect that sequence of events in directory-watcher and compress them into a single MODIFY.

J-N-K commented 1 year ago

You can see example here: https://github.com/openhab/openhab-core/issues/3400#issuecomment-1445500615 (except the last post from myself, that is a bug in our code).

gmethvin commented 1 year ago

Which platforms have you tested on? Seems like it's either due to a bug in the underlying WatchService implementation or the events the OS itself is reporting.

J-N-K commented 1 year ago

For @mhilbush it is Ubuntu 22.04 for the samba server and a MacOS client. I'm not sure who actually is the issue here, if it's a faulty behavior of the client or the server.

mhilbush commented 1 year ago

I also see similar behavior using a Windows client (e.g. Notepad++ on Win 10) and a Linux client (nano on Ubuntu) accessing the same share as with the MacOS client.

2023-02-28 07:18:03.876 [DEBUG] [io.methvin.watcher.DirectoryWatcher ] - ENTRY_MODIFY [/opt/openhab4/conf/rules/zoneminder.rules]
2023-02-28 07:18:03.877 [DEBUG] [io.methvin.watcher.DirectoryWatcher ] - -> MODIFY [/opt/openhab4/conf/rules/zoneminder.rules] (isDirectory: false)
2023-02-28 07:18:03.877 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'zoneminder.rules'
2023-02-28 07:18:03.923 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'zoneminder.rules' is either empty or cannot be parsed correctly!
2023-02-28 07:18:03.923 [DEBUG] [io.methvin.watcher.DirectoryWatcher ] - ENTRY_MODIFY [/opt/openhab4/conf/rules/zoneminder.rules]
2023-02-28 07:18:03.924 [DEBUG] [io.methvin.watcher.DirectoryWatcher ] - -> MODIFY [/opt/openhab4/conf/rules/zoneminder.rules] (isDirectory: false)
2023-02-28 07:18:03.924 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'zoneminder.rules'

Versus saving it locally using e.g. nano.

[2023-02-28 07:39:28.275 [DEBUG] [io.methvin.watcher.DirectoryWatcher ] - ENTRY_MODIFY [/opt/openhab4/conf/rules/zoneminder.rules]
2023-02-28 07:39:28.276 [DEBUG] [io.methvin.watcher.DirectoryWatcher ] - -> MODIFY [/opt/openhab4/conf/rules/zoneminder.rules] (isDirectory: false)
2023-02-28 07:39:28.277 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'zoneminder.rules'
2023-02-28 07:39:29.909 [DEBUG] [io.methvin.watcher.DirectoryWatcher ] - ENTRY_MODIFY [/opt/openhab4/conf/rules/zoneminder.rules]
priyankadhiman0803 commented 1 year ago

I am also getting 8 events on one event type . Like a file is deleted then 8 events are fired for it

gmethvin commented 1 year ago

I likely won't have time to look into this further anytime soon, but I'm happy to accept pull requests if anyone finds a viable way to handle this.