http://qs321.pair.com?node_id=269349

rr has asked for the wisdom of the Perl Monks concerning the following question:


Hello,

I am experiencing 2 problems with Perl 5.8 under mod_perl2 (apache 2) on Linux (Mandrake 9.1). Included is the subroutine and some example code to use it that can be used to replicate the problems.

Problem 1: When the program below is run, the program spits out "ERROR Bad file descriptor". There is no good reason for this. If I run the same code in the debugger it works correctly. I don't get it, I think this is a bug.

Problem 2: The subroutine below was written for a mod_perl module. When I make many connections to the server that causes this module to be run (subsequently executing the subroutine below) Apache's error_log contains many lines "ERROR Interrupted system call". By adding some debugging statements, I have discovered that this happens a few thousand times before stopping and uses up most/all of the cpu on the machine. What could be the cause of this? From the manpage I see EINTR is caused by an un-handled signal. Is there a way to tell what signal is causing this or where it came from?

Thanks for any help!

rr

#!/usr/bin/perl -w use strict; use Symbol; use Fcntl qw(:DEFAULT); use Socket; use Time::HiRes; print STDOUT simple("localhost", 80, "GET / HTTP/1.1\012Host:bidtxt.wh +enu.com\012\012"); exit 0; sub simple { my $host = shift || return undef; my $port = shift || return undef; my $mesg = shift || return undef; my $timeout = shift || 60; my $t0 = Time::HiRes::time(); my $ti; my $proto = getprotobyname('tcp'); my $iaddr = inet_aton($host) || return undef; my $paddr = sockaddr_in($port, $iaddr); my $s = Symbol::gensym; socket($s, PF_INET, SOCK_STREAM, $proto) || return undef; connect($s, $paddr) || return undef; my $fd = fileno($s); my $t; my $b; my $res; my $nf; my $tl; my $buf = "\000" x 8120; ## BUFSIZ my $l = length($mesg); my $rin = 0 ; my $win = 0 , my $ein = 0; my $rout = 0; my $wout = +0; my $eout = 0; vec($rin, $fd, 1) = 1; vec($win, $fd, 1) = 1; $ein = $rin | $win; my $i; $t = 0; $b = 0; while (($nf, $tl) = select(undef, $wout=$win, $eout=$ein, 2)) { $i++; $ti = Time::HiRes::time() - $t0; if ($ti > $timeout) { print STDERR "Timed out writing ($ti) $!\n"; shutdown($s, 2); return undef; } if (vec($wout, $fd, 1) == 1) { $b = syswrite($s, $mesg, $l - $b, $b); $t = $t + $b; last if $t = $l; } if (vec($eout, $fd, 1) == 1) { print STDERR "Got an error while writing: $!\n"; shutdown($s, 2); return undef; } } $t = 0; $l = 0; $b = 0; while (($nf, $tl) = select($rout=$rin, undef, $eout=$ein, 2)) { $i++; $ti = Time::HiRes::time() - $t0; if ($!) { print STDERR "ERROR $!\n"; } if ($ti > $timeout) { print STDERR "Timed out reading ($ti) $!\n"; shutdown($s, 2); return undef; } if (vec($rout, $fd, 1) == 1) { $b = sysread($s, $buf, 8192); $t = $t + $b; last if $b == 0; $res .= $buf; } if (vec($eout, $fd, 1) == 1) { print STDERR "Got an error while reading: $!\n"; shutdown($s, 2); return undef; } } shutdown($s, 2); return $res; }