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.
This commit is contained in:
Michael Albinus 2023-02-26 15:40:30 +01:00
parent 8797e514ab
commit 6c4abbab79
2 changed files with 30 additions and 2 deletions

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

View 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))))