From 6c4abbab7999f55792a323e4bb1eb55ef5a7b990 Mon Sep 17 00:00:00 2001 From: Michael Albinus Date: Sun, 26 Feb 2023 15:40:30 +0100 Subject: [PATCH] Tramp: Do not unlock when connection is broken * 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 | 8 +++++++- test/lisp/net/tramp-tests.el | 24 +++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el index baa9f966dd8..2110d815c95 100644 --- a/lisp/net/tramp.el +++ b/lisp/net/tramp.el @@ -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. diff --git a/test/lisp/net/tramp-tests.el b/test/lisp/net/tramp-tests.el index f19847b0103..69004bdbdf3 100644 --- a/test/lisp/net/tramp-tests.el +++ b/test/lisp/net/tramp-tests.el @@ -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)))) -- 2.39.5