Subject: bug#21559: [PATCH] autorevert: Wait a while before calling vc-find-file-hook

From: Ben Gamari <[email-masked]>
Date: Mon, 26 Oct 2015 19:43:38 +0100

This provides a resolution for Bug #21559.

emacs running with `auto-revert-mode` enabled breaks `git rebase` in
repositories where files are open. The problem appears to be that
`auto-revert-mode` attempts to refresh version control information with
`vc-find-file-hook` on revert events. `vc-find-file-hook` calls out to
`git`, taking the repository's index lock.

This interferes badly with `git rebase`, which performs many git
commands in quick succession. When `auto-revert-mode` is enabled there
is a very high chance that the following race will occur,

    git                                  emacs
    ----------------------               -----------------------------
    1. git rebase checks out
       a commit, releases
       `index.lock`
                                         2. `auto-revert-mode` notices
                                            change, firing off a `git`
                                            process and taking
`index.lock`.

    3. git rebase applies patch
       and attempts to commit.
       Notices that `index.lock`
       is taken and fails.

                                         4. emacs' `git` process
                                            finishes, releasing lock

In the end the user is left with a badly broken rebase process and an
error message complaining that `index.lock` exists, which he then goes
to confirm and finds no such file as emacs has already released the
lock.

We work around this by scheduling a worker to call `vc-fine-file-hook`
at some point in the future when the repository is more likely to be
idle (for instance, when there have been no change events for a second
or so).

Signed-Off-By: Ben Gamari <[email-masked]>
---
 lisp/autorevert.el | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/lisp/autorevert.el b/lisp/autorevert.el
index 37ee8ee..0c25ac3 100644
--- a/lisp/autorevert.el
+++ b/lisp/autorevert.el
@@ -136,6 +136,11 @@ auto-revert-tail-mode
 (defvar auto-revert-timer nil
   "Timer used by Auto-Revert Mode.")
 
+(defvar auto-revert-vc-check-timer nil
+  "Timer used by Auto-Revert Mode to schedule
+checks of version control information. See
+`auto-revert-schedule-vc-check' for details.")
+
 (defcustom auto-revert-interval 5
   "Time, in seconds, between Auto-Revert Mode file checks.
 The value may be an integer or floating point number.
@@ -260,6 +265,13 @@ auto-revert-check-vc-info
   :type 'boolean
   :version "22.1")
 
+(defcustom auto-revert-vc-check-idle-time 2
+  "How much time to wait after noticing a changed file before calling
+`vc-find-file-hook' or nil to check immediately."
+  :group 'auto-revert
+  :type 'number
+  :version "25.1")
+
 (defvar-local global-auto-revert-ignore-buffer nil
   "When non-nil, Global Auto-Revert Mode will not revert this buffer.
 This variable becomes buffer local when set in any fashion.")
@@ -671,7 +683,28 @@ auto-revert-handler
     ;; `preserve-modes' avoids changing the (minor) modes.  But we do
     ;; want to reset the mode for VC, so we do it manually.
     (when (or revert auto-revert-check-vc-info)
-      (vc-find-file-hook))))
+      (auto-revert-schedule-vc-check))))
+
+(defun auto-revert-schedule-vc-check ()
+  "Schedule a call to `vc-find-file-hook'.
+
+We need to be careful when calling `vc-find-file-hook' after file changes
+as some version control utilities (e.g. git rebase) have a tendency
+to do many successive calls and will fail ungracefully if they find
+we have taken the repository lock.
+
+For this reason we wait for the repository to be idle for at least
+`auto-revert-vc-check-idle-time' seconds before calling
+`vc-find-file-hook'."
+  (if auto-revert-vc-check-idle-time
+      (progn
+        (when (timerp auto-revert-vc-check-timer)
+          (cancel-timer auto-revert-vc-check-timer))
+        (setq auto-revert-vc-check-idle-timer
+              (run-at-time auto-revert-vc-check-idle-time nil
+                           'vc-find-file-hook))
+        )
+    (vc-find-file-hook)))
 
 (defun auto-revert-tail-handler (size)
   (let ((modified (buffer-modified-p))
-- 
2.6.1