[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#51377: Automatically exit server when it has no remaining clients

From: Jim Porter
Subject: bug#51377: Automatically exit server when it has no remaining clients
Date: Sun, 24 Oct 2021 12:39:02 -0700

On 10/24/2021 11:43 AM, Gregory Heytings wrote:

If `save-buffers-kill-emacs' were called after that, the Emacs daemon would be killed, losing the edits to bar.txt.

Indeed, you are correct.  Updated patch attached, which also takes care of running processes.

Yeah, that looks like it should avoid any data loss. However, it's not the behavior I'd personally expect. As I understand it, this will just keep the Emacs daemon alive if there are any unsaved files. I'd find it easier to use if Emacs warned the user about unsaved files before killing the last client. Then, Emacs can safely kill the daemon once I've confirmed that that's what I want.

I've attached a lightly-tested patch series that implements things in the way that I'd be happiest with. There are a few points I should explain though.

First, I chose to add a new --lazy-daemon option (I'm not sure about the name; feel free to suggest a better one). This makes it possible to distinguish whether the daemon was started on demand via `emacsclient'. I chose a command-line option rather than a command accepted by the server protocol because all the other server protocol commands can be used any time a new `emacsclient' is started; this option only makes sense when starting the daemon (since it describes how the daemon was started). I also chose to add this as a "third" daemon type rather than an entirely separate flag since I don't think there's any reason to support a lazily-created foreground daemon.

In the second patch, I added a minor performance optimization to `server-kill-emacs-query-function'. Now it stops once it finds the first live client. This patch isn't strictly required though, and I could rework things to exclude it.

Next, I updated `server-save-buffers-kill-terminal' to check if the server is a lazy daemon and if the current client is the last one: if so, it calls `save-buffers-kill-emacs', which then calls an improved `server-kill-emacs-query-function' that knows to avoid prompting unnecessarily when this is the last client.

Notably, the change to `server-save-buffers-kill-terminal' alters the behavior of killing a frame created by `emacsclient -n'. Looking at the code before, I don't see how it could possibly have been the right behavior: if there were multiple frames open, it only closed *one* frame, even if all of those frames were created after a single call of `emacsclient -n' (e.g. by calling `make-frame-command'). If it was the *last* frame, it killed Emacs, even if the user explicitly called `emacs --daemon' on system boot and wants the daemon to live forever. I've changed the behavior so that when you call this from a `nowait' frame, it kills Emacs if a) it's a lazy daemon and b) there are no other clients. That's a lot closer to the behavior when calling this from a "normal" client frame.

Finally, these patches would all require documentation improvements to merge. However, it didn't seem like a good use of my time to do that until people agree that this is the right strategy overall.

Attachment: 0001-Add-lazy-daemon-option-used-to-start-a-daemon-on-dem.patch
Description: Text document

Attachment: 0002-Stop-searching-for-live-clients-once-we-find-one.patch
Description: Text document

Attachment: 0003-When-killing-the-last-client-attached-to-a-lazy-daem.patch
Description: Text document

reply via email to

[Prev in Thread] Current Thread [Next in Thread]