> Also, could Joe English please review and provide his opinion on the patch?

The patch is not necessary. Under normal circumstances Tk does the right thing
inside Tk_CreateErrorHandler() ... Tk_DeleteErrorHandler() blocks, there is
 no need to call XSync(), and in fact doing so is likely to hurt performance.
(Also: my mistake, chw is correct, this logic is part of Tk, not Xlib.)

The cause of the problem is a bug in Tk's shutdown sequence.
From 2007-10-28 comment, the root cause:

> is because DeleteWindowsExitProc sets tsdPtr->displayList to NULL
> before calling TkCloseDisplay() on each member of the list.

The correct fix probably involves nulling out tsdPtr->displayList *after*
calling TkCloseDisplay() instead of before; IIRC the only reason I didn't do
that in 2007 is that the implications of making that change were unclear
(Tk's shutdown sequence is rather complicated.)