Subject: Re: bug#66381: 29.1; Auto-revert not polling files when notifications are enabled

From: Daniel Jacobowitz <[email-masked]>
Date: Sat, 7 Oct 2023 12:14:52 -0400

emacs -Q --eval '(setq auto-revert-debug t file-notify-debug t)'
--eval '(global-auto-revert-mode t)' file.txt

Modify the file outside of Emacs:

file-notify-handle-event (file-notify ((1 . 0) (modify) "file.txt" 0)
file-notify--callback-inotify)
file-notify-callback (1 . 0) changed "/path/to/file.txt" nil
#s(file-notify--watch "/path/to" "file.txt"
auto-revert-notify-handler) "/path/to/file.txt" "/path/to/google3"
auto-revert-notify-handler ((1 . 0) changed "/path/to/file.txt")
Reverting buffer ‘file.txt’

Restart the network filesystem (sudo systemctl restart netfs). Modify
the file outside of Emacs again.

Nothing new in *Messages* for either the restart or the modification.

Hit a key in the file buffer:

file.txt changed on disk; really edit the buffer? (y, n, r or C-h)

Same story with inotify; there's no event when the filesystem
restarts. I'm not explicitly unmounting it, systemd kills and restarts
the job providing the fuse filesystem.

The path does not match auto-revert-notify-exclude-dir-regexp.

If supporting this is of general interest, I'd do it by (A)
re-enabling polling even when using notification; (B) removing and
re-adding the watch when polling reports changes that we weren't
notified about. If it's not of general interest, the best alternative
would be removing the bit of documentation that says "By default, Auto
Revert mode will poll files for changes periodically even when file
notifications are used."

For anyone with a similar problem, since the filesystem restarts are
typically overnight as part of system updates, I went with a simple
workaround:

(run-with-timer 1800 1800 (lambda () (progn
                                       (global-auto-revert-mode 'toggle)
                                       (global-auto-revert-mode 'toggle))))

On Sat, Oct 7, 2023 at 11:15 AM Michael Albinus <[email-masked]> wrote:
>
> Eli Zaretskii <[email-masked]> writes:
>
> Hi Eli,
>
> >> I use a network filesystem which sometimes has to restart, e.g.  for
> >> updates or when credentials expire. Normally, file notifications work
> >> fine on this filesystem, but after a restart the old notifications will
> >> never fire. Documentation says "By default, Auto Revert mode will poll
> >> files for changes periodically even when file notifications are used." -
> >> experimentally the file is never polled.
> >>
> >> Ideally the notification would be recreated, but falling back to
> >> polling would be
> >> an improvement.
> >
> > Michael, can we reliably know when the watch handles are stale?  If
> > not, then users of such "restarting" network filesystems will need to
> > disable auto-revert-use-notify.
>
> We have `file-notify-valid-p'. It calls the backend specific funtion,
> for example `inotify-valid-p' in the case of GNU/Linux using
> inotify. The OP didn't tell us which backend he's using, so let's assume
> inotify ATM.
>
> This backend specific check does not ask the operating system, it checks
> only some internal data structures. This isn't a robust check for a
> stale watch handle.
>
> However, inotify sends also an internal event IN_IGNORED, which is
> raised according to inotify(7)
>
> --8<---------------cut here---------------start------------->8---
>            IN_IGNORED
>                   Watch  was removed explicitly (inotify_rm_watch(2)) or auto‐
>                   matically (file was deleted, or filesystem  was  unmounted).
>                   See also BUGS.
> --8<---------------cut here---------------end--------------->8---
>
> filenotify.el translates this to the Emacs file-notify event `stopped',
> and if this event arrives, the respective data structures are
> updated. In case of a network filesystem unmount, everything shall work.
>
> A recreation of file notification after a remount is not supported. This
> would be a new feature.
>
> However, the situation is more complex I fear. If I understand the OP
> message correctly, file notification is used for auto-revert-mode. There
> is the user option `auto-revert-notify-exclude-dir-regexp'. If a
> directory located on a file name matching this regexp, file notification
> is discarded. File names like  "/mnt", "/net/", or "/tmp_mnt/" match. So
> in order to use file notification with auto-revert-mode on such a
> location, this user option must be adapted.
>
> I haven't tested the combination of watching a mounted file system, and
> unmounting them, in autorevert.el. According to
> `auto-revert-notify-handler', the file-notify event `stopped' is
> handled, and auto-revert-mode continues with polling. I would test this
> once I see a clear recipe (starting with
>
> --8<---------------cut here---------------start------------->8---
> emacs -Q --eval '(setq auto-revert-debug t file-notify-debug t)'
> --8<---------------cut here---------------end--------------->8---
>
> and telling all steps how to enable auto-revert-mode and how to unmount
> the filesystem and what happens afterwards. The two debug options show
> us the story in the *Messages* buffer.
>
> Best regards, Michael.

-- 
Thanks,
Daniel