Subject: bug#76870: 31.0.50; server-start with non-nil arg LEAVE-DEAD can write confusing warnings

From: Jens Schmidt <[email-masked]>
Date: Sun, 23 Mar 2025 16:22:08 +0100

On 2025-03-16  08:34, Eli Zaretskii wrote:

>> So would you mind if I also changed function `server-stop' to always
>> signal according to your proposal:
>>
>>   (signal 'server-running-external
>>           (list (format "The existing Emacs server, called \"%s\", could not be stopped."
>>                         server-name))))
>>
>> and then use the `(cadr err)' for both cases?
> 
> Yes, that'd be good.

I have attached two versions of the patch: The first

  0001-Improve-message-handling-on-server-stop.patch

with the messages we have agreed upon so far:

 Restart-then-stop:

  1  ⛔ Warning (server): Unable to start the Emacs server.
  2  The existing Emacs server, called "server", could not be stopped.
  3  To start the server in this Emacs process, stop the existing server or call M-x server-force-delete to forcibly disconnect it.

 Stop-only:

  4  ⛔ Warning (server): Unable to stop the server.
  5  The existing Emacs server, called "server", could not be stopped.
  6  (Perhaps it was run from a different Emacs session?)
  7  You can try stopping the server forcibly by calling M-x server-force-delete.

The second one

  0001-Improve-message-handling-on-server-stop-consistent.patch

fixes (IMO) two minor inconsistencies ("server" vs. "Emacs server"
in line 4, "Emacs process" vs. "Emacs session" in line 3):

 Restart-then-stop:

   1  ⛔ Warning (server): Unable to start the Emacs server.
   2  The existing Emacs server, called "server", could not be stopped.
   3  To start the server in this Emacs session, stop the existing server or call M-x server-force-delete to forcibly disconnect it.

 Stop-only:

   4  ⛔ Warning (server): Unable to stop the Emacs server.
   5  The existing Emacs server, called "server", could not be stopped.
   6  (Perhaps it was run from a different Emacs session?)
   7  You can try stopping the server forcibly by calling M-x server-force-delet

If one of these is OK for you (I'd prefer the second one), please
go ahead and install them, otherwise please let me know what still
needs fixing.

Thanks!
From 0f7a7e19ead341f1a6ae3a1d33588829d3eab9f3 Mon Sep 17 00:00:00 2001
From: Jens Schmidt <[email-masked]>
Date: Sat, 8 Mar 2025 18:13:54 +0100
Subject: [PATCH] Improve message handling on server stop

* lisp/server.el (server-stop): Use a clearer signal message if there is
an existing, external Emacs server.
(server-start): Use a different warning if one attempts to only
stop (and not restart) an external server.
(server-unload-function): Silently stop the server if unloading its
library.  (Bug#76870)
---
 lisp/server.el | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)

diff --git a/lisp/server.el b/lisp/server.el
index a49787e3498..2cbc23aa4ab 100644
--- a/lisp/server.el
+++ b/lisp/server.el
@@ -673,7 +673,7 @@ server-stop
                   (ignore-errors
                     (delete-directory (file-name-directory server-file))))))
             (signal 'server-running-external
-                    (list (format "There is an existing Emacs server, named %S"
+                    (list (format "The existing Emacs server, called \"%s\", could not be stopped."
                                   server-name))))
       ;; If this Emacs already had a server, clear out associated status.
       (while server-clients
@@ -725,16 +725,27 @@ server-start
           (when (server-stop)
             (message (if leave-dead "Stopped server" "Restarting server"))))
       (server-running-external
-       (display-warning
-        'server
-        (concat "Unable to start the Emacs server.\n"
-                (cadr err)
-                (substitute-command-keys
-                 (concat "\nTo start the server in this Emacs process, stop "
-                         "the existing server or call \\[server-force-delete] "
-                         "to forcibly disconnect it.")))
-        :warning)
-       (setq leave-dead t)))
+       (cond
+        ((not leave-dead)
+         (display-warning
+          'server
+          (concat "Unable to start the Emacs server.\n"
+                  (cadr err)
+                  (substitute-command-keys
+                   (concat "\nTo start the server in this Emacs process, stop "
+                           "the existing server or call \\[server-force-delete] "
+                           "to forcibly disconnect it.")))
+          :warning)
+         (setq leave-dead t))
+        (t
+         (display-warning
+          'server
+          (concat "Unable to stop the server.\n"
+                  (cadr err)
+                  (substitute-command-keys
+                   (concat "\n(Perhaps it was run from a different Emacs session?)\n"
+                           "You can try stopping the server forcibly by calling \\[server-force-delete].")))
+          :warning)))))
       ;; Now any previous server is properly stopped.
     (unless leave-dead
       (let ((server-file (server--file-name)))
@@ -2016,7 +2027,7 @@ ctl-x-map
 
 (defun server-unload-function ()
   "Unload the Server library."
-  (server-mode -1)
+  (ignore-errors (server-stop 'noframe))
   (substitute-key-definition 'server-edit nil ctl-x-map)
   (save-current-buffer
     (dolist (buffer (buffer-list))
-- 
2.39.5

From 0f7a7e19ead341f1a6ae3a1d33588829d3eab9f3 Mon Sep 17 00:00:00 2001
From: Jens Schmidt <[email-masked]>
Date: Sat, 8 Mar 2025 18:13:54 +0100
Subject: [PATCH] Improve message handling on server stop

* lisp/server.el (server-stop): Use a clearer signal message if there is
an existing, external Emacs server.
(server-start): Use a different warning if one attempts to only
stop (and not restart) an external server.
(server-unload-function): Silently stop the server if unloading its
library.  (Bug#76870)
---
 lisp/server.el | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)

diff --git a/lisp/server.el b/lisp/server.el
index a49787e3498..2cbc23aa4ab 100644
--- a/lisp/server.el
+++ b/lisp/server.el
@@ -673,7 +673,7 @@ server-stop
                   (ignore-errors
                     (delete-directory (file-name-directory server-file))))))
             (signal 'server-running-external
-                    (list (format "There is an existing Emacs server, named %S"
+                    (list (format "The existing Emacs server, called \"%s\", could not be stopped."
                                   server-name))))
       ;; If this Emacs already had a server, clear out associated status.
       (while server-clients
@@ -725,16 +725,27 @@ server-start
           (when (server-stop)
             (message (if leave-dead "Stopped server" "Restarting server"))))
       (server-running-external
-       (display-warning
-        'server
-        (concat "Unable to start the Emacs server.\n"
-                (cadr err)
-                (substitute-command-keys
-                 (concat "\nTo start the server in this Emacs process, stop "
-                         "the existing server or call \\[server-force-delete] "
-                         "to forcibly disconnect it.")))
-        :warning)
-       (setq leave-dead t)))
+       (cond
+        ((not leave-dead)
+         (display-warning
+          'server
+          (concat "Unable to start the Emacs server.\n"
+                  (cadr err)
+                  (substitute-command-keys
+                   (concat "\nTo start the server in this Emacs session, stop "
+                           "the existing server or call \\[server-force-delete] "
+                           "to forcibly disconnect it.")))
+          :warning)
+         (setq leave-dead t))
+        (t
+         (display-warning
+          'server
+          (concat "Unable to stop the Emacs server.\n"
+                  (cadr err)
+                  (substitute-command-keys
+                   (concat "\n(Perhaps it was run from a different Emacs session?)\n"
+                           "You can try stopping the server forcibly by calling \\[server-force-delete].")))
+          :warning)))))
       ;; Now any previous server is properly stopped.
     (unless leave-dead
       (let ((server-file (server--file-name)))
@@ -2016,7 +2027,7 @@ ctl-x-map

 (defun server-unload-function ()
   "Unload the Server library."
-  (server-mode -1)
+  (ignore-errors (server-stop 'noframe))
   (substitute-key-definition 'server-edit nil ctl-x-map)
   (save-current-buffer
     (dolist (buffer (buffer-list))
--
2.39.5