One other thing I noticed: 

after quitting the GUI frame, Ctrl-C to break the "waiting" loop and reconnecting with a terminal emacsclient, the *Messages* buffer shows the following error:

server-delete-client: X protocol error: BadFont (invalid Font parameter) on protocol request 46

Not sure if that helps at all.

On Mon, Sep 8, 2014 at 8:18 AM, Dmitry Antipov <dmantipov@yandex.ru> wrote:
On 09/08/2014 05:44 PM, Paul Eggert wrote:

I cannot reproduce this new problem on Ubuntu 14.04, configuring trunk bzr 117843 --with-x-toolkit=lucid.
x_delete_terminal calls XtCloseDisplay, and then calls emacs_close (dpyinfo->connection), and
the 'close' returns 0.

Perhaps you configured with some other toolkit?  That might explain the discrepancy.

No, this is Lucid but with your revert patch applied.

Does it fix things for you if you add a line 'dpyinfo->connection = -1;' after the existing line
'dpyinfo->display = 0;' in xterm.c's x_connection_closed?  Though that might cause a file descriptor
leak; I'm not fully following what's going on here, since I can't reproduce the new problem.

No, because x_connection_closed is not called.

There is another example of a debugging session, clearly showing double-close problem:

;; 1) Run ./src/emacs -Q --daemon
;; 2) Run ./lib-src/emacsclient -c
;; 3) Attach gdb -p to daemon process

(gdb) b close
Breakpoint 1 at 0x3290ce6c10: close. (4 locations)
(gdb) b x_connection_closed
Breakpoint 2 at 0x541d10: file ../../trunk/src/xterm.c, line 8425.
(gdb) b die
Breakpoint 3 at 0x5f6d05: file ../../trunk/src/alloc.c, line 7116.
(gdb) c
Continuing.

;; 4) C-x C-c in emacsclient frame

Breakpoint 1, close () at ../sysdeps/unix/syscall-template.S:81
81      T_PSEUDO (SYSCALL_SYMBOL, SYSCALL_NAME, SYSCALL_NARGS)
(gdb) n
close () at ../sysdeps/unix/syscall-template.S:82
82              ret
(gdb)
xcb_disconnect (c=0x13825e0) at xcb_conn.c:320
320         pthread_mutex_destroy(&c->iolock);
(gdb) p c->fd                                                      ;; X connection fd is 8
$1 = 8
(gdb) c
Continuing.

Breakpoint 1, close () at ../sysdeps/unix/syscall-template.S:81
81      T_PSEUDO (SYSCALL_SYMBOL, SYSCALL_NAME, SYSCALL_NARGS)
(gdb) n
83      T_PSEUDO_END (SYSCALL_SYMBOL)
(gdb)
posix_close (fd=8, flag=1) at ../../trunk/src/sysdep.c:2386        ;; We're closing X connection fd again
2386    }
(gdb) c

Continuing.
Breakpoint 3, die (msg=0x717274 "errno != EBADF || fd < 0", file=0x7170e0 "../../trunk/src/sysdep.c", line=2408)
    at ../../trunk/src/alloc.c:7116
7116      fprintf (stderr, "\r\n%s:%d: Emacs fatal error: assertion failed: %s\r\n",
(gdb) bt 6
#0  0x00000000005f6d05 in die (msg=0x717274 "errno != EBADF || fd < 0", file=0x7170e0 "../../trunk/src/sysdep.c", line=2408)
    at ../../trunk/src/alloc.c:7116
#1  0x0000000000598480 in emacs_close (fd=8) at ../../trunk/src/sysdep.c:2408         ;; This is it
#2  0x000000000054784b in x_delete_terminal (terminal=0xfa0218) at ../../trunk/src/xterm.c:11382
#3  0x000000000051f8b6 in Fdelete_terminal (terminal=..., force=...) at ../../trunk/src/terminal.c:348
#4  0x00000000004290ba in delete_frame (frame=..., force=...) at ../../trunk/src/frame.c:1691
#5  0x0000000000429630 in Fdelete_frame (frame=..., force=...) at ../../trunk/src/frame.c:1801

Dmitry