Basic problem:
----------------------------------------------------------------------
Metacity has several race conditions that can be triggered regarding
choice of focus window.  All of these involve the user doing something
so that a new focus window must be chosen (e.g. closing the currently
focused window), and then rapidly trying to focus a different window.
Having two windows be focused within a short period of each other can
result in the wrong window getting focus due to asynchronousness.

The short summary:
----------------------------------------------------------------------
I believe there are 5 representative cases under which races exist for
click-to-focus, and 9 for sloppy and mouse focus[1].  I have been able
to trigger nearly all of them for each focus mode.

The more detailed version:
----------------------------------------------------------------------
The basic cases under which I believe there are race conditions for
all focus modes are below.  In all cases, A, B, and C stand for
distinct windows.  Window A, if listed, is the one that originally has
focus.  Window B (or the no_focus_window sometimes) is the one that
sometimes gets focus due to getting focus events in the wrong order.
Window C is the one that we expect to be focused in all cases.  A
further explanation of these cases follow the table.

  Race condition cases
  ----------------------------------------------------------------------
  1) close A via X button, B under mouse, activate C
  2) close A via X button, nothing under mouse, B is mru, activate C
  3) minimize A via _ button, B under mouse, activate C
  4) minimize A via _ button, nothing under mouse, B is mru, activate C
  5) switch workspaces via keybinding; B under mouse, activate C
  6) switch workspaces via keybinding; nothing under mouse, B is mru, activate C
  7) unshow desktop via keybinding, B under mouse, activate C
  8) unshow desktop via keybinding, nothing under mouse, B is mru, activate C
  9) unshow desktop via applet, B is mru, activate C

  Further explanation
  ----------------------------------------------------------------------
  "B is mru" really means "B is the most recently used window exluding A"

  For click-to-focus: all the cases that list "B under mouse" aren't
  applicable.  The "nothing under mouse" part of the other cases is
  irrelevant.

  For sloppy focus: everything in the table applies.

  For mouse focus: the "B is mru" part of various cases is irrelevant.

There are other cases too which are quite similar (e.g. closing a
window with a keybinding instead of clicking on the X in the upper
right corner).  I have tested some of these and verified that they
also exhibit races in all focus modes, but the above set looks
representative to me of all the race conditions I suspect or know
about.  Of course, there may be other race conditions that I haven't
yet run across or suspected[2].

Now, while there are many race conditions, the difficulty to trigger
them varies greatly.  Some are much harder to produce than others on
any given machine, and on some machines it is difficult to trigger any
of these at all.  In my personal testing, triggering any of these race
conditions on my home machine was difficult (but with perserverance
and practice I was able to trigger some of the easier ones).  On my
machine at school, some of the race conditions are pretty easy to
trigger (and I in fact do so several times a day during normal
computer usage), requiring only a dozen or so attempts.  Others
required many more attempts and lots of practice to be able to
trigger.  The table below lists all the race conditions I was able to
trigger on my machine at school:

  Case   click   sloppy   mouse
    1     N/A       Y       Y 
    2      Y        Y       N
    3     N/A       Y       Y 
    4      N        Y       N
    5     N/A       Y       Y
    6      Y        Y       Y
    7     N/A       Y       Y
    8      Y        Y       Y
    9      Y        Y       Y

  Y - Yes, N - No, N/A - Not Applicable


[1] The reason that there are more for sloppy and mouse focus is
merely because they each have two choices for the window to be focused
depending on whether the mouse is contained within a window or not,
while click-to-focus only has a single choice.  Realistically, though,
about half the cases for sloppy and mouse focus could be considered
duplicates which would make the number of cases the same.

[2] I found out about most of the cases through code analysis and
hunches--and then later verified those hunches by testing and
triggering the problem.  Case 1 is the only one that I found out about
through triggering it during normal usage.  (And I was using mouse
focus; it's somewhat harder to trigger even that test case in
click-to-focus)
