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; }

Replies are listed 'Best First'.
Re: 2 problems using sockets perl 5.8 and mod_perl2 on Linux
by sgifford (Prior) on Jun 26, 2003 at 20:41 UTC
    I believe you can only rely on $! being set after you've already detected an error. Otherwise it can contain garbage. In this case, you're dying because there's some garbage in there:
    if ($!) { print STDERR "ERROR $!\n"; }
    I didn't see it in the documentation, but on my system $nf is set to -1 in the event of an error from select. Checking this instead of $! might fix your problem.
      Hello,

      I checked and $nf is set to 1 on my machine. Odd.

      I also added code to count the number of times this occurs and it happens thousands of times.

      -rr
Re: 2 problems using sockets perl 5.8 and mod_perl2 on Linux
by Thelonius (Priest) on Jun 27, 2003 at 00:15 UTC
    EINTR happens (in Perl 5.8) when a signal is caught. The correct response is to just call again with the same arguments. See 244415.

      Hi,

      Thanks, I can do that. But when this occurs it happens thousands of times in a row and chomps up lots of CPU. Is there a way to diagnose what's occurring? Where the signal is coming from etc?

      I still dont' know why the command line version of this program complains about bad file descriptor.

      Thanks -- rr

Re: 2 problems using sockets perl 5.8 and mod_perl2 on Linux
by Thelonius (Priest) on Jun 30, 2003 at 03:57 UTC
    Hello,

    I checked and $nf is set to 1 on my machine. Odd.

    I also added code to count the number of times this occurs and it happens thousands of times.

    I don't think you understood what sgifford said. When $nf is 1, there is no error. This is exactly what should be expected. You only look at $! to see what the error is after there is an error. The perl variable $! and the related C variable errno do not get reset to 0 when there is a successfull call. They will hold the previous error value until another error happens or the program specificly sets them. The moral is: Don't look at $! to see if there has been an error. Only look at it to see what error occurred after something else indicates that there has indeed been an error.

      Hello,

      Okay, I grok this and removed the
      if ($!) { print STDERR "ERROR $!\n"; }
      I don't understand why even without checking this the loop spins uncontrolably until it times out (notice the use of Time::HiRes).

      What I am observing as that this select loop keeps returning, $nf is set to 1, but there is no file descriptor in $eout, nor in $rout set to 1 and the loop just starts over again. This chomps up CPU and degrades the performance so badly I can't use it for it's intended purpose.

      Is the logic I am using to detect readable or erroring file descriptors perhaps incorrect?

      Thanks -- rr

        After much pondering, I think the problem is that you are initializing your bit vectors like this:
        my $rin = 0;
        but you should be doing this
        my $rin = '';
        When you say: $rin = 0; vec($rin, $fd, 1) = 1;, apparently the 0 is converted to the string "0" before the bit for $fd is set. This means that an extra bit will be set in each of your vectors.

        Personally, I've never understood vec() well enough to want to use it. I always use IO::Select and can_read() rather than that. I highly recommend the IO::Socket and IO::Select methods because they are so easy to use and, I think, less error-prone.