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

From: Spencer Baugh <[email-masked]>
Date: Mon, 29 Jan 2024 13:13:42 -0500

Eli Zaretskii <[email-masked]> writes:
>> From: Spencer Baugh <[email-masked]>
>> Cc: [email-masked]
>> Date: Mon, 29 Jan 2024 12:32:00 -0500
>> 
>> Eli Zaretskii <[email-masked]> writes:
>> 
>> >> 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.
>> >
>> > Or maybe server-start signals an error, and then the code which shows
>> > an error message and shuts down Emacs doesn't get run?
>> 
>> Yes, that's exactly what happens.
>> 
>> So should we wrap a condition-case around server-start, I suppose?
>
> Probably.  But perhaps we should also modify server-start so that,
> when it is called from a daemon, it signals in this case a distinct
> error, which startup.el could then detect and display a friendly error
> message.

Actually, it seems that main() already does a
fputs ("Error: server did not start correctly\n", stderr);
if the server process exits non-zero.  So we already get a nice:

Starting Emacs daemon.
Creating directory: Permission denied, /nonexistent
Error: server did not start correctly

So I think the below patch suffices.

>From 2a6f039663a678d0fcae47318011fbb5d8bb5f1c Mon Sep 17 00:00:00 2001
From: Spencer Baugh <[email-masked]>
Date: Mon, 29 Jan 2024 13:11:47 -0500
Subject: [PATCH] Message when server-start errors in emacs --daemon

Previously, if a user invoked emacs --daemon and then server-start
failed, there would be no indication of this and emacs would simply
hang.

Now, something like emacs --daemon=/nonexistent/sock results in:

$ emacs --fg-daemon=/nonexistent/sock
Starting Emacs daemon.
Creating directory: Permission denied, /nonexistent
$ emacs --daemon=/nonexistent/sock
Starting Emacs daemon.
Creating directory: Permission denied, /nonexistent
Error: server did not start correctly

and Emacs exits non-zero.

* lisp/startup.el (command-line): Catch errors from server-start, and
message and exit.  (bug#68799)
---
 lisp/startup.el | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lisp/startup.el b/lisp/startup.el
index 23937055f30..ae465b5bb07 100644
--- a/lisp/startup.el
+++ b/lisp/startup.el
@@ -1627,7 +1627,11 @@ command-line
   (let ((dn (daemonp)))
     (when dn
       (when (stringp dn) (setq server-name dn))
-      (server-start)
+      (condition-case err
+          (server-start)
+        (error
+         (message "%s" (error-message-string err))
+         (kill-emacs 1)))
       (if server-process
 	  (daemon-initialized)
 	(if (stringp dn)
-- 
2.39.3