notify
notify copied to clipboard
Events not delivered until a file is closed on macOS.
System details
- OS/Platform name and version: macOS 10.15.2
- Rust version (if building from source):
rustc --version
:rustc 1.40.0 (73528e339 2019-12-16)
- Notify version (or commit hash if building from git): 4.0.14
- Filesystem type and options: APFS
- If you're running as a privileged user (root, System): no
- If you're running in a container, details on the runtime and overlay: no
- If you're running in a VM, details on the hypervisor: no
What you did (as detailed as you can)
Writes are generated to a file every second using the following C program. However, the events are only noticed by notify's FSEventsWatcher every 10 seconds, when the file is closed and reopened.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main()
{
while (1) {
FILE *f = fopen("/Users/brennan/test.txt", "w");
for (int i = 0; i < 10; ++i) {
fputs("Hello!\n", f);
fflush(f);
sleep(1);
}
printf("Closing file!\n");
fclose(f);
}
}
What you expected
Notify should trigger once per second when watching /Users/brennan/test.txt
and running the above C program.
What happened
It only triggers every 10 seconds.
Apparently, this is expected behavior from the FSEvents API; it's just not clear from Apple's documentation. But looking at the kernel source code makes it clear that this event is only raised on file close: https://github.com/apple/darwin-xnu/blob/a449c6a3b8014d9406c2ddbdc81795da24aa7443/bsd/vfs/vfs_vnops.c#L771-L775
This issue will be resolved by creating a backend that uses kqueues, since those do not wait until file close to deliver modify events.
To explain why this matters (I can't link to code as the repo I'm using this in is private for now):
Imagine you wanted to use notify
to write a program that tails log files. You process all lines, then go to sleep until notify
notifies you that the file has changed, at which point you read again, and so on ad infinitum.
If the log file is held open long-term by some daemon, this will not work, since the file modified events will never be raised.
Actually, I am 99% sure that this was the real reason for https://github.com/notify-rs/notify/issues/50 , and not anything related to security (since that file is, in fact, world-readable).
I'm facing the same problem on windows...I have no idea what to do.