4

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?

4

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.

3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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