Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Race condition in Tcl Notifier thread

Closed / Fixed218921ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 01:07:55
OriginalBugID: 2792 Bug
Version: 8.2
SubmitDate: '1999-09-16'
LastModified: '1999-10-19'
Severity: MED
Status: Released
Submitter: techsupp
ChangedBy: hobbs
OS: AIX
OSVersion: 4.3.2
Machine: RS6000
FixedDate: '1999-10-19'
FixedInVersion: 8.2.2
ClosedDate: '2000-10-25'


Name:

Nick Kisseberth



Comments:

The problem only occured on a multi-CPU machine. There is a race to

    Tcl_MutexLock(&notifierMutex) between the calling thread after it

    calls write(triggerPipe,"",1) and the notifier thread who wants to lock

    it so it can broadcast the event to everyone. The code expects the

    calling thread to win the race which it usually does on a single CPU

    machine, but on a multi-CPU machine notifier thread can get the lock

    first

    and broadcast the event (which the calling thread won't see because its

    not yet listening for the broadcast).



ObservedBehavior:

Several tests fail under AIX when run on a multi-CPU machine due to 

    a race condition when interrupting the notifier thread. Occurs with

    --enable-threads only.

    

    ==== event-1.1 Tcl_CreateFileHandler, reading FAILED

    ==== Contents of test case:

    

        testfilehandler close

        testfilehandler create 0 readable off

        testfilehandler clear 0

        testfilehandler oneevent

        set result ""

        lappend result [testfilehandler counts 0]

        testfilehandler fillpartial 0

        testfilehandler oneevent

        lappend result [testfilehandler counts 0]

        testfilehandler oneevent

        lappend result [testfilehandler counts 0]

        testfilehandler close

        set result

    

    ---- Result was:

    {0 0} {0 0} {0 0}

    ---- Result should have been:

    {0 0} {1 0} {2 0}

    ==== event-1.1 FAILED

    

    ==== io-50.1 testing handler deletion FAILED

    ==== Contents of test case:

    

        removeFile test1

        set f [open test1 w]

        close $f

        set f [open test1 r]

        testchannelevent $f add readable [list delhandler $f]

        proc delhandler {f} {

            global z

            set z called

            testchannelevent $f delete 0

        }

        set z not_called

        update

        close $f

        set z

    

    ---- Result was:

    not_called

    ---- Result should have been:

    called

    ==== io-50.1 FAILED

    

    ==== io-50.2 testing handler deletion with multiple handlers FAILED

    ==== Contents of test case:

    

        removeFile test1

        set f [open test1 w]

        close $f

        set f [open test1 r]

        testchannelevent $f add readable [list delhandler $f 1]

        testchannelevent $f add readable [list delhandler $f 0]

        proc delhandler {f i} {

            global z

            lappend z "called delhandler $f $i"

            testchannelevent $f delete 0

        }

        set z ""

        update

        close $f

        string compare [string tolower $z]  [list [list called delhandler $f

    0] [lis

    t called delhandler $f 1]]

    

    ---- Result was:

    -1

    ---- Result should have been:

    0

    

    ==== io-50.2 FAILED

    

    ==== io-50.3 testing handler deletion with multiple handlers FAILED

    ==== Contents of test case:

    

        removeFile test1

        set f [open test1 w]

        close $f

        set f [open test1 r]

        testchannelevent $f add readable [list notcalled $f 1]

        testchannelevent $f add readable [list delhandler $f 0]

        set z ""

        proc notcalled {f i} {

            global z

            lappend z "notcalled was called!! $f $i"

        }

        proc delhandler {f i} {

            global z

            testchannelevent $f delete 1

            lappend z "delhandler $f $i called"

            testchannelevent $f delete 0

            lappend z "delhandler $f $i deleted myself"

        }

        set z ""

        update

        close $f

        string compare [string tolower $z]  [list [list delhandler $f 0

    called]  [li

    st delhandler $f 0 deleted myself]]

    

    ---- Result was:

    -1

    ---- Result should have been:

    0

    ==== io-50.3 FAILED

    

    ==== io-50.4 testing handler deletion vs reentrant calls FAILED

    ==== Contents of test case:

    

        removeFile test1

        set f [open test1 w]

        close $f

        set f [open test1 r]

        testchannelevent $f add readable [list delrecursive $f]

        proc delrecursive {f} {

             global z u

            if {"$u" == "recursive"} {

                testchannelevent $f delete 0

                lappend z "delrecursive deleting recursive"

            } else {

                lappend z "delrecursive calling recursive"

                set u recursive

                update

            }

        }

        set u toplevel

        set z ""

        update

        close $f

        string compare [string tolower $z]  {{delrecursive calling

    recursive} {delre

    cursive deleting recursive}}

    

    ---- Result was:

    -1

    ---- Result should have been:

    0

    ==== io-50.4 FAILED

    ==== io-50.5 testing handler deletion vs reentrant calls FAILED

    ==== Contents of test case:

    

        removeFile test1

        set f [open test1 w]

        close $f

        set f [open test1 r]

        testchannelevent $f add readable [list notcalled $f]

        testchannelevent $f add readable [list del $f]

        proc notcalled {f} {

            global z

            lappend z "notcalled was called!! $f"

        }

        proc del {f} {

            global z u

            if {"$u" == "recursive"} {

                testchannelevent $f delete 1

                testchannelevent $f delete 0

                lappend z "del deleted notcalled"

                lappend z "del deleted myself"

            } else {

                set u recursive

                lappend z "del calling recursive"

                update

                lappend z "del after update"

            }

        }

        set z ""

        set u toplevel

        update

        close $f

        string compare [string tolower $z]  [list {del calling recursive}

    {del delet

    ed notcalled}  {del deleted myself} {del after update}]

    

    ---- Result was:

    -1

    ---- Result should have been:

    0

    ==== io-50.5 FAILED

    

    ==== io-50.6 testing handler deletion vs reentrant calls FAILED

    ==== Contents of test case:

    

        removeFile test1

        set f [open test1 w]

        close $f

        set f [open test1 r]

        testchannelevent $f add readable [list second $f]

        testchannelevent $f add readable [list first $f]

        proc first {f} {

            global u z

            if {"$u" == "toplevel"} {

                lappend z "first called"

                set u first

                update

                lappend z "first after update"

            } else {

                lappend z "first called not toplevel"

            }

        }

        proc second {f} {

            global u z

            if {"$u" == "first"} {

                lappend z "second called, first time"

                set u second

                testchannelevent $f delete 0

            } elseif {"$u" == "second"} {

                lappend z "second called, second time"

                testchannelevent $f delete 0

            } else {

                lappend z "second called, cannot happen!"

                testchannelevent $f removeall

            }

        }

        set z ""

        set u toplevel

        update

        close $f

        string compare [string tolower $z]  [list {first called} {first

    called not t

    oplevel}  {second called, first time} {second called, second time}

    {first after

     update}]

    

    ---- Result was:

    -1

    ---- Result should have been:

    0

    ==== io-50.6 FAILED



DesiredBehavior:

All tests should pass.



Patch:

*** tcl8.2.0/unix/tclUnixNotfy.c        Fri Jul  2 01:05:34 1999

--- tcl8.2.0-AIX/unix/tclUnixNotfy.c    Thu Sep  9 11:41:11 1999

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

*** 719,727 ****

          waitingListPtr = tsdPtr;

        tsdPtr->onList = 1;

        

!       Tcl_MutexUnlock(&notifierMutex);

        write(triggerPipe, "", 1);

!       Tcl_MutexLock(&notifierMutex);

      }

  

      memset((VOID *) tsdPtr->readyMasks, 0, 3*MASK_SIZE*sizeof(fd_mask));

--- 719,727 ----

          waitingListPtr = tsdPtr;

        tsdPtr->onList = 1;

        

!       /* Tcl_MutexUnlock(&notifierMutex);  */ /*NJK* Race condition */

        write(triggerPipe, "", 1);

!       /* Tcl_MutexLock(&notifierMutex);    */ /*NJK* Race condition */

      }

  

      memset((VOID *) tsdPtr->readyMasks, 0, 3*MASK_SIZE*sizeof(fd_mask));

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

*** 750,758 ****

          }

          tsdPtr->nextPtr = tsdPtr->prevPtr = NULL;

        tsdPtr->onList = 0;

!         Tcl_MutexUnlock(&notifierMutex);

        write(triggerPipe, "", 1);

!         Tcl_MutexLock(&notifierMutex);

  

      }

  

--- 750,758 ----

          }

          tsdPtr->nextPtr = tsdPtr->prevPtr = NULL;

        tsdPtr->onList = 0;

!       /* Tcl_MutexUnlock(&notifierMutex);  */ /*NJK* Race condition */

        write(triggerPipe, "", 1);

!       /* Tcl_MutexLock(&notifierMutex);    */ /*NJK* Race condition */

  

      }



PatchFiles:

tcl8.2.0/unix/tclUnixNotfy.c





Fixed in place for 8.3b1. 
-- 10/19/1999 hobbs
welch Closed ticket [218921ffff]. [History diff]2000-10-26 08:31:16
dgp Ticket [218921ffff]: 3 changes [History diff]2001-04-19 03:22:23
Log in (anonymous is fine) to add a comment.