Subject: Re: bug#13864: 24.3.50; emacsclient -t loops when connected to emacs server running in X11

From: Eli Zaretskii <[email-masked]>
Date: Tue, 02 Apr 2013 20:10:16 +0300

> From: [email-masked] (Ashish SHUKLA)
> Cc: [email-masked]
> Date: Mon, 01 Apr 2013 22:15:46 +0530
> 
> Please refer to the attached output.

Thanks, I think we've finally nailed this sucker.

> I'm not sure if it's for the right frame (i.e. "garbaged" flag
> monitored for X11 frame, or emacsclient frame).

It is certainly for the right frame, because the code that sets the
"garbaged" flag is here:

  if (FRAME_TERMCAP_P (XFRAME (frame)) || FRAME_MSDOS_P (XFRAME (frame)))
    {
      if (FRAMEP (FRAME_TTY (XFRAME (frame))->top_frame))
	/* Mark previously displayed frame as now obscured.  */
	SET_FRAME_VISIBLE (XFRAME (FRAME_TTY (XFRAME (frame))->top_frame), 2);
      SET_FRAME_VISIBLE (XFRAME (frame), 1);  <<<<<<<<<<<<<<<<<<<<<<<<<<<
      FRAME_TTY (XFRAME (frame))->top_frame = frame;
    }

As you can see from the condition for this block, it is only run for
TTY (a.k.a. "termcap") frames.

I think the problem here is that the code sets the "garbaged" flag
even if the "top frame" of the TTY did not change at all.

Can you try the patch below?  Please try it both with a single TTY
frame on the xterm (in addition to a GUI frame), like what you did
until now, and also with several TTY frames on the same xterm (you can
create additional frames by "C-x 5" commands).

If this gives good results, I will install it.  Thanks.

=== modified file 'src/frame.c'
--- src/frame.c	2013-04-02 01:54:56 +0000
+++ src/frame.c	2013-04-02 17:06:50 +0000
@@ -803,10 +803,18 @@ do_switch_frame (Lisp_Object frame, int 
 
   if (FRAME_TERMCAP_P (XFRAME (frame)) || FRAME_MSDOS_P (XFRAME (frame)))
     {
-      if (FRAMEP (FRAME_TTY (XFRAME (frame))->top_frame))
-	/* Mark previously displayed frame as now obscured.  */
-	SET_FRAME_VISIBLE (XFRAME (FRAME_TTY (XFRAME (frame))->top_frame), 2);
-      SET_FRAME_VISIBLE (XFRAME (frame), 1);
+      Lisp_Object top_frame = FRAME_TTY (XFRAME (frame))->top_frame;
+
+      /* Don't mark the frame garbaged and/or obscured if we are
+	 switching to the frame that is already the top frame of that
+	 TTY.  */
+      if (!EQ (frame, top_frame))
+	{
+	  if (FRAMEP (top_frame))
+	    /* Mark previously displayed frame as now obscured.  */
+	    SET_FRAME_VISIBLE (XFRAME (top_frame), 2);
+	  SET_FRAME_VISIBLE (XFRAME (frame), 1);
+	}
       FRAME_TTY (XFRAME (frame))->top_frame = frame;
     }