DCPM: Timeouts in modules

Neil Williams linux at codehelp.co.uk
Sun Nov 30 15:17:03 CST 2003


On Sunday 30 Nov 2003 8:03 pm, Simon Waters wrote:
> Neil,
>
> I tried this simplified version of your script, which works fine.
> How are you handling the alarm outside the block?

The full script is here, minus the handler code which caused the alarm to be 
missed.
http://isbnsearch.sourceforge.net/verify.html

> #!/usr/bin/perl
> eval{
>   local $SIG{ALRM} = sub{ die "my own alarm" };
>   alarm 5;
>   my $input=<>;
>   alarm 0;
> };
>
> if ( $@ =~ /my own alarm/) {
>
>    print "Too slow\n";
>
> }

Example of one of the alternatives tried:

	eval{
		local $SIG{ALRM} = sub{ die"Alarm" };
		alarm(10);
		$conn = $mgr->connect("$server", "$port");
		alarm 0;
	};

if ($@ =~ /Alarm/) {
		$bug = 9;
		report;
		next;
	}

report is a simple MySQL + print subroutine.

I tried all manner of handler codes, from simple print statements like that, 
to full MySQL output.

My problems seem to be based around either the loop (because all the examples 
use the eval{} block in a straight piece of code) or the module (which must 
the most awkward module I've ever tried to understand). My connect call has 
to go into a loop that can be called maybe 600 times.

I wouldn't expect to define the local $SIG{ALRM} line each time through the 
loop, but putting the local definition line outside (near the top of the 
script) doesn't change the lack of handling.

> I'm still mastering Perl, but assume the called code could also use or
> redefine the alarm signal handler?

With an alarm handler defined, I get:
[neil at mdk91 notes]$ perl -T verify.pl
Content-type: text/plain

Server count: 196
IP: 198.168.27.3
Server: www.biblinat.gouv.qc.ca, port: 210, database: IRIS

(I have to force the script to end using Ctrl-C).

With the alarm handler commented out or replaced with 
local $SIG{ALRM}=''; I get:
[neil at mdk91 notes]$ perl -T verify.pl
Content-type: text/plain

Server count: 196
IP: 198.168.27.3
Server: www.biblinat.gouv.qc.ca, port: 210, database: IRIS
Alarm clock

Note the Alarm clock default which halts the script - the alarm is working.

So I tried your example of input =<>; and the alarm becomes active again:

### isbnsearch cron task error report: 1 ###
--- isbnsearch ERROR: 5 Script crash - see mailing list ---
Error connecting to www.biblinat.gouv.qc.ca
Server details: IRIS - www.biblinat.gouv.qc.ca : 210
Reason: Interrupted system call

OK, it's not the error report I was expecting, (it should be error 9 not error 
5) but at least it shows that the loop isn't the problem - the error repeated 
for subsequent servers with the same delays - but it means it's going to be 
harder still to deal with the problem. It means now that the problem must be 
related to the module.

	eval{
		alarm(10);
#		$conn = $mgr->connect("$server", "$port");
		my $input=<>;
		alarm 0;
	};

works - albeit with error 5 instead of 9 as defined by the handler. By 
deliberately setting 9 in the handler, before calling die, I get the right 
error message:

local $SIG{ALRM} = sub{ $bug = 9; die"error9" };

	if ($@ =~ /error9/) {
		$bug = 9;
		print STDERR "alarm handled OK\n";
		report;
		next;
	}

(The handler is never being called because the print statement is ignored - 
the error output below is created by the die handler which acts as a 
catch-all.

### isbnsearch cron task error report: 1 ###
--- isbnsearch ERROR: 9 Connection request timed out ---
Error connecting to z3950.bl.uk
Server details: MAR - z3950.bl.uk : 2020
Reason: Interrupted system call
Server IP address: 194.66.234.45
Connection request timeout error.

(Reason is the output from $! - does that give any clues?)
(The Connection time out stuff is manual output generated from setting the 
error to 9.)

As soon as I re-instate the original $conn=$mgr->connect() line, the alarm is 
completely ignored, the handler is never called, the script doesn't die until 
the default timeout of some 200 seconds and I can only get it working again 
by deleting the handler code.
local $SIG{ALRM} = '';

But then all I get is
Alarm clock
again + halt.

Is $@ being overridden by the call within the alarm block?
Even when I comment out the entire die handler (+ warn handler too), 
$SIG{ALRM}=sub{die"anything"}; doesn't work!

-- 

Neil Williams
=============
http://www.codehelp.co.uk/
http://www.dclug.org.uk/
http://www.isbn.org.uk/
http://sourceforge.net/projects/isbnsearch/

http://www.biglumber.com/x/web?qs=0x8801094A28BCB3E3
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: signature
Url : http://mail.pm.org/archives/devoncornwall-pm/attachments/20031130/3d43ee56/attachment.bin


More information about the Devoncornwall-pm mailing list