Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2004-11-12
04:28 • Closed ticket [219252ffff]: Tcl deadlocks when closing a piped command. plus... artifact: 1b8b6dd1ad user: davygrvy
03:28 • Ticket [219252ffff]: 5 changes artifact: c06182fdd1 user: hobbs
2001-09-12
03:03 • Ticket [219252ffff]: 4 changes artifact: f3a99042ea user: andreas_kupries
2001-09-08
06:38 • Ticket [219252ffff]: 1 change artifact: 8b6452658c user: andreas_kupries
2001-04-05
08:20 • Ticket [219252ffff]: 1 change artifact: 4ee0737fc3 user: dgp
2001-01-09
22:19 • Ticket [219252ffff]: 4 changes artifact: 2c2b8e9e6f user: dkf
2000-10-26
05:04 • New ticket [219252ffff]. artifact: 85f815191b user: nobody

Tcl deadlocks when closing a piped command.

Closed / Works For Me219252ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 22 years ago
Descriptionplain2000-10-26 05:04:11
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
dkfplain2001-01-09 22:19:12
It would be fair for the error to only be generated in the case where blocking is not permitted ([fconfigure $pipe -blocking 0]) as this would also allow a tackling of bug #119255 with the same stone.
dgp Ticket [219252ffff]: 1 change [History diff]2001-04-05 08:20:12
andreas_kupries Ticket [219252ffff]: 1 change [History diff]2001-09-08 06:38:38
andreas_kupriesplain2001-09-12 03:03:47
Logged In: YES 
user_id=75003

Rather #219255 after SF renumbering tango.
hobbsplain2004-11-12 03:28:43
Logged In: YES 
user_id=72656

Passing to davygrvy - I think he addressed this.
davygrvyplain2004-11-12 04:28:32
Logged In: YES 
user_id=7549

With '-blocking 0' the [close] will not block, nor return
any exit status.  When that behavior came around in
unix/tclUnixPipe.c, I don't know for sure.

So currently (8.4 at least) there is a path for this.  Same
on windows, too.
Log in (anonymous is fine) to add a comment.