Re: timeout problems

Tim Allwine (tallwine@ixlabs.com)
Mon, 2 Aug 1999 09:49:22 -0700 (PDT)


Bruno Semrau said...
>
>Hello,
>i have a problem with libwww 5.43, perl 5.005_02 and timeouts.
>
>i have set the UserAgent Timeout to 20 sec.
>if i try to connect to a http server and there is a broken pipe, or the
>server is down, then the UserAgent timeout is so long (over 5 min.), or
>it never timeouts.
>

I have had similar problems as have many writing to this list. I have
seen little discussion on the issue.

Myself, before I GET or POST, I fork. The parent listens the child writes
to the parent. I use the module Storeable to write the data back to the 
parent.

This is a method from a module that I use to monitor web sites. The parent
will wait for 3 minutes.

sub my_fork {
	my $self = shift;
	pipe(R,W);          # R is the reader W is the writer
	W->autoflush(1);
	$SIG{ALRM} = sub {die 'timeout'};
	my $pid;
	if ( $pid = fork) {
		eval {
			alarm 180; # 3 minutes or whatever you want
			close W;
			$self->{fork} = retrieve_fd(*R);
			close R;
			waitpid($pid,0);
			alarm 0;
		};
		   
		if ($@) {
			if($@ =~ /timeout/) {
				push(@{$self->{Log}},["Process hung. Can not connect with server. Killing pro
				unless( kill 2,$pid ) {
					kill 9,$pid;
				}
			} else {
				alarm 0;
				push(@{$self->{Log}},["Died. $@ -- Killing process $pid",1]);
					unless( kill 2,$pid ) {
					kill 9,$pid;
				}
			}
		} else {
		push(@{$self->{fork}{Log}},["Returned successfully from fork",0]);
		return 1;
		}
	} else {
		push(@{$self->{Log}},["Can not fork",1]) unless defined $pid;
		$self->_get();
		close R;
		store_fd($self, *W);
		close W;
		exit;
	}
	return;
}




-- 
Tim Allwine
IX Development Laboratories
(707)-543-8030 Ext.15