Subject: bug#66151: 29.1.50; daemon crashing after X forwarding disconnects

From: Benjamin Schwehn <[email-masked]>
Date: Fri, 22 Sep 2023 16:28:40 +0200

Thanks a lot for helping me with this!

> > - emacs running as daemon via systemd user service
> > - running "emacsclient -nc" via ssh -X session
> > - stop X server on client machine or cut network connection between
> >  client and server
> > - run emacsclient -nc again
> >
> > Result:
> > emacs window opens for about a second, then the daemon crashes.
>
> Does this happen with any emacsclient command in this situation?  What
> if you don't use -c, for example, or use -t instead?

emacsclient -t also causes the crash, with this backtrace (looks the same to me)

emacs_backtrace at /home/ben/install/emacs/emacs/src/sysdep.c:2304
terminate_due_to_signal at /home/ben/install/emacs/emacs/src/emacs.c:458
deliver_process_signal at /home/ben/install/emacs/emacs/src/sysdep.c:1741
 (inlined by) deliver_fatal_signal at
/home/ben/install/emacs/emacs/src/sysdep.c:1789
deliver_thread_signal.constprop.0 at
/home/ben/install/emacs/emacs/src/sysdep.c:1765
?? ??:0
make_lisp_ptr at /home/ben/install/emacs/emacs/src/lisp.h:1364
 (inlined by) realize_default_face at
/home/ben/install/emacs/emacs/src/xfaces.c:5802
 (inlined by) realize_basic_faces at
/home/ben/install/emacs/emacs/src/xfaces.c:5736
recompute_basic_faces at /home/ben/install/emacs/emacs/src/xfaces.c:682
 (inlined by) recompute_basic_faces at
/home/ben/install/emacs/emacs/src/xfaces.c:677
 (inlined by) free_realized_fontsets at
/home/ben/install/emacs/emacs/src/fontset.c:1345
Fset_fontset_font at /home/ben/install/emacs/emacs/src/fontset.c:1696
eval_sub at /home/ben/install/emacs/emacs/src/eval.c:2515
Fprogn at /home/ben/install/emacs/emacs/src/eval.c:436
 (inlined by) Flet at /home/ben/install/emacs/emacs/src/eval.c:1026
eval_sub at /home/ben/install/emacs/emacs/src/eval.c:2449
Fprogn at /home/ben/install/emacs/emacs/src/eval.c:436
 (inlined by) prog_ignore at /home/ben/install/emacs/emacs/src/eval.c:447
 (inlined by) Fwhile at /home/ben/install/emacs/emacs/src/eval.c:1047
eval_sub at /home/ben/install/emacs/emacs/src/eval.c:2449
Fprogn at /home/ben/install/emacs/emacs/src/eval.c:436
 (inlined by) Flet at /home/ben/install/emacs/emacs/src/eval.c:1026
eval_sub at /home/ben/install/emacs/emacs/src/eval.c:2449
eval_sub at /home/ben/install/emacs/emacs/src/eval.c:2593
Fprogn at /home/ben/install/emacs/emacs/src/eval.c:436
eval_sub at /home/ben/install/emacs/emacs/src/eval.c:2449
eval_sub at /home/ben/install/emacs/emacs/src/eval.c:2449
eval_sub at /home/ben/install/emacs/emacs/src/eval.c:2593
Fprogn at /home/ben/install/emacs/emacs/src/eval.c:436
 (inlined by) funcall_lambda at /home/ben/install/emacs/emacs/src/eval.c:3233
Ffuncall at /home/ben/install/emacs/emacs/src/eval.c:2997
funcall_nil at /home/ben/install/emacs/emacs/src/eval.c:2679
run_hook_with_args at /home/ben/install/emacs/emacs/src/eval.c:2822
Frun_hooks at /home/ben/install/emacs/emacs/src/eval.c:2700 (discriminator 3)
eval_sub at /home/ben/install/emacs/emacs/src/eval.c:2472
Fprogn at /home/ben/install/emacs/emacs/src/eval.c:436
 (inlined by) funcall_lambda at /home/ben/install/emacs/emacs/src/eval.c:3233
Ffuncall at /home/ben/install/emacs/emacs/src/eval.c:2997
funcall_nil at /home/ben/install/emacs/emacs/src/eval.c:2679
run_hook_with_args at /home/ben/install/emacs/emacs/src/eval.c:2822
Frun_hooks at /home/ben/install/emacs/emacs/src/eval.c:2700 (discriminator 3)
Ffuncall at /home/ben/install/emacs/emacs/src/eval.c:2997
?? ??:0
funcall_subr at /home/ben/install/emacs/emacs/src/eval.c:3046
exec_byte_code at /home/ben/install/emacs/emacs/src/bytecode.c:809
Ffuncall at /home/ben/install/emacs/emacs/src/eval.c:2997
Ffuncall at /home/ben/install/emacs/emacs/src/eval.c:2997
?? ??:0
Ffuncall at /home/ben/install/emacs/emacs/src/eval.c:2997
safe_free at /home/ben/install/emacs/emacs/src/lisp.h:5373
 (inlined by) Fapply at /home/ben/install/emacs/emacs/src/eval.c:2668

> > >> deliver_thread_signal.constprop.0 at
> > >> /home/ben/install/emacs/emacs/src/sysdep.c:1765
> > >> ?? ??:0
> > >> make_lisp_ptr at /home/ben/install/emacs/emacs/src/lisp.h:1364
> > >>  (inlined by) realize_default_face at
> > >> /home/ben/install/emacs/emacs/src/xfaces.c:5802
> > >>  (inlined by) realize_basic_faces at
> > >> /home/ben/install/emacs/emacs/src/xfaces.c:5736
> > >
> > > The segfault seems to be caused by the frame's output_data.x being
> > > NULL or invalid.  Po Lu, is this caused by stopping the server or
> > > cutting the network connection between client and server?  If so, is
> > > there any remedy?
> >
> > The cause is the network connection being cut.  If a display disconnect
> > transpires while Emacs is operating on live frames connected to that
> > display, Emacs will crash, as the Xlib error handler deletes such frames
> > promptly.  There are no simple solutions or means of circumventing this
> > problem.
>
> I think in this case there were no live frames connected to the
> display, only the initial (non-GUI) daemon frame.  Benjamin, is that
> true?

The crash is triggered when a live frame was connected when the network
connection was cut, but the crash happens only later, the next time I open a
frame.  But I am not fully sure I correctly understand the question. Let me try
to explain better the circumstances:

I have emacs running in server mode on a VM. I have a windows machine running an
X server. Both machines are connected via a VPN which somtimes loses connection.
The issues comes after this connection loss. To reproduce I do this:

1. ssh -X into the machine and run emacsclient -nc. Emacs frame opens on the
   client (windows) machine.
2. While the frame is open, I disconnect (C-d, C-c in the terminal that has the
   ssh -X connection).
3. I reconnect to the server via ssh. At this point, the emacs server process
   has not yet crashed.
4. I run emacsclient -nc
5. On the client machine, an emacs frame opens and does some initial draw, then
   the server process crashes

If in step 4, I run emacclient -t instead, the server process also crashes. I
can't see an initial draw happening in this case.

If in step 2, I first close the frame, then disconnect, the crash in step 5 does
not happen (neither for -nc nor -t) and opening a frame works fine.

It's not a terrible issue for me, but annoyingly happens every time the VPN
connection is lost (~twice a day) and I have emacs open (~all the time :)).

Thanks, Ben