in reply to Script hangs when called via IPC::Open3

The cause is indeed that the subroutine is returning and closing the pipe to the external program without waiting for the child to finish. The number 1030 it reaches is probably just a buffering threshold at which it is waiting for the parent to flush - having not released the buffer it is not even looking for a closed pipe from the other side.

To maintain full control over all aspects of pipes, buffering, batching (e.g. reading and writing 1000 records at a time between two external programs) and so on (my external program is usually RDBMS or TSDBMS related), I usually need to write my own classes to manage open3, instead of using Run3, e.g. something like:

use IPC::Open3; use POSIX ":sys_wait_h"; package Query; sub new { my $self = shift; my %opt = @_; $self = \%opt; defined $self -> { SERVICE } or $self -> { SERVICE } = 'defaultqueryprog'; if ( defined $self -> { SEND } ) { my $pid = open3 my $wh, my $rh, my $eh, $self -> { SERVICE } or warn "$!: " . $self -> { SERVICE } . "\n" && exit $?; $self -> { WH } = $wh; $self -> { RH } = $rh; $self -> { EH } = $eh; $self -> { PID } = $pid; write $wh $self -> { SEND } . "\n"; } else { my $pid = open3 undef(), my $rh, my $eh, $self -> { SERVICE } or warn "$!: " . $self -> { SERVICE } . "\n" && exit $?; $self -> { RH } = $rh; $self -> { EH } = $eh; $self -> { PID } = $pid; } return bless $self; # note the pipe is not closed! } sub fetch { my $self = shift; my $rh = $self -> { RH }; my $eh = $self -> { EH }; my $oneliner = !wantarray; unless ( $oneliner &&= <$rh> ) { # cleanup if and only if flushing the pipe my @out = <$rh>; close $rh; my @err = <$eh>; close $eh; $self -> { STDERR } = \@err; waitpid $self -> { PID },0; delete $self -> { PID }; return @out; } return $oneliner; } sub fetcherror { # similar to fetch but on $self -> { EH }; ... } sub shut { my $self = shift; close $self -> { RH }, $self -> { EH }; $sts = $?; waitpid $self -> { PID }, 0; delete $self -> { PID }; return $sts; } 1;

-M

Free your mind