salva has asked for the wisdom of the Perl Monks concerning the following question:
The issue I am facing on Windows is that when I run the slave process with its STDIN stream attached to a pipe and, after writing something, I close the perl side, the slave doesn't get the EOF.
The following program shows the issue:
open my $oldin, '<&', \*STDIN or die $!; pipe my($r), my ($w) or die $!; open STDIN, '<&', $r or die $!; my $pid = system 1, 'perl -ne print'; print "pid: $pid\n"; close STDIN or die $!; open STDIN, '<&', $oldin; close $r or die $!; print {$w} "hello world! ($_)\n" for 0..9; close $w or die $!; print "waiting for slave process\n"; waitpid $pid, 0 or die $!
If you run it you will see that the program stalls waiting for the slave to finish.
I can also reproduce the issue with IPC::Open2 open2:
use IPC::Open2 qw(open2); my $pid = open2 ">&STDOUT", my($w), 'perl -ne print' or die $!; print "pid: $pid\n"; print {$w} "hello world! ($_)\n" for 0..9; close $w or die $!; print "waiting for slave process\n"; waitpid $pid, 0 or die $!
On the other hand, when a regular file is used, everything works as expected:
open my $oldin, '<&', \*STDIN or die $!; open STDIN, '<&', \*DATA or die $!; my $pid = system 1, 'perl -ne print'; print "pid: $pid\n"; close STDIN or die $!; open STDIN, '<&', $oldin; print "waiting for slave process\n"; waitpid $pid, 0 or die $! __DATA__ hello world! (from DATA)
my $pid = open my $w, '|-', 'perl -ne print' or die $!; print "pid: $pid\n"; print {$w} "hello world! ($_)\n" for 0..9; close $w or die $!; print "waiting for slave process\n"; waitpid $pid, 0 or die $!
All that occurs to me is that the slave may be inheriting also the write side of the pipe.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Windows and pipes that don't close (close-on-exec)
by tye (Sage) on Dec 10, 2014 at 16:27 UTC | |
by salva (Canon) on Dec 10, 2014 at 17:03 UTC |