DCPM: body bags

Neil Williams linux at codehelp.co.uk
Fri Oct 31 17:25:16 CST 2003


On Friday 31 Oct 2003 10:54 am, Steve Marvell wrote:
> On the subject of "what do I do with a die from a module way way down
> the chain of command?":
>
> #!/usr/bin/perl
>
> local $SIG{__DIE__} = sub {
>   die "You killed Kenny! @_";
> };
>
> die "you bastard";
>
> Be careful not to exit() in these handlers, since that really break
> thing when someone evals it and expects to catch the dies.
>

By using __WARN__ and __DIE__ I've caught nearly all the horrible errors that 
have been plagueing me. (Thanks!)

Yet I can't catch this one:

(in cleanup) Net::Z3950::Manager=HASH(0x82ab24c) can't forget 
Net::Z3950::Connection=HASH(0x8358b00) at 
/usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi/Net/Z3950/Manager.pm 
line 310 during global destruction.

This snippet demonstrates some of the methods I've used to try to get the 
module to forget the current connection. (All attempts further into the 
script / closer to the connection itself have also failed).

local $SIG{__DIE__} = sub{
	@_ = '';
	$conn = qw//;
	#$conn->close();
#	my @cleanup = $mgr->connections();
#	my $yy = @cleanup;
#	print STDERR qq/number of connections: $yy\n/;
#	foreach $conn (@cleanup) {
#		$conn->close();
#	}
#	if(defined($conn)) { $conn->close(); }
	#$conn->delete();
	#$mgr->delete();
#	print STDERR qq/$conn\n/;
	print STDERR qq/Error connecting to $server ID:$count for ISBN: 
$inputisbn\n/;
	die("\n");
 };

The final code would only use one of these, hopefully, the rest are there for 
easier debugging with comments etc.

Line 310 is:
    die "$this can't forget $conn";

part of:
sub forget {
    my $this = shift();
    my($conn) = @_;

    my $connections = $this->{connections};
    my $n = @$connections;
    $this->warnconns("forget()", "looking for $conn");
    for (my $i = 0; $i < $n; $i++) {
        if (defined $connections->[$i] && $connections->[$i] eq $conn) {
            $this->warnconns("pre-splice", "forgetting $i of $n");
            splice @{ $this->{connections} }, $i, 1;
            $this->warnconns("post-splice", "forgot $i of $n");
            return;
        }
    }

    die "$this can't forget $conn";
}

This happens with servers that ARE running but which refuse a connection. The 
full output from the existing (unwrapped) cron task is:

Event: trapped error: [sauco.csic.es:9909] connection refused
  in Event::io=HASH(0x837e2fc) --
              poll: 'w'
                fd: 'IO::Handle=GLOB(0x837e47c)'
           timeout: 0
        timeout_cb: <undef>
                cb: 'CODE(0x82057a8)'
            cbtime: 1067637610.03063
              desc: '?? Connection.pm:146'
             debug: ''
              prio: 4
         reentrant: 1
            repeat: 1
         max_cb_tm: 1

(Hence why I want to wrap it!)

If I try to close the connection at the point of error (later in the code), I 
get:
Can't call method "close" on an undefined value
 (in cleanup) Net::Z3950::Manager=HASH(0x82ab24c) can't forget 
etc.

This reflects a bit about what we were discussing about new() failures - how 
can I close a connection when the handler is undefined?

To me, the answer SHOULD be: You shouldn't have to close it - if the handler 
is undefined, there isn't a connection!

If a server refuses a connection, as in this case, why should a module require 
the refused connection to be closed?

> I suppose evaling your call and checking $@ might be a good way of
> handling this situation too.

Using:
	$conn = eval( $mgr->connect($server, $port));
instead of :
	$conn = $mgr->connect($server, $port);

generates the cleanup error EVERYTIME, even on successful queries.

What's going on in cleanup and how can I pre-empt it?

If I try to use $conn->close(), the sub doesn't output the final print STDERR 
statement, it goes straight to cleanup and the error.

$! returns Connection refused - can I use that?

-- 

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/20031031/40ea5b76/attachment.bin


More information about the Devoncornwall-pm mailing list