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

From: [email-masked] (GNU bug Tracking System)
Date: Sun, 20 Oct 2024 09:17:02 +0000

Your bug report

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

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
73855: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=73855
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#73855: [PATCH] * lisp/autorevert.el: Avoid reverting buffer in short time
From: Michael Albinus <[email-masked]>
To: Lin Sun <[email-masked]>
Cc: [email-masked]
Date: Sun, 20 Oct 2024 11:15:26 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

Version: 31.1

Lin Sun <[email-masked]> writes:

> Hi Michael,

Hi Lin,

> 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.

This looks good now. I've pushed it to master, closing the bug.

Best regards, Michael.

Subject: [PATCH] * lisp/autorevert.el: Avoid reverting buffer in short time
From: Lin Sun <[email-masked]>
To: [email-masked]
Date: Thu, 17 Oct 2024 23:24:00 +0000
List-Id: <debbugs-submit.debbugs.gnu.org>

Hi,

High CPU consumption after enabling the "auto-revert" for a buffer.
Here is a way to reproduce the issue:
1. on one terminal run command "strace -f -o /tmp/a.log vi -nw"
2. on second terminal, start another emacs and open the file
/tmp/a.log, enable the "auto-revert" mode on the /tmp/a.log buffer.

Typing on 1st terminal(vi), the strace will continually write the
/tmp/a.log, and the emacs try to revert the /tmp/a.log buffer again
and again, then CPU loading turns high.

The function `auto-revert-handler` may be called twice for 2.5 seconds
intervals on a rapidly changed buffer/file.

The root cause is `auto-revert--end-lockout` will call
`auto-revert-handler` in which the `auto-revert--lockout-timer` was
cleared, then the next call `auto-revert-notify-handler` will revert
the buffer immediately regardless the buffer actually was revert just
before.

This patch will record when the buffer was reverted and avoid
reverting it again in the same second.
From b1dc630718c39b2fb630d349f186ac060290c515 Mon Sep 17 00:00:00 2001
From: Lin Sun <[email-masked]>
Date: Thu, 17 Oct 2024 06:50:31 +0000
Subject: [PATCH] * lisp/autorevert.el: Avoid reverting buffer in short time

---
 lisp/autorevert.el | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 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)))
-- 
2.34.1