Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 85f815191bb6b7cd1f6d2fa3ee590a6189a8361b
Ticket: 219252ffffffffffffffffffffffffffffffffff
Tcl deadlocks when closing a piped command.
User & Date: nobody on 2000-10-26 05:04:11
Changes

  1. assignee changed to: "nobody"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    OriginalBugID: 5759 Bug
    Version: 8.4a1
    SubmitDate: '2000-05-31'
    LastModified: '2000-08-22'
    Severity: CRIT
    Status: Assigned
    Submitter: techsupp
    ChangedBy: hobbs
    RelatedBugIDs: 6148
    OS: Linux-Red Hat
    FixedDate: '2000-10-25'
    ClosedDate: '2000-10-25'
    
    
    Name:
    
    Mo
    
    
    CVS:
    
    Mon May 29
    
    
    Comments:
    
    I was not sure where to put a regression test for this problem. I
    
        thought
    
        about adding one to ioCmd.test but the test I created would only work on
    
        UNIX. I am open to suggestions on where and how to add a test for this
    
        feature.
    
    
        Another interesting question is should the blocking status of the pipe
    
        have anything to do with the choice between using WNOHANG or 0 as the
    
        argument to Tcl_WaitPid?
    
    
    ReproducibleScript:
    
    This problem can be demonstrated in a simple two line script
    
    that will hang Tcl.
    
    
    set pipe [open {|/bin/sh -c "sleep 60"} r]
    
    close $pipe
    
    
    ObservedBehavior:
    
    The above script will hang a Tcl/Tk process for 60 seconds.
    
    If the program being run in the pipe was stuck in a loop, Tcl
    
    would never return.
    
    
    DesiredBehavior:
    
    I would like Tcl to return an error in the case where a process
    
    in the pipeline would deadlock. The right thing to do would be
    
    to tell the user what happened and then reap the pid later.
    
    
    Here is the output after my patch has been applied.
    
    
    set pipe [open {|/bin/sh -c "sleep 60"} r]
    
    close $pipe
    
    child status not available: 12257
    
    
    For multiple commands in a pipeline, we get multiple errors.
    
    
    set pipe [open {|/bin/sh -c "sleep 60" | /bin/sh -c "sleep 60" | /bin/sh -c "exit -1"} r]
    
    close $pipe
    
    child status not available: 12258
    
    child status not available: 12259
    
    child process exited abnormally
    
    
    Patch:
    
    Index: doc/close.n
    
    ===================================================================
    
    RCS file: /cvsroot/tcl/doc/close.n,v
    
    retrieving revision 1.2
    
    diff -u -r1.2 close.n
    
    --- close.n1998/09/14 18:39:511.2
    
    +++ close.n2000/05/29 11:05:32
    
    @@ -53,7 +53,9 @@
    
     .VE
    
     .PP
    
     The command returns an empty string, and may generate an error if
    
    -an error occurs while flushing output.
    
    +an error occurs while flushing output. The close command can also
    
    +generate an error if the child's status is not available because
    
    +the child has not yet terminated.
    
    
     .SH KEYWORDS
    
     blocking, channel, close, nonblocking
    
    Index: generic/tclPipe.c
    
    ===================================================================
    
    RCS file: /cvsroot/tcl/generic/tclPipe.c,v
    
    retrieving revision 1.3
    
    diff -u -r1.3 tclPipe.c
    
    --- tclPipe.c1999/04/16 00:46:511.3
    
    +++ tclPipe.c2000/05/29 11:05:36
    
    @@ -282,7 +282,7 @@
    
    
         abnormalExit = 0;
    
         for (i = 0; i < numPids; i++) {
    
    -        pid = Tcl_WaitPid(pidPtr[i], (int *) &waitStatus, 0);
    
    +        pid = Tcl_WaitPid(pidPtr[i], (int *) &waitStatus, WNOHANG);
    
     if (pid == (Tcl_Pid) -1) {
    
         result = TCL_ERROR;
    
                 if (interp != (Tcl_Interp *) NULL) {
    
    @@ -322,7 +322,31 @@
    
                                 (char *) NULL);
    
                     }
    
     abnormalExit = 1;
    
    -    } else if (WIFSIGNALED(waitStatus)) {
    
    +    } else if (pid == 0) {
    
    +                /*
    
    + * In this case, we tried to wait for a child
    
    +                 * that had not yet terminated. This is a special
    
    +                 * case, we need to put the child pid on our
    
    +                 * list of pids to be reaped later but we cant
    
    +                 * use the pid returned by Tcl_WaitPid as it is 0.
    
    +                 * Get the original child pid from pidPtr[i]
    
    +                 * and set a reasonable error code and message.
    
    +                 */
    
    +
    
    +                Tcl_DetachPids(1, &pidPtr[i]);
    
    +
    
    +                if (interp != (Tcl_Interp *) NULL) {
    
    +                    TclFormatInt(msg1, (long) pidPtr[i]);
    
    +
    
    +                    /* documentation for pclose uses ECHILD for this errno */
    
    +                    Tcl_SetErrorCode(interp, "ECHILD", msg1,
    
    +                            (char *) NULL);
    
    +                    
    
    +                    Tcl_AppendResult(interp, "child status not available: ", msg1, "\n",
    
    +                            (char *) NULL);
    
    +                }
    
    +
    
    +            } else if (WIFSIGNALED(waitStatus)) {
    
                     if (interp != (Tcl_Interp *) NULL) {
    
                         char *p;
    
    
    PatchFiles:
    
    doc/close.n
    
        generic/tclPipe.c
    
    
    
    
    Mo adds later:
    
    I later found that the change I suggested in 5759 caused
    some other problems with a normal exec, so it really should
    not be applied as is.
     
    -- 08/22/2000 hobbs
    
  4. foundin changed to: "obsolete: 8.4a1"
  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: "60. NRE and coroutines"
  10. title changed to: "Tcl deadlocks when closing a piped command."
  11. type changed to: "Bug"