fs.watch fails to detect changes to file replaced by rename event #2062
Comments
I suspect that Coda first writes to a temp file, then does an atomic |
Confirmed, this test case gets the same behavior:
No response to further changes to |
So should I be spawning a new |
Over at CoffeeScript, we're doing this for our compilation utility: https://github.com/TrevorBurnham/coffee-script/blob/d30aa6d621cdd6c5af4eb8514ba30b3ddc97d567/src/command.coffee#L174-186 Is it the Node team's intention to make |
Also, it appears that |
Yes.
Maybe it should. The kqueue implementation works different from the inotify implementation right now because inotify monitors the directory entry while kqueue monitors the inode. |
It turns out there's a problem with re-watching a file whenever a rename event is detected: A rename event can mean either "a different file was renamed on top of this one" (in which case you want to re-watch it) or "this file was renamed elsewhere" (in which case attempting to re-watch it will cause an error). There's no way to distinguish between the two, at least on the Mac. Worse,
As a result, you always have to |
Confirmed, test case:
|
I'd rather you had reported that as a separate issue but it's fixed in 8dd4fcb. |
Thanks for the patch! I'll try to create finer-grained issues in the future. On Nov 10, 2011, at 1:28 PM, Ben Noordhuisreply@reply.github.com wrote:
|
Looking at this issue now, I understand that this is the expected behavior: The file has been unlinked, and a new file put in its place; from But, it's very unfortunate that a So, @bnoordhuis, would it be possible to add a |
The problem is that kqueue is file descriptor (inode) based. As long as the file descriptor is open, the old file isn't really unlinked, it's just not visible in the file system any more. You will even get events for the old file if another process also has an open fd and writes to it. I'm considering making it more in line with how linux and windows behave by reopening the file descriptor after every event but that is:
|
It's already the case that some events are slipping through the cracks (if I do 10 I would see it as a problem, though, if the behavior became less internally consistent. E.g., if a file is overwritten via an |
That's expected (and sensible) behaviour: if there are several events that amount to the same thing then the kernel collapses them into a single event. It's not kqueue specific, inotify and ReadDirectoryChangesW work the same. Example: say that there have been three mtime updates since the last poll. The kernel will simply discard the first two because only the last one is relevant.
Yes, no, maybe. The rm + mv case is particularly troublesome. There is a time window where the file simply doesn't exist so rearming the watcher may fail with a 'file not found' error. |
It seems possible to detect path changes with FSEvents (related #2023) Is there any way to get a filename from a file descriptor? |
@ricardobeat: A file descriptor may have more than one filename associated with it. edit: wording |
And Node uses kqueue under OS X, not FSEvents. On Nov 19, 2011, at 6:03 PM, Michael Ficarrareply@reply.github.com wrote:
|
That's why I mentioned issue #2023. @michaelficarra: OSX seems to support it with node-inotify exposes file moves by correlating |
@viatropos did you intend to reopen this issue? This issue seems to be resolved. |
See discussion at #1803, #1846, and at nodejs/node-v0.x-archive#2062
Closing. Issue appears to have been resolved. No information as to why it was reopened. Can reopen if new information is received. |
If I
fs.watch
a file and then save a change to it using the Coda text editor, it first detect a'rename'
event, then no further events. Coda appears to be very unusual in this respect; I haven't seen this behavior with any other Mac text editor. Maybe this is a fundamental problem with file change event notifications under Darwin; I'm not sure.The text was updated successfully, but these errors were encountered: