Subject: bug#30421: 25.3; desktop.el: Steal lock when no living "emacs" process owns it

From: Eli Zaretskii <[email-masked]>
Date: Sat, 17 Feb 2018 09:43:41 +0200

> From: Pierre Neidhardt <[email-masked]>
> Cc: [email-masked], [email-masked]
> Date: Fri, 16 Feb 2018 23:58:52 +0100
> 
> I tried patching with
> 
>                        -(daemonp)
>                        +(and (daemonp) (= (length (visible-frame-list)) 1))
> 
> Then I killed the daemon, leaving a .lock behind.
> On restart:
> 
> - In a tty, I get a black screen with a blinking cursor on the bottom left
> corner.
> If I press 'y', it proceeds with loading the desktop file.  I can verify
> that from the *Messages* buffer.
> 
> - With EXWM, I get a black screen, but 'y' does not work.  So it's
> effectively stuck.
> 
> I'm thinking that maybe `after-make-frame-functions' are run too early,
> before the frame is fully ready.
> 
> Then I re-considered Eli's last remark: the daemon already has a frame, so I
> thought maybe we should also guard the hook with a check on the frames count:
> 
> 	(defun ambrevar/desktop-init (_frame)
> 	    (when (> (length (visible-frame-list)) 1)
> 	      (desktop-save-mode)
> 	      (desktop-read)
> 	      (remove-hook 'after-make-frame-functions 'ambrevar/desktop-init)))
> 
> That did not change anything.
> 
> Any clue?

Instead of counting frames, I'd suggest using

  (frame-parameter FRAME 'client)

where FRAME is the argument with which your after-make-frame-functions
hook was called.  If this returns non-nil, FRAME is a frame created by
serving a request from emacsclient.

If this still doesn't work, please show the code of entire hook and
the complete recipe you used to test it, maybe there's something else
at work here.