]> git.eshelyaron.com Git - emacs.git/commitdiff
Tramp: Do not unlock when connection is broken
authorMichael Albinus <michael.albinus@gmx.de>
Sun, 26 Feb 2023 14:40:30 +0000 (15:40 +0100)
committerMichael Albinus <michael.albinus@gmx.de>
Sun, 26 Feb 2023 14:40:30 +0000 (15:40 +0100)
* lisp/net/tramp.el (tramp-handle-unlock-file): Do not unlock when
connection is broken.  (Bug#61663)

* test/lisp/net/tramp-tests.el (tramp-test39-make-lock-file-name):
Extend test.

lisp/net/tramp.el
test/lisp/net/tramp-tests.el

index baa9f966dd8245ea39bdedfdd40df709ee3a88a1..2110d815c951aa10943d8655dfbf3d0ce6506655 100644 (file)
@@ -4778,7 +4778,13 @@ Do not set it manually, it is used buffer-local in `tramp-get-lock-pid'.")
 
 (defun tramp-handle-unlock-file (file)
   "Like `unlock-file' for Tramp files."
-  (when-let ((lockname (tramp-compat-make-lock-file-name file)))
+  ;; When there is no connection, we don't do it.  Otherwise,
+  ;; functions like `kill-buffer' would try to reestablish the
+  ;; connection.  See Bug#61663.
+  (when-let ((v (tramp-dissect-file-name file))
+            (p (tramp-get-process v))
+            ((process-live-p p))
+            (lockname (tramp-compat-make-lock-file-name file)))
     (condition-case err
         (delete-file lockname)
       ;; `userlock--handle-unlock-error' exists since Emacs 28.1.
index f19847b0103e419b39f9c21a17cb5012e4f21ea4..69004bdbdf35b549c859cc3c261c4c5c4e2dfb33 100644 (file)
@@ -6535,11 +6535,33 @@ INPUT, if non-nil, is a string sent to the process."
               (save-buffer)
              (should-not (buffer-modified-p)))
             (should-not (with-no-warnings (file-locked-p tmp-name1)))
+
+            ;; `kill-buffer' removes the lock.
            (with-no-warnings (lock-file tmp-name1))
            (should (eq (with-no-warnings (file-locked-p tmp-name1)) t))
+            (with-temp-buffer
+              (set-visited-file-name tmp-name1)
+              (insert "foo")
+             (should (buffer-modified-p))
+             (cl-letf (((symbol-function #'read-from-minibuffer)
+                         (lambda (&rest _args) "yes")))
+                (kill-buffer)))
+           (should-not (with-no-warnings (file-locked-p tmp-name1)))
 
+            ;; `kill-buffer' should not remove the lock when the
+            ;; connection is broken.  See Bug#61663.
+           (with-no-warnings (lock-file tmp-name1))
+           (should (eq (with-no-warnings (file-locked-p tmp-name1)) t))
+            (with-temp-buffer
+              (set-visited-file-name tmp-name1)
+              (insert "foo")
+             (should (buffer-modified-p))
+             (tramp-cleanup-connection tramp-test-vec 'keep-debug 'keep-password)
+             (cl-letf (((symbol-function #'read-from-minibuffer)
+                         (lambda (&rest _args) "yes")))
+                (kill-buffer)))
            ;; A new connection changes process id, and also the
-           ;; lockname contents.
+           ;; lockname contents.  But the lock file still exists.
            (tramp-cleanup-connection tramp-test-vec 'keep-debug 'keep-password)
            (should (stringp (with-no-warnings (file-locked-p tmp-name1))))