diff -urN metacity-cvs-clean/src/display.c metacity/src/display.c
--- metacity-cvs-clean/src/display.c	Mon Jul 22 17:44:26 2002
+++ metacity/src/display.c	Mon Jul 22 18:34:54 2002
@@ -279,6 +279,7 @@
   display->pending_pings = NULL;
   display->autoraise_timeout_id = 0;
   display->focus_window = NULL;
+  display->expected_focus_window = NULL;
   display->mru_list = NULL;
 
   display->showing_desktop = FALSE;
diff -urN metacity-cvs-clean/src/display.h metacity/src/display.h
--- metacity-cvs-clean/src/display.h	Mon Jul 22 17:44:26 2002
+++ metacity/src/display.h	Mon Jul 22 18:34:56 2002
@@ -144,6 +144,11 @@
    */
   MetaWindow *focus_window;
 
+  /* XSetInputFocus has been called but we have not yet 
+   * received the resulting FocusIn event for this window
+   */
+  MetaWindow *expected_focus_window;
+
   /* Most recently focused list. Always contains all
    * live windows.
    */
diff -urN metacity-cvs-clean/src/window.c metacity/src/window.c
--- metacity-cvs-clean/src/window.c	Mon Jul 22 17:44:26 2002
+++ metacity/src/window.c	Mon Jul 22 18:34:50 2002
@@ -793,6 +793,14 @@
                   window->desc);
       meta_screen_focus_top_window (window->screen, window);
     }
+  else if (window->display->expected_focus_window == window)
+    {
+      meta_topic (META_DEBUG_FOCUS,
+                  "Focusing top window since expected focus window freed %s\n",
+                  window->desc);
+      window->display->expected_focus_window = NULL;
+      meta_screen_focus_top_window (window->screen, window);
+    }
   else
     {
       meta_topic (META_DEBUG_FOCUS,
@@ -2750,6 +2758,7 @@
                           window->frame->xwindow,
                           RevertToPointerRoot,
                           CurrentTime);
+          window->display->expected_focus_window = window;
         }
     }
   else
@@ -2765,6 +2774,7 @@
                           window->xwindow,
                           RevertToPointerRoot,
                           timestamp);
+          window->display->expected_focus_window = window;
         }
       
       if (window->take_focus)
@@ -3480,6 +3490,9 @@
     
   if (event->type == FocusIn)
     {
+      if (window->display->expected_focus_window == window)
+        window->display->expected_focus_window = NULL;
+
       if (window != window->display->focus_window)
         {
           meta_topic (META_DEBUG_FOCUS,
