notify-rs / notify

🔭 Cross-platform filesystem notification library for Rust.
https://docs.rs/notify
2.67k stars 213 forks source link

How can I get the events from the files that are not closed on windows? #422

Open Tatametheus opened 2 years ago

Tatametheus commented 2 years ago

I'm using notify to watch csv files that are generated by another app on windows. During the development stage, all the files tested on wsl, everything were good. However, when I test on windows right now, no events emit by the files,which are read-only and the app holds the writing, unless the app shut down. I'm not familiar with the file system on windows or linux(ubuntu wsl) and not clear why it works on ubuntu wsl but not windows. Thanks for the awesome project.

jarruda commented 1 year ago

I can reproduce this on Windows and on MacOS. Here's a reproduction using a python script to write:

from time import sleep

with open("test.txt", "a") as f:
    while True:
        f.write("foo\n")
        f.flush()
        print("wrote")
        sleep(2)

Using the monitor-raw.rs sample to read test.txt, no events are generated until the python script is terminated.

tail -f test.txt shows the file being written to as expected.

On Windows, if you run Get-Content -Tail 10 -Wait, then events begin to get generated by notify.

0xpr03 commented 1 year ago

My current guess is that windows doesn't flush the file and waits with change reports until either the file handle is closed, the application is closed or a certain time is passed.

You can also watch this happen in explorer when download/moving giant files: The file size stays at (for example) 0KB while you know that the download progress is happening, and F5/refresh in explorer won't help. But if you right click on the file and go to Properties, then suddenly the actual size is reported (but not updated unless you redo this all the time). From what I know it's kinda the same with windows defender, where closing a file handle blocks until the virus scan finished - so events are happening only then.

Whether we can change/"fix" this behaviour on windows I can't say right now.

jarruda commented 1 year ago

This issue is not Windows specific. I was able to reproduce this behavior on MacOS and Windows per my comment. Further, the Get-Content -Tail -Wait behavior in Windows and tail -f behavior in MacOS implies that flushing is not the issue at hand.

AntoineGS commented 4 months ago

I just ran into this issue on Windows using notify to create a tail-style log parser. Has anyone find a solution or workaround?

Edit: I noticed that if I refresh the file in Notepad++ or use tail in Powershell at the same time then an event is triggered.