Subject: bug#49621: 28.0.50; tramp: Remote lock symlink remains after save-buffer

From: Naofumi Yasufuku <[email-masked]>
Date: Sun, 18 Jul 2021 21:04:29 +0900

Remote lock symlink remains even though the locked file is saved by
`save-buffer'.

NG CASE:

  0) emacs -Q
  1) C-x C-f /sshx:user@host:~/file
  2) type some text
     (self-insert-command -> lock-file -> tramp-handle-lock-file)
  3) C-x C-s
     (save-buffer -> write-region -> tramp-sh-handle-write-region)
  4) lock symlink remains
  5) C-x k
     (kill-buffer)
  6) lock symlink remains

OK CASE:

  0) emacs -Q
  1) C-x C-f /sshx:user@host:~/file
  2) type some text
     (self-insert-command -> lock-file -> tramp-handle-lock-file)
  3) C-x k
     (kill-buffer -> unlock-file -> tramp-handle-unlock-file)
  4) lock symlink is removed

`tramp-sh-handle-write-region' doesn't unlock the file whose lock is
already acquired.  The following change can solve this problem for sh
methods.  It seems that all tramp methods have the same problem.

--------
diff --git a/lisp/net/tramp-sh.el b/lisp/net/tramp-sh.el
index e6bd42a83a..8b4c78fe65 100644
--- a/lisp/net/tramp-sh.el
+++ b/lisp/net/tramp-sh.el
@@ -3249,7 +3249,7 @@ tramp-sh-handle-write-region
 		     (format "File %s exists; overwrite anyway? " filename)))))
       (tramp-error v 'file-already-exists filename))
 
-    (let (file-locked
+    (let ((file-locked (eq (file-locked-p lockname) t))
 	  (uid (or (tramp-compat-file-attribute-user-id
 		    (file-attributes filename 'integer))
 		   (tramp-get-remote-uid v 'integer)))
@@ -3260,7 +3260,7 @@ tramp-sh-handle-write-region
       ;; Lock file.
       (when (and (not (auto-save-file-name-p (file-name-nondirectory filename)))
 		 (file-remote-p lockname)
-		 (not (eq (file-locked-p lockname) t)))
+		 (not file-locked))
 	(setq file-locked t)
 	;; `lock-file' exists since Emacs 28.1.
 	(tramp-compat-funcall 'lock-file lockname))
@@ -3481,7 +3481,7 @@ tramp-sh-handle-write-region
           (tramp-set-file-uid-gid filename uid gid))
 
 	;; Unlock file.
-	(when (and file-locked (eq (file-locked-p lockname) t))
+	(when file-locked
 	  ;; `unlock-file' exists since Emacs 28.1.
 	  (tramp-compat-funcall 'unlock-file lockname))
 
--------

Regards,
--Naofumi

In GNU Emacs 28.0.50 (build 1, aarch64-unknown-linux-gnu, GTK+ Version 3.24.25, cairo version 1.16.0)
 of 2021-07-18 built on phoebe
Repository revision: 6b802a08cabfb23bdf1f65faa2ee163d3efa820d
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101001
System Description: Ubuntu 21.04

Configured using:
 'configure --prefix=/home/naofumi/.local/emacs-head --with-mailutils
 --with-native-compilation --with-xwidgets'

Configured features:
CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG JSON
LCMS2 LIBSELINUX LIBSYSTEMD LIBXML2 MODULES NATIVE_COMP NOTIFY INOTIFY
PDUMPER PNG RSVG SECCOMP SOUND THREADS TIFF TOOLKIT_SCROLL_BARS X11 XDBE
XIM XPM XWIDGETS GTK3 ZLIB

Important settings:
  value of $LANG: en_US.UTF-8
  value of $XMODIFIERS: @im=ibus
  locale-coding-system: utf-8-unix

Major mode: Dired by name

Minor modes in effect:
  company-statistics-mode: t
  global-company-mode: t
  company-mode: t
  yas-global-mode: t
  yas-minor-mode: t
  doom-modeline-mode: t
  which-key-mode: t
  disable-mouse-global-mode: t
  key-chord-mode: t
  shell-dirtrack-mode: t
  projectile-mode: t
  global-git-gutter-mode: t
  ivy-mode: t
  amx-mode: t
  pyvenv-tracking-mode: t
  editorconfig-mode: t
  gnus-dired-mode: t
  windmove-mode: t
  winner-mode: t
  global-so-long-mode: t
  global-whitespace-mode: t
  recentf-mode: t
  global-auto-revert-mode: t
  electric-pair-mode: t
  delete-selection-mode: t
  show-paren-mode: t
  savehist-mode: t
  global-eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tab-bar-history-mode: t
  tab-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  buffer-read-only: t
  column-number-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
/home/naofumi/.emacs.d/elpa-28/cmake-mode-20210104.1831/cmake-mode hides /usr/share/emacs/site-lisp/cmake-mode
/home/naofumi/.emacs.d/elpa-28/transient-20210701.1116/transient hides /home/naofumi/.local/emacs-head/share/emacs/28.0.50/lisp/transient

Features:
(mail-extr sort vc-hg vc-git diff-mode vc-bzr vc-dispatcher tramp-cache
tramp-sh dired-aux ffap shadow highlight-indent-guides face-remap
editorconfig-core editorconfig-core-handle editorconfig-fnmatch emacsbug
counsel xdg swiper server skk-cursor ccc skk-sticky skk-dcomp skk-comp
skk-look skk-server skk-study skk-hint skk-cus cus-edit pp cus-start
cus-load skk-emacs skk-macs skk-vars skk company-statistics
company-oddmuse company-keywords company-etags etags fileloop
company-gtags company-dabbrev-code company-dabbrev company-files
company-clang company-capf company-cmake company-semantic
company-template company-bbdb company pcase yasnippet-snippets yasnippet
doom-modeline doom-modeline-segments doom-modeline-env
doom-modeline-core shrink-path f all-the-icons all-the-icons-faces
data-material data-weathericons data-octicons data-fileicons
data-faicons data-alltheicons tramp tramp-loaddefs trampver
tramp-integration files-x tramp-compat ls-lisp doom-tomorrow-night-theme
doom-themes doom-themes-base base16-tomorrow-night-theme base16-theme
vlf-setup which-key disable-mouse key-chord shell-pop term shell ehelp
win-switch elscreen-gf elscreen projectile grep compile ibuf-ext ibuffer
ibuffer-loaddefs git-gutter ivy-xref ivy-migemo two-column ivy-hydra ivy
ivy-faces ivy-overlay colir color ace-link avy amx s migemo comp
comp-cstr warnings smart-jump-lisp-mode smart-jump-elisp-mode smart-jump
xref project cl-extra help-mode hydra lv pyvenv eshell esh-cmd esh-ext
esh-opt esh-proc esh-io esh-arg esh-module esh-groups esh-util
editorconfig rx mu4e-maildirs-extension dash gnus-dired mu4e desktop
frameset mu4e-org mu4e-main mu4e-headers mu4e-view thingatpt gnus-art
mm-uu mml2015 mm-view mml-smime smime dig gnus-sum shr kinsoku svg dom
gnus-group gnus-undo gnus-start gnus-dbus dbus xml gnus-cloud nnimap
nnmail mail-source utf7 netrc nnoo parse-time iso8601 gnus-spec gnus-int
gnus-range gnus-win gnus nnheader mu4e-compose mu4e-context mu4e-draft
mu4e-actions org-capture org-refile ido rfc2368 smtpmail sendmail
mu4e-mark mu4e-message flow-fill mu4e-proc mu4e-utils doc-view jka-compr
image-mode exif mu4e-lists ox-gfm ox-md ox-odt rng-loc rng-uri rng-parse
rng-match rng-dt rng-util rng-pttrn nxml-parse nxml-ns nxml-enc xmltok
nxml-util ox-latex ox-icalendar ox-html table ox-ascii ox-publish ox
org-element avl-tree generator org ob ob-tangle ob-ref ob-lob ob-table
ob-exp org-macro org-footnote org-src ob-comint org-pcomplete pcomplete
comint ansi-color org-list org-faces org-entities noutline outline
easy-mmode org-version ob-emacs-lisp ob-core ob-eval org-table ol
org-keys org-compat org-macs org-loaddefs format-spec find-func cal-menu
calendar cal-loaddefs mule-util hl-line mu4e-vars message rmc puny
dired-x dired dired-loaddefs rfc822 mml mml-sec epa derived epg
epg-config gnus-util rmail rmail-loaddefs text-property-search time-date
mm-decode mm-bodies mm-encode mail-parse rfc2231 rfc2047 rfc2045 mm-util
ietf-drums mail-prsvr mailabbrev mail-utils gmm-utils mailheader
mu4e-meta auth-source-pass windmove winner ring so-long disp-table
whitespace recentf-ext recentf tree-widget wid-edit diminish autorevert
filenotify cua-base elec-pair delsel paren savehist japan-util
exec-path-from-shell finder-inf advice edmacro kmacro slime-autoloads
info package browse-url url url-proxy url-privacy url-expand url-methods
url-history url-cookie url-domsuf url-util mailcap url-handlers
url-parse auth-source cl-seq eieio eieio-core cl-macs eieio-loaddefs
password-cache json subr-x map url-vars seq byte-opt gv bytecomp
byte-compile cconv cl-loaddefs cl-lib iso-transl tooltip eldoc electric
uniquify ediff-hook vc-hooks lisp-float-type mwheel term/x-win x-win
term/common-win x-dnd tool-bar dnd fontset image regexp-opt fringe
tabulated-list replace newcomment text-mode elisp-mode lisp-mode
prog-mode register page tab-bar menu-bar rfn-eshadow isearch easymenu
timer select scroll-bar mouse jit-lock font-lock syntax font-core
term/tty-colors frame minibuffer cl-generic cham georgian utf-8-lang
misc-lang vietnamese tibetan thai tai-viet lao korean japanese eucjp-ms
cp51932 hebrew greek romanian slovak czech european ethiopic indian
cyrillic chinese composite charscript charprop case-table epa-hook
jka-cmpr-hook help simple abbrev obarray cl-preloaded nadvice button
loaddefs faces cus-face macroexp files window text-properties overlay
sha1 md5 base64 format env code-pages mule custom widget
hashtable-print-readable backquote threads xwidget-internal dbusbind
inotify lcms2 dynamic-setting system-font-setting font-render-setting
cairo move-toolbar gtk x-toolkit x multi-tty make-network-process
native-compile emacs)

Memory information:
((conses 16 827372 493815)
 (symbols 48 47126 2)
 (strings 32 202612 63709)
 (string-bytes 1 6875606)
 (vectors 16 90294)
 (vector-slots 8 2023024 312170)
 (floats 8 1286 1723)
 (intervals 56 15402 985)
 (buffers 992 40))