Subject: Re: bug#39977: 28.0.50; Unhelpful stack trace

From: martin rudalics <[email-masked]>
Date: Sun, 15 Mar 2020 18:49:21 +0100

> And what do you suggest to do if the frame at RHS is not live?

Sorry, what is RHS?  As far as xdisp.c is concerned it simply must not
set selected_frame to a dead frame.  Never ever.  As far as frame.c is
concerned, it should do something like in the attached patch.  In the
worst case this might make it impossible to remove a specific frame but
this can usually be fixed by C-x 5 2 followed by C-x 5 1 unless things
are awfully broken.  But at least this is a place from where to continue
investigating.  I have no better idea.

 >> I have no idea whether these could help in any way but since we are in
 >> redisplay and the selected frame has become dead all of a sudden ...
 >
 > Why do you think this is a problem for redisplay?

I didn't say that this is a problem for redisplay.  What I wanted to say
is that your fix which is supposed to handle a (maybe only temporary)
problem inside redisplay might cause more serious problems when
selecting a dead frame outside of redisplay.  But maybe I'm confusing
things.

martin
diff --git a/src/frame.c b/src/frame.c
index 88d6f22fc0..ef42f230ef 100644
--- a/src/frame.c
+++ b/src/frame.c
@@ -1466,6 +1466,11 @@ do_switch_frame (Lisp_Object frame, int track, int for_deletion, Lisp_Object nor
       tty->top_frame = frame;
     }

+  if (!FRAME_LIVE_P (f))
+    /* Return nil if for some reason FRAME has become dead now
+       (Bug#39977).  */
+    return Qnil;
+
   selected_frame = frame;
   if (! FRAME_MINIBUF_ONLY_P (XFRAME (selected_frame)))
     last_nonminibuf_frame = XFRAME (selected_frame);
@@ -2009,8 +2014,6 @@ delete_frame (Lisp_Object frame, Lisp_Object force)
 	error ("Attempt to delete the only frame");
     }

-  /* At this point, we are committed to deleting the frame.
-     There is no more chance for errors to prevent it.  */
   minibuffer_selected = EQ (minibuf_window, selected_window);
   sf = SELECTED_FRAME ();
   /* Don't let the frame remain selected.  */
@@ -2066,10 +2069,17 @@ delete_frame (Lisp_Object frame, Lisp_Object force)
 	Fraise_frame (frame1);
 #endif

-      do_switch_frame (frame1, 0, 1, Qnil);
+      if (!EQ (do_switch_frame (frame1, 0, 1, Qnil), frame1))
+	/* Error out if for some reason FRAME1 was not live
+	   (Bug#39977).  */
+	error ("No frame to switch to found");
+
       sf = SELECTED_FRAME ();
     }

+  /* At this point, we are committed to deleting the frame.
+     There is no more chance for errors to prevent it.  */
+
   /* Don't allow minibuf_window to remain on a deleted frame.  */
   check_minibuf_window (frame, minibuffer_selected);