unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#73855: [PATCH] * lisp/autorevert.el: Avoid reverting buffer in short time
@ 2024-10-17 23:24 Lin Sun
  2024-10-18  7:50 ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-11-13  0:31 ` bug#73855: [BUG] Error: void-function (rx) during generating autoloads for python.el when building from source Lin Sun
  0 siblings, 2 replies; 14+ messages in thread
From: Lin Sun @ 2024-10-17 23:24 UTC (permalink / raw)
  To: 73855

[-- Attachment #1: Type: text/plain, Size: 968 bytes --]

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.

[-- Attachment #2: 0001-lisp-autorevert.el-Avoid-reverting-buffer-in-short-t.patch --]
[-- Type: text/x-patch, Size: 2225 bytes --]

From b1dc630718c39b2fb630d349f186ac060290c515 Mon Sep 17 00:00:00 2001
From: Lin Sun <sunlin7@hotmail.com>
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


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2024-11-17 16:04 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-17 23:24 bug#73855: [PATCH] * lisp/autorevert.el: Avoid reverting buffer in short time Lin Sun
2024-10-18  7:50 ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-19  5:58   ` Lin Sun
2024-10-19  9:06     ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-19 17:52       ` Lin Sun
2024-10-20  9:15         ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-10-20 18:29           ` Lin Sun
2024-11-13  0:31 ` bug#73855: [BUG] Error: void-function (rx) during generating autoloads for python.el when building from source Lin Sun
2024-11-13 14:51   ` Eli Zaretskii
2024-11-13 17:13     ` Lin Sun
2024-11-14  7:39       ` Eli Zaretskii
2024-11-14  9:35         ` Andreas Röhler
2024-11-17 14:08         ` Stefan Kangas
2024-11-17 16:04           ` Lin Sun

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).