Subject: bug#68255: 28.2; Can't connect to emacs daemon after logging out and back in.

From: Eli Zaretskii <[email-masked]>
Date: Fri, 05 Jan 2024 13:47:46 +0200

> From: Jeremy Phelps <[email-masked]>
> Date: Thu, 4 Jan 2024 14:52:17 -0500
> 
> 1. Configure your session manager not to kill leftover user processes
>    after logging out. If you're using elogind, this can be done by
>    editing /etc/elogind/logind.conf and setting KillUserProcesses=no.
>    Same process for systemd-logind, except the file is located
>    at /etc/systemd/logind.conf.
> 2. Log into a new session and run "emacs --daemon".
> 3. Log out of all sessions for that user. This causes the directory
>    named in $XDG_RUNTIME_DIR to be deleted, and with it, the Emacs
>    daemon's socket.
> 4. Log back in.
> 5. Observe that the emacs daemon's process is still there, and that
>    its socket is gone.
> 
> Working around this problem is difficult because the XDG_RUNTIME_DIR
> variable whose use causes this problem is referenced in two places:
> 
> 1. The "server-socket-dir" variable in Elisp. This is fairly easy to
>    change because Emacs can take you right to the definition.
> 2. The emacsclient binary. Changing the behavior here requires
>    editing emacsclient.c, recompiling, and installing the resulting
>    binary. Except it's too hard to get Emacs to build on modern
>    Linux distros, so I hex-edited the emacsclient binary instead,
>    NUL-ing out all strings containing "XDG_RUNTIME_DIR".

I'm not sure I understand why you cannot successfully use the daemon
in the situation you described.  Basically, what your scenario means
is that XDG_RUNTIME_DIR cannot be trusted because it is ephemeral, so
you need to tell Emacs and emacsclient to use a directory for a socket
in a way that doesn't use XDG_RUNTIME_DIR.  Is that correct?

If the above analysis is correct, then you can customize 'server-name'
to be an absolute file name of the directory where you want the socket
to be created, and invoke emacsclient with the --socket-name=NAME
option (or set EMACS_SOCKET_NAME=NAME in the environment), where NAME
is the same directory as in the value of 'server-name' option.  Did
you try that?