Subject: Re: bug#68799: 30.0.50; emacs --fg-daemon fails silently if server-start fails

From: Eli Zaretskii <[email-masked]>
Date: Mon, 29 Jan 2024 19:11:36 +0200

> From: Spencer Baugh <[email-masked]>
> Date: Mon, 29 Jan 2024 11:54:21 -0500
> 
> 
> 1. emacs -Q --fg-daemon=/nonexistent/dir/sock
> 2. Emacs prints "Starting Emacs daemon." and sits in foreground.
> 3. emacsclient -c -s /nonexistent/dir/sock
> 4. emacsclient prints and exits:
> emacsclient: can't find socket; have you started the server?
> emacsclient: To start the server in Emacs, type "M-x server-start".
> emacsclient: error accessing socket "/nonexistent/dir/sock"
> 
> This is because in step 1, the server actually failed to start, but
> Emacs did not log that at all.  In fact, it's impossible to access the
> Emacs started in 1 now, since it's not actually running a server and it
> has no frames.
> 
> The same thing happens with --bg-daemon, although it's slightly more
> obvious that something is wrong in that case, because --bg-daemon is not
> supposed to sit in foreground.
> 
> Emacs with --fg-daemon should instead print an error and exit if it
> fails to start the server.

Have some faith in Emacs: we already do that.  From startup.el:

  (let ((dn (daemonp)))
    (when dn
      (when (stringp dn) (setq server-name dn))
      (server-start)
      (if server-process
	  (daemon-initialized)
	(if (stringp dn)
	    (message
	     "Unable to start daemon: Emacs server named %S already running"
	     server-name)
	  (message "Unable to start the daemon.\nAnother instance of Emacs is running the server, either as daemon or interactively.\nYou can use emacsclient to connect to that Emacs process."))
	(kill-emacs 1))))

So the problem seems to be that somehow server-start succeeds to leave
a non-nil server-process variable behind, although testing that is the
documented way of telling whether server is running.

So what is the value of server-process in your scenario?