develooper Front page | perl.perl5.porters | Postings from January 2001

[PATCH] Re: [ID 20010105.002] close() on process filehandle leaves defunctprocess

Thread Previous | Thread Next
From:
Radu Greab
Date:
January 5, 2001 11:04
Subject:
[PATCH] Re: [ID 20010105.002] close() on process filehandle leaves defunctprocess
Message ID:
14934.6849.742435.23178@ix.netsoft.ro
On , 5 Jan 2001 13:30 -0000, mjd@plover.com wrote:
 > This program leaves a defunct process in the table during the 'sleep':
 > 
 >         open(TIME, "/usr/bin/date |");
 >         @time=<TIME>;
 >         close(TIME);
 >         sleep 20;
 > 
 > The manual says:
 > 
 >         Closing a pipe also waits for the process executing on the
 >         pipe to complete,

The pipe was closed before open returned, but perl didn't reap the
child if an error occurred. Indeed, perl -w on the test script shows:

 Name "main::time" used only once: possible typo at ../t9.pl line 2.
 Can't exec "/usr/bin/date": No such file or directory at ../t9.pl line 1.
 readline() on closed filehandle TIME at ../t9.pl line 2.

The bug is fixed by the patch below which calls wait4pid() as in
Perl_my_pclose.


Radu Greab


--- util.c~	Mon Jan  1 02:02:31 2001
+++ util.c	Fri Jan  5 19:55:12 2001
@@ -2455,8 +2455,12 @@
 	PerlLIO_close(pp[0]);
 	did_pipes = 0;
 	if (n) {			/* Error */
+	    int pid2, status;
 	    if (n != sizeof(int))
 		Perl_croak(aTHX_ "panic: kid popen errno read");
+	    do {
+		pid2 = wait4pid(pid, &status, 0);
+	    } while (pid2 == -1 && errno == EINTR);
 	    errno = errkid;		/* Propagate errno from kid */
 	    return Nullfp;
 	}
--- t/io/pipe.t~	Fri Dec  1 23:50:28 2000
+++ t/io/pipe.t	Fri Jan  5 20:43:07 2001
@@ -11,7 +11,7 @@
 }
 
 $| = 1;
-print "1..15\n";
+print "1..16\n";
 
 # External program 'tr' assumed.
 open(PIPE, "|-") || (exec 'tr', 'YX', 'ko');
@@ -185,3 +185,21 @@
 }
 print "ok 15\n";
 $? = 0;
+
+# check that child is reaped if the piped program can't be executed
+{
+  local $SIG{CHLD} = 'DEFAULT';
+  open NIL, '/no_such_process |';
+  close NIL;
+
+  my $child = 0;
+  eval {
+    local $SIG{ALRM} = sub { die; };
+    alarm 2;
+    $child = wait;
+    alarm 0;
+  };
+
+  print "not " if $child != -1;
+  print "ok 16\n";
+}

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About