Subject: Re: bug#17691: 24.3.91; crash closing remote frame

From: Ken Raeburn <[email-masked]>
Date: Thu, 12 Jun 2014 17:14:39 -0400

I've noticed Emacs doing a lot of garbage collection after I've lost a
remote network connection where I had an Emacs window displayed. (This is
Emacs 24.3.91 with the changes from Dmitry; without the changes the lost
connection would kill Emacs altogether.) Typically it seems to be GCing a
few times in the span of a couple seconds or so, mostly after I've typed
something. Each character seems to be enough to trigger it, so if I start
typing a line of text, the "Garbage collecting..." messages are constantly
flickering in the minibuffer.

I eventually traced it back to lots of invocations of timer callbacks;
while I'm still tracing down exactly why they're happening so much, I
noticed that the CPU utilization of Emacs is hovering around 100% now.
(It's sluggish but does still respond.) According to strace it seems to be
constantly doing this, over and over:

[pid  5736] 16:27:14.008209 clock_gettime(CLOCK_REALTIME, {1402604834,
8265741}) = 0
[pid  5736] 16:27:14.008428 pselect6(21, [7 8 10 11 12 13 16 18 20], [],
NULL, {45, 991734259}, {NULL, 8}) = 1 (in [20], left {45, 991729436})
[pid  5736] 16:27:14.008666 poll([{fd=11, events=POLLIN}], 1, 0) = 0
(Timeout)
[pid  5736] 16:27:14.008925 clock_gettime(CLOCK_REALTIME, {1402604834,
8985614}) = 0
[pid  5736] 16:27:14.009215 recvfrom(7, 0x3ef1ed4, 4096, 0, 0, 0) = -1
EAGAIN (Resource temporarily unavailable)

In this process, fd 20 is the dropped TCP connection for the remote
display, and fd 7 is the unix-domain socket to the local display. Since the
remote connection is closed, pselect6 returns immediately, but we never
drop it from the fd set.

I'm still trying to figure out if it's incorrectly firing an idle timer
handler each time around a loop or something like that, which might account
for the excessive garbage collection.

My test method:
1) Start (modified) Emacs on :0 in daemon mode via emacsclient -c -a "" -n
2) Use ssh to log into the desktop again with a different $DISPLAY
3) Use emacsclient to get an X11 window popped up
4) Use "~." to kill the ssh session
5) Use top, strace, etc to look at the spinning Emacs process