7

If Emacs or the system crashes when desktop-save-mode is active, Emacs leaves a stale desktop lock file. The next time the desktop is loaded (from another Emacs process ID), loading is interrupted with a question:

Warning: desktop file appears to be in use by PID xxx.
Using it may cause conflicts. Use it anyway? (y or n)

even if if the process with PID xxx (substitute a process ID for xxx) is no longer alive.

Here is minimal example: First run emacs -Q -l init.el with the following init file (init.el):

(require 'desktop)
(let ((dir "/home/hakon/my-emacs-desktop-test"))
  (setq desktop-path (list dir))
  (desktop-save-mode 1))

Then open some new buffers, and exit emacs. This will generate a new desktop file. Now, create a fake lock file (with process ID 999 or substitute any integer that does not represent a running process ID)

echo "999" > /home/hakon/my-emacs-desktop-test/.emacs.desktop.lock

and rerun Emacs with a new init file emacs -Q -l init2.el, where init2.el is:

(require 'desktop)
(let ((dir "/home/hakon/my-emacs-desktop-test"))
  (setq desktop-path (list dir))
  (desktop-save-mode 1))
(desktop-read)

This should bring up the warning ("desktop file appears to be in use").

How can we get rid of this message?

1
  • On Windows 10 I get a stale desktop lock file just by shutting down normally with an open Emacs 28.1. Commented Apr 30, 2023 at 6:42

3 Answers 3

6

You can check if the process is still alive using system command ps -p $PID, then delete the lock file if the process is not still alive. The following modification of init2.el above shows how this can be done from within Emacs. Note that I use two private functions from desktop.el, namely desktop-owner and desktop-full-lock-name.

(require 'desktop)
(defun my-remove-stale-lock-file (dir)
  (let ((pid (desktop-owner dir)))
    (when pid
      (let ((infile nil)
            (destination nil)
            (display nil))
        (unless (= (call-process "ps" infile destination display "-p"
                                 (number-to-string pid)) 0)
          (let ((lock-fn (desktop-full-lock-name dir)))
            (delete-file lock-fn)))))))

(let ((dir "/home/hakon/my-emacs-desktop-test"))
  (my-remove-stale-lock-file dir)
  (setq desktop-path (list dir))
  (desktop-save-mode 1))
(desktop-read)

Now using this approach, the lock file should not exist at the time when desktop-read is called, and the warning is no longer issued.

Note:

There is also a variable desktop-load-locked-desktop in desktop.el. Setting this variable to t will load a desktop even if the lock file exist (and prevent the warning). But as far as I can see this does not check if the process is alive or not. So it might not be such a good idea in the case there is an alive Emacs process using the same desktop.

6

Building on the accepted answer, this two line solution loads for me without a locked prompt.

 (setq desktop-load-locked-desktop t)
 (call-interactively 'desktop-read t (vector "~/.emacs.d/desktops/" t))
2
  • per the docs setting desktop-load-locked-desktop to t just ignores the lock file. Commented Feb 15, 2022 at 17:19
  • I only have a ~/.emacs.desktop file not a ~/emacs.d/desktops/ directory. Modified code accordingly, and seem to work. Using Emacs 28.1 on Windows 10. Commented Apr 30, 2023 at 6:58
1

This version checks to see if the process in the lock file is still alive, but also that it is an emacs process, and it does it without calling out to ps since we can do that directly in elisp. Just ensure this block comes after you've set your desktop-dirname in your init.el:

;; Ensure we don't skip restoring the desktop after emacs has crashed
(require 'desktop)
(unless (equal (alist-get 'comm (process-attributes (or (desktop-owner) 0))) "emacs")
  (delete-file (desktop-full-lock-name)))
(desktop-read)

I tested this by launching a second emacs instance, and it properly warns that the desktop file appears to be in use. And when I pkill -9 emacs, leaving the lock file in place, I am able to successfully relaunch emacs and it deletes the lock file and recreates it without any input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.