Subject: Re: bug#73855: [PATCH] * lisp/autorevert.el: Avoid reverting buffer in short time

From: Lin Sun <[email-masked]>
Date: Sat, 19 Oct 2024 17:52:35 +0000

Hi Michael,

On Sat, Oct 19, 2024 at 9:06 AM Michael Albinus <[email-masked]> wrote:
>
> Thanks for explanation, now I understand the scenario.
>
> Your patch makes sense. However, autorevert-tests fail now:
...
> 1 unexpected results:
>    FAILED  auto-revert-test05-global-notify
>
> make[1]: *** [Makefile:185: lisp/autorevert-tests.log] Error 1
...
> Could you pls check what's up?
>
> Best regards, Michael.

Thank you for found the issue, really appreciate it! The issue was
fixed by setting the variable 'auto-revert--lockout-interval' to the
half value of 'auto-revert-interval' (similar to its definition).

Please help review again, thanks.
From bc22ff0fc60b2ccb071a18fb55e08bb49f78c25a Mon Sep 17 00:00:00 2001
From: Lin Sun <[email-masked]>
Date: Thu, 17 Oct 2024 06:50:31 +0000
Subject: [PATCH] Enhance the auto-revert to avoid revert a buffer in short
 time

* lisp/autorevert.el: New variable auto-revert--last-time for
  (auto-revert-handler) and (auto-revert-notify-handler).
* test/lisp/autorevert-tests.el: Set the auto-revert--lockout-interval
  correctly in (with-auto-revert-test).
---
 lisp/autorevert.el            | 16 ++++++++++++----
 test/lisp/autorevert-tests.el |  7 +++++--
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/lisp/autorevert.el b/lisp/autorevert.el
index 0fdab6ffc9f..ef758584c0d 100644
--- a/lisp/autorevert.el
+++ b/lisp/autorevert.el
@@ -370,6 +370,9 @@ auto-revert-notify-modified-p
   "Non-nil when file has been modified on the file system.
 This has been reported by a file notification event.")
 
+(defvar-local auto-revert--last-time nil
+  "The last time of buffer was reverted.")
+
 (defvar auto-revert-debug nil
   "Use for debug messages.")
 
@@ -749,13 +752,17 @@ auto-revert-notify-handler
               ;; Mark buffer modified.
               (setq auto-revert-notify-modified-p t)
 
-              ;; Revert the buffer now if we're not locked out.
+              ;; Lock out the buffer
               (unless auto-revert--lockout-timer
-                (auto-revert-handler)
                 (setq auto-revert--lockout-timer
                       (run-with-timer
                        auto-revert--lockout-interval nil
-                       #'auto-revert--end-lockout buffer))))))))))
+                       #'auto-revert--end-lockout buffer))
+                ;; Revert it when first entry or it was reverted intervals ago
+                (when (or (null auto-revert--last-time)
+                          (> (float-time (time-since auto-revert--last-time))
+                             auto-revert--lockout-interval))
+                  (auto-revert-handler))))))))))
 
 (defun auto-revert--end-lockout (buffer)
   "End the lockout period after a notification.
@@ -801,7 +808,8 @@ auto-revert-handler
                               #'buffer-stale--default-function)
                           t))))
          eob eoblist)
-    (setq auto-revert-notify-modified-p nil)
+    (setq auto-revert-notify-modified-p nil
+          auto-revert--last-time (current-time))
     (when revert
       (when (and auto-revert-verbose
                  (not (eq revert 'fast)))
diff --git a/test/lisp/autorevert-tests.el b/test/lisp/autorevert-tests.el
index 4763994c5d4..7e176df6803 100644
--- a/test/lisp/autorevert-tests.el
+++ b/test/lisp/autorevert-tests.el
@@ -132,12 +132,15 @@ auto-revert--deftest-remote
          (error (message "%s" err) (signal (car err) (cdr err)))))))
 
 (defmacro with-auto-revert-test (&rest body)
-  `(let ((auto-revert-interval-orig auto-revert-interval))
+  `(let ((auto-revert-interval-orig auto-revert-interval)
+         (auto-revert--lockout-interval-orig auto-revert--lockout-interval))
      (unwind-protect
          (progn
            (customize-set-variable 'auto-revert-interval 0.1)
+           (setq auto-revert--lockout-interval 0.05)
            ,@body)
-       (customize-set-variable 'auto-revert-interval auto-revert-interval-orig))))
+       (customize-set-variable 'auto-revert-interval auto-revert-interval-orig)
+       (setq auto-revert--lockout-interval auto-revert--lockout-interval-orig))))
 
 (defun auto-revert-tests--write-file (text file time-delta &optional append)
   (write-region text nil file append 'no-message)
-- 
2.34.1