I have a problem with hung processes with my Perl program, and I think I have isolated it to whenever I write significant amounts of data to a pipe.
Below is all of the code that I think is relevant from my program. When the program hangs, it hangs on the line in ResponseConstructor.pm: print { $self->{writer} } $data;
.
I've tested with different data sizes, and it doesn't appear to hang at an exact size. It may become more likely with size, though: sizes around 32KB sometimes work, sometimes don't. Every time I've tried a 110KB string it has failed.
I believe I've also ruled out the contents of the data as a cause, because the same data sometimes causes a hang, and othertimes doesn't.
This is probably the first time I have used pipes in a program before, so I'm not sure what to try next. Any ideas?
use POSIX ":sys_wait_h";
STDOUT->autoflush(1);
pipe(my $pipe_reader, my $pipe_writer);
$pipe_writer->autoflush(1);
my $pid = fork;
if ($pid) {
#I am the parent
close $pipe_writer;
while (waitpid(-1, WNOHANG) <= 0){
#do some stuff while waiting for child to send data on pipe
}
#process the data it got
open(my $fh, '>', "myoutfile.txt");
while ( my $line = <$pipe_reader>){
print $fh $line;
}
close $pipe_reader;
close $fh;
else {
#I am the child
die "cannot fork: $!" unless defined $pid;
close $pipe_reader;
my $response = ResponseConstructor->new($pipe_writer);
if ([a condition where we want to return small data]){
$response->respond('small data');
exit;
}
elsif ([a condition where we want to return big data]){
$response->respond('imagine this is a really big string');
}
}
ResponseConstructor.pm:
package ResponseConstructor;
use strict;
use warnings;
sub new {
my $class = shift;
my $writer = shift;
my $self = {
writer => $writer
};
bless($self, $class);
return($self);
}
#Writes the response then closes the writer (pipe)
sub respond {
my $self = shift;
my $data = shift;
print { $self->{writer} } $data;
close $self->{writer};
}
1;