Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: fe1fec346f6d3ecc4d878666c6eedf797b921b3d
Ticket: 217956ffffffffffffffffffffffffffffffffff
bg exec causes Tcl to hang on exit under Linux
User & Date: nobody on 2000-10-26 00:46:34
Changes

  1. assignee changed to: "nobody"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    OriginalBugID: 4140 Bug
    Version: 8.3b2
    SubmitDate: '2000-02-07'
    LastModified: '2000-02-07'
    Severity: CRIT
    Status: Closed
    Submitter: techsupp
    ChangedBy: ericm
    RelatedBugIDs: 4139
    OS: Linux
    OSVersion: Linux
    Machine: i686
    FixedDate: '2000-10-25'
    ClosedDate: '2000-02-07'
    
    
    Name:
    
    mo
    
    
    
    CVS:
    
    Feb 5th
    
    
    
    CustomShell:
    
    None
    
    
    
    Comments:
    
    Here is a quick stack trace that I got from the hung process.
    
        
    
        #0  0x40147164 in __syscall_sigsuspend ()
    
        #1  0x4016b6cc in __DTOR_END__ ()
    
        #2  0x4017c513 in pthread_cond_wait (cond=0x804bf88, mutex=0x8049f80) at
    
        restart.h:49
    
        #3  0x4009d057 in Tcl_ConditionWait (condPtr=0x400b0c78,
    
        mutexPtr=0x400b0c74, timePtr=0x0)
    
            at ../../unix/tclUnixThrd.c:634
    
        #4  0x4009d2a6 in Tcl_FinalizeNotifier (clientData=0x8049c58)
    
            at ../../unix/tclUnixNotfy.c:268
    
        #5  0x40078fe7 in TclFinalizeNotifier () at
    
        ../../generic/tclNotify.c:152
    
        #6  0x40050996 in Tcl_FinalizeThread () at ../../generic/tclEvent.c:878
    
        #7  0x4005081e in Tcl_Finalize () at ../../generic/tclEvent.c:778
    
        #8  0x400505d1 in Tcl_Exit (status=0) at ../../generic/tclEvent.c:560
    
        
    
        Here is another quick bit of output that shows the select() problem
    
        in action. I added some debug statements around the select() in the
    
        notifier thread and got this output.
    
        
    
        // With Bug
    
        
    
        % exit
    
        
    
        // Hung until you kill children manually
    
        
    
        Notifier Thread : select() returned
    
        Notifier Thread : reading trigger pipe
    
        Notifier Thread : cond1 = 32, data = 0, cond2 = 1
    
        Notifier Thread : reading EOF on trigger pipe
    
        
    
        // Without bug
    
        
    
        % exit
    
        Notifier Thread : select() returned
    
        Notifier Thread : reading trigger pipe
    
        Notifier Thread : cond1 = 32, numbytes = 1, buf = "q"
    
        Notifier Thread : trigger pipe was closed
    
    
    
    ReproducibleScript:
    
    First save the following as a file, and run in a thread enabled version
    
    of Tcl (I used 8.3 from the CVS but this should hang others too).
    
    
    
    # start of file "sleepscript"
    
    
    
    echo "while true
    
    do
    
    sleep 3
    
    echo wakeup \$1 \$\$
    
    done
    
    " > /tmp/sleepscript
    
    
    
    /bin/sh /tmp/sleepscript child &
    
    
    
    exec /bin/sh /tmp/sleepscript parent
    
    
    
    # end of file "sleepscript"
    
    
    
    
    
    Now run the following commands in the Tcl shell.
    
    
    
    exec /bin/sh sleepscript &
    
    
    
    exit
    
    
    
    ObservedBehavior:
    
    After exit is called, Tcl will hang. If you manually kill off the
    
    subprocesses that were exec'ed, the script will finish after each
    
    subprocess has terminated.
    
    
    
    DesiredBehavior:
    
    The Tcl script should exit.
    
    
    
    Patch:
    
    Index: unix/tclUnixNotfy.c
    
    ===================================================================
    
    RCS file: /cvsroot/tcl/unix/tclUnixNotfy.c,v
    
    retrieving revision 1.7
    
    diff -u -r1.7 tclUnixNotfy.c
    
    --- tclUnixNotfy.c2000/01/21 02:26:251.7
    
    +++ tclUnixNotfy.c2000/02/07 20:05:20
    
    @@ -264,7 +264,20 @@
    
     if (triggerPipe < 0) {
    
         panic("Tcl_FinalizeNotifier: notifier pipe not initialized");
    
     }
    
    +
    
    +/* Send "q" message to the notifier thread so that it will
    
    +   terminate. The notifier will return from its call to
    
    +   select() and notice that a "q" message has arrived, it
    
    +   will then close its side of the pipe and terminate its
    
    +   thread. Note the we can not just close the pipe and check
    
    +   for EOF in the notifier thread because if a background
    
    +   child process was created with exec, select() would not
    
    +   register the EOF on the pipe until the child processes
    
    +   had terminated.
    
    +*/
    
    +write(triggerPipe, "q", 1);
    
     close(triggerPipe);
    
    +
    
     Tcl_ConditionWait(&notifierCV, &notifierMutex, NULL);
    
         }
    
     
    
    @@ -991,10 +1004,11 @@
    
      * to avoid a race condition we only read one at a time.
    
      */
    
     
    
    -if ((masks[index] & bit) && (read(receivePipe, buf, 1) == 0)) {
    
    +if ((masks[index] & bit) &&
    
    +            ((read(receivePipe, buf, 1) == 1) && buf[0] == 'q')) {
    
         /*
    
    -     * Someone closed the write end of the pipe so we need to
    
    -     * shut down the notifier thread.
    
    +     * Someone sent us a Quit message and closed the write end
    
    +     * of the pipe so we need to shut down the notifier thread.
    
          */
    
     
    
         break;
    
    Index: unix/tclUnixThrd.c
    
    ===================================================================
    
    RCS file: /cvsroot/tcl/unix/tclUnixThrd.c,v
    
    retrieving revision 1.7
    
    diff -u -r1.7 tclUnixThrd.c
    
    --- tclUnixThrd.c2000/01/12 11:13:561.7
    
    +++ tclUnixThrd.c2000/02/07 20:05:20
    
    @@ -340,7 +340,7 @@
    
     MASTER_LOCK;
    
     if (*mutexPtr == NULL) {
    
         /* 
    
    -     * Double inside master lock check to avoid a race.
    
    +     * Double check inside master lock to avoid a race condition.
    
          */
    
         
    
         pmutexPtr = (pthread_mutex_t *)ckalloc(sizeof(pthread_mutex_t));
    
    @@ -358,7 +358,7 @@
    
     /*
    
      *----------------------------------------------------------------------
    
      *
    
    - * TclpMutexUnlock --
    
    + * Tcl_MutexUnlock --
    
      *
    
      *This procedure is invoked to unlock a mutex.  The mutex must
    
      *have been locked by Tcl_MutexLock.
    
    
    
    PatchFiles:
    
    unix/tclUnixNotfy.c
    
        unix/tclUnixThrd.c (minor comment cleanup only)
    
    
    
    
    
    
    
  4. foundin changed to: "obsolete: 8.3b2"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. status changed to: "Open"
  8. submitter changed to: "nobody"
  9. subsystem changed to: "69. Other"
  10. title changed to: "bg exec causes Tcl to hang on exit under Linux"
  11. type changed to: "Bug"