Subject: Re: bug#6831: 23.2; emacsclient warning "can't find socket"

From: Juanma Barranquero <[email-masked]>
Date: Mon, 4 Jul 2011 17:28:55 +0200

> I run emacsclient on a gnu/linux machine and I can still see the
> warning without passing --server=server.

I don't think that's a bug.

You're running emacs / emacsclient in a POSIX environment, where the
default is *not* using TCP, but you're asking Emacs to use TCP:

  2. (setq server-use-tcp t)

and you're not specifying an alternate server name

  3. M-x server-start

so Emacs uses "server", the default.

Then you run emacsclient without passing --server-file (or -f), which
would ask for TCP:

  4. emacsclient -n .

so it defaults to trying local sockets first. Let's see what set_socket() does:

Try 1:
  #ifndef NO_SOCKETS_IN_FILE_SYSTEM
    /* Explicit --socket-name argument.  */
    if (socket_name)
      {
        s = set_local_socket ();
        if ((s != INVALID_SOCKET) || no_exit_if_error)
	  return s;
        message (TRUE, "%s: error accessing socket \"%s\"\n",
	         progname, socket_name);
        exit (EXIT_FAILURE);
      }
  #endif

but there's no explicit --socke-name arg, so

Try 2:
    /* Explicit --server-file arg or EMACS_SERVER_FILE variable.  */
    if (!server_file)
      server_file = egetenv ("EMACS_SERVER_FILE");

    if (server_file)
      {
        s = set_tcp_socket ();
        /* etc */
      }

but you didn't pass --server-file nor did you set EMACS_SERVER_FILE, so

Try 3:
  #ifndef NO_SOCKETS_IN_FILE_SYSTEM
    /* Implicit local socket.  */
    s = set_local_socket ();
    if (s != INVALID_SOCKET)
      return s;
  #endif

and set_local_socket tries to open the default local socket [at
(format "%s/emacs%d" (or (getenv "TMPDIR") "/tmp") (user-uid)))], and
fails, displays the error you're seeing, and returns. So,

Try 4:
    /* Implicit server file.  */
    server_file = "server";
    s = set_tcp_socket ();
    if ((s != INVALID_SOCKET) || no_exit_if_error)
      return s;

Finally! emacsclient tries a socket with the default name, and
succeeds, so the whole run works even if set_local_socket gave us a
warning.

When you explicitly pass --server=server, you shortcut the search at
Try 2, which works so set_local_socket() is not called.

Now, perhaps set_local_socket() should be somewhat more quiet and
leave the noise to set_socket(), but it is trying to be helpful and
distinguish between different error conditions; passing that back to
set_socket() wouldn't be particulary clean or elegant.

Moral: if you're taking the trouble to set the server for TCP, take
the trouble to say that to emacsclient. If typing " -f server" is too
much trouble and you always use the default name, try setting
EMACS_SERVER_FILE.

    Juanma