Tk Source Code

View Ticket
Login
Ticket UUID: 220608
Title: Race in tkUnixWm.C:TkCoordsToWindow leads to X error
Type: Bug Version: obsolete: 8.2
Submitter: welch Created on: 2000-10-31 23:46:14
Subsystem: 67. Unix Window Operations Assigned To: nobody
Priority: 2 Severity:
Status: Closed Last Modified: 2001-03-23 02:24:17
Resolution: Fixed Closed By: dgp
    Closed on: 2001-03-22 19:24:17
Description:
OriginalBugID: 3328 Bug
Version: 8.2
SubmitDate: '1999-11-05'
LastModified: '1999-12-22'
Severity: CRIT
Status: Released
Submitter: techsupp
ChangedBy: hobbs
OS: Linux-Red Hat
FixedDate: '1999-12-22'
FixedInVersion: 8.3b1


Name:

David Ackley


CustomShell:

Only the included patch.


ReproducibleScript:

TO DUPLICATE:


    #start of deathwish.tcl

    puts "set tk_version [set tk_version]"

    update

    for {set i 0} {1} {incr i} {

        set xy [winfo pointerxy .]

        set win [winfo containing [lindex $xy 0] [lindex $xy 1]]

        after 10

    }

    #end of deathwish.tcl


    #start of deathwin.tcl

    for {set i 0} {1} {incr i} {

        toplevel .foo

        update

        after 500

        destroy .foo

    }

    #end of deathwin.tcl


    Do 'wish deathwish.tcl'.  Get your window manager to place windows

    automatically.  My window manager places this window at 0,0.


    Do 'wish deathwin.tcl' as well.  Mine repeatedly puts .foo at 0,0.


    Now you have .foo blinking on and off (over the deathwish window,

    although I don't think that matters).


    Put the pointer into the area where the .foo window is being created

    and destroyed.  Wait.  Watch deathwish.tcl get an X error and die

    (BadWindow, from X_TranslateCoords).  (If you have to wait more than

    ten or fifteen seconds, your experience is very different than mine.)


ObservedBehavior:

$ wish deathwish.tcl

    set tk_version 8.0

    X Error of failed request:  BadWindow (invalid Window parameter)

      Major opcode of failed request:  40 (X_TranslateCoords)

      Resource id in failed request:  0x140e7da

      Serial number of failed request:  1911

      Current serial number in output stream:  1911

    $


DesiredBehavior:

DIAGNOSIS:


    There is a race condition in tkUnixWm.c, at least in versions 8.0

    through 8.2, perhaps earlier.  Tk4.2 does not suffer from the problem;

    it appears to have been introduced whenever the concept of 'embedded

    toplevel' windows (for the Tcl plug-in?) appeared.


    In tkUnixWm.c, TkCoordsToWindow uses XTranslateCoordinates on a series

    of windows down from the root window of the relevant screen.  At each

    step, the 'child' window returned from one XTranslateCoordinates call

    is used as the 'parent' in the subsequent XTranslateCoordinates call.


    No provision is made to handle the possible failure of a

    XTranslateCoordinates call.  However, it is possible that the returned

    'child' window will be destroyed between the time it is returned from

    one call and the time it is supplied in the next call.  In such cases,

    the latter XTranslateCoordinates call will fail with a BadWindow

    error, and take the application down with it.


    SOLUTION:


    The supplied patch against tk8.2.0 corrects the problem, albeit in

    less than optimal fashion.  It sets an error handler around each call

    to XTranslateCoordinates, and jumps back to the top of the routine if

    a call fails.  As a desperation fallback, if find we've restarted more

    than a few times in a single call, we return NULL.  This amounts to

    bogusly claiming that the pointer is not in any of the application's

    windows, even though it might be --- but if we've tried several times

    and failed to locate which one, things must be pretty dynamic on the

    server, so perhaps we could get away with this little lie..  And

    perhaps not --- but it's still better than an X protocol error and

    death. 


    The patch below is likely suboptimal, at least because:

    (1) It sets and clears the errorhandler inside the loop.  Would be

        better to move that out, but the loop has too many exits for me to

        touch. 

    (2) The restart point might be higher than necessary.  Perhaps the

        original parent could be saved and reused.


Patch:

diff -c tkUnixWm.c.orig tkUnixWm.c

*** tkUnixWm.c.origSun Oct  3 20:22:35 1999

--- tkUnixWm.cSun Oct  3 23:36:47 1999

***************

*** 3490,3495 ****

--- 3490,3496 ----

      WmInfo *wmPtr;

      TkWindow *winPtr, *childPtr, *nextPtr;

      TkDisplay *dispPtr = ((TkWindow *) tkwin)->dispPtr;

+     int spincount = 0;


      /*

       * Step 1: scan the list of toplevel windows to see if there is a

***************

*** 3498,3503 ****

--- 3499,3505 ----

       * coordinates.

       */


+  restart:

      parent = window = RootWindowOfScreen(Tk_Screen(tkwin));

      x = rootX;

      y = rootY;

***************

*** 3529,3539 ****

       */


      while (1) {

! if (XTranslateCoordinates(Tk_Display(tkwin), parent, window,

! x, y, &childX, &childY, &child) == False) {

!     panic("Tk_CoordsToWindow got False return from XTranslateCoordinates");

  }

! if (child == None) {

      return NULL;

  }

  for (wmPtr = (WmInfo *) dispPtr->firstWmPtr; wmPtr != NULL; 

--- 3531,3567 ----

       */


      while (1) {

!         /* Sun Oct  3 23:33:17 1999 Ackley: Need to be careful.  Between

!    (1) the time we get a child window back from one 

!        call to XTranslateCoordinates, and 

!            (2) the time that we use that window as parent 

!        in the subsequent call of XTranslateCoordinates, 

!            that window might be destroyed.   If so the latter

!    XTranslateCoordinates gets a Bad Window error.  So, we

!    (1) Ignore X errors here

!    (2) If XTranslateCoords returns false, we going to make sure

!         we aren't looping too much.  If so, we'll just bogusly declare

!        that the pointer is NOT in our windows, otherwise

!    (3) We'll just jump back to restart again from the root(s) 

!        and hope the churn on the server has calmed down. 

! */

! 

!         int result;

! Tk_ErrorHandler errHandler;

! errHandler = Tk_CreateErrorHandler(dispPtr->display, -1, -1, -1,

!    (Tk_ErrorProc *) NULL, (ClientData) NULL);

!       

! result = 

!   XTranslateCoordinates(Tk_Display(tkwin), parent, window,

! x, y, &childX, &childY, &child);

! Tk_DeleteErrorHandler(errHandler);

! 

! if (result == False) {

!   if (++spincount > 5) return NULL; /* Last ditch lie during utter chaos.. */

!   goto restart;

  }

! 

!         if (child == None) {

      return NULL;

  }

  for (wmPtr = (WmInfo *) dispPtr->firstWmPtr; wmPtr != NULL;


PatchFiles:

tkUnixWm.c




This could also occur in Tix through another means.  A different
fix was applied for 8.3b1 where a message would be printed to
stderr, but no panic will occur. 
-- 12/22/1999 hobbs